-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSweegoTransport.php
157 lines (128 loc) · 5.33 KB
/
SweegoTransport.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Notifier\Bridge\Sweego;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Message\SmsMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
/**
* @author Mathieu Santostefano <msantostefano@protonmail.com>
*/
final class SweegoTransport extends AbstractTransport
{
protected const HOST = 'api.sweego.io';
public function __construct(
#[\SensitiveParameter] private readonly string $apiKey,
private readonly string $region,
private readonly string $campaignType,
private readonly ?bool $bat,
private readonly ?string $campaignId,
private readonly ?bool $shortenUrls,
private readonly ?bool $shortenWithProtocol,
?HttpClientInterface $client = null,
?EventDispatcherInterface $dispatcher = null,
) {
parent::__construct($client, $dispatcher);
}
public function __toString(): string
{
return \sprintf('sweego://%s%s', $this->getEndpoint(), '?'.http_build_query([
SweegoOptions::REGION => $this->region,
SweegoOptions::CAMPAIGN_TYPE => $this->campaignType,
SweegoOptions::BAT => $this->bat,
SweegoOptions::CAMPAIGN_ID => $this->campaignId,
SweegoOptions::SHORTEN_URLS => $this->shortenUrls,
SweegoOptions::SHORTEN_WITH_PROTOCOL => $this->shortenWithProtocol,
]));
}
public function supports(MessageInterface $message): bool
{
return $message instanceof SmsMessage
&& (null === $message->getOptions() || $message->getOptions() instanceof SweegoOptions);
}
protected function doSend(MessageInterface $message): SentMessage
{
if (!$message instanceof SmsMessage) {
throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message);
}
$options = $message->getOptions()?->toArray() ?? [];
$body = [
'recipients' => [
[
'num' => $message->getPhone(),
'region' => $options[SweegoOptions::REGION] ?? $this->region,
],
],
'message-txt' => $message->getSubject(),
'channel' => 'sms',
'provider' => 'sweego',
];
$body = $this->setBat($body, $options);
$body = $this->setCampaignType($body, $options);
$body = $this->setCampaignId($body, $options);
$body = $this->setShortenUrls($body, $options);
$body = $this->setShortenWithProtocol($body, $options);
$endpoint = \sprintf('https://%s/send', $this->getEndpoint());
$response = $this->client->request('POST', $endpoint, [
'headers' => [
'Api-Key' => $this->apiKey,
],
'json' => array_filter($body),
]);
try {
$statusCode = $response->getStatusCode();
} catch (TransportExceptionInterface $e) {
throw new TransportException('Could not reach the remote Sweego server.', $response, 0, $e);
}
if (200 !== $statusCode) {
throw new TransportException('Unable to send the SMS.', $response);
}
$success = $response->toArray(false);
$sentMessage = new SentMessage($message, (string) $this);
$sentMessage->setMessageId(array_values($success['swg_uids'])[0]);
return $sentMessage;
}
private function setBat(array $body, array $options): array
{
$body['bat'] = (bool) ($options[SweegoOptions::BAT] ?? $this->bat);
return $body;
}
private function setCampaignType(array $body, array $options): array
{
$body['campaign-type'] = $this->campaignType;
if (\array_key_exists(SweegoOptions::CAMPAIGN_TYPE, $options) && \is_string($options[SweegoOptions::CAMPAIGN_TYPE])) {
$body['campaign-type'] = $options[SweegoOptions::CAMPAIGN_TYPE];
}
return $body;
}
private function setCampaignId(array $body, array $options): array
{
$body['campaign-id'] = $this->campaignId;
if (\array_key_exists(SweegoOptions::CAMPAIGN_ID, $options) && \is_string($options[SweegoOptions::CAMPAIGN_ID])) {
$body['campaign-id'] = $options[SweegoOptions::CAMPAIGN_ID];
}
return $body;
}
private function setShortenUrls(array $body, array $options): array
{
$body['shorten_urls'] = (bool) ($options[SweegoOptions::SHORTEN_URLS] ?? $this->shortenUrls);
return $body;
}
private function setShortenWithProtocol(array $body, array $options): array
{
$body['shorten_with_protocol'] = (bool) ($options[SweegoOptions::SHORTEN_WITH_PROTOCOL] ?? $this->shortenWithProtocol);
return $body;
}
}