-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractApiTransport.php
77 lines (66 loc) · 2.5 KB
/
AbstractApiTransport.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
/*
* This file is part of the Klipper package.
*
* (c) François Pluchino <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Klipper\Component\SmsSender\Transport;
use Klipper\Component\SmsSender\Envelope;
use Klipper\Component\SmsSender\Exception\InvalidArgumentException;
use Klipper\Component\SmsSender\Exception\TransportException;
use Klipper\Component\SmsSender\Exception\TransportResultException;
use Klipper\Component\SmsSender\Mime\Sms;
use Klipper\Component\SmsSender\SentMessage;
use Psr\Log\LoggerInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
/**
* Abstract class for the api transport.
*
* @author François Pluchino <[email protected]>
*/
abstract class AbstractApiTransport extends AbstractTransport
{
protected ?HttpClientInterface $client;
/**
* @param null|HttpClientInterface $client The http client
* @param null|EventDispatcherInterface $dispatcher The event dispatcher
* @param null|LoggerInterface $logger The logger
*/
public function __construct(
?HttpClientInterface $client = null,
?EventDispatcherInterface $dispatcher = null,
?LoggerInterface $logger = null
) {
parent::__construct($dispatcher, $logger);
$this->client = $client;
}
protected function doSend(SentMessage $message): void
{
try {
$sms = $message->getOriginalMessage();
if (!$sms instanceof Sms) {
throw new InvalidArgumentException(sprintf('The message must be an instance %s ("%s" given).', Sms::class, \get_class($sms)));
}
} catch (\Throwable $e) {
throw new TransportException(sprintf('Unable to send message with the "%s" transport: %s', static::class, $e->getMessage()), 0, $e);
}
$this->doSendSms($sms, $message->getEnvelope(), $message->getResult());
if ($message->getResult()->hasErrors()) {
throw new TransportResultException($message->getResult());
}
}
/**
* Action to send the SMS.
*
* @param Sms $sms The SMS message
* @param Envelope $envelope The SMS envelope
* @param Result $result The result wrapper
*
* @throws
*/
abstract protected function doSendSms(Sms $sms, Envelope $envelope, Result $result): void;
}