|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Notifier\Bridge\Sweego; |
| 13 | + |
| 14 | +use Symfony\Component\Notifier\Exception\TransportException; |
| 15 | +use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException; |
| 16 | +use Symfony\Component\Notifier\Message\MessageInterface; |
| 17 | +use Symfony\Component\Notifier\Message\SentMessage; |
| 18 | +use Symfony\Component\Notifier\Message\SmsMessage; |
| 19 | +use Symfony\Component\Notifier\Transport\AbstractTransport; |
| 20 | +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
| 21 | +use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; |
| 22 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 23 | + |
| 24 | +/** |
| 25 | + * @author Mathieu Santostefano <msantostefano@protonmail.com> |
| 26 | + */ |
| 27 | +final class SweegoTransport extends AbstractTransport |
| 28 | +{ |
| 29 | + protected const HOST = 'api.sweego.io'; |
| 30 | + |
| 31 | + public function __construct( |
| 32 | + #[\SensitiveParameter] private readonly string $apiKey, |
| 33 | + private readonly string $region, |
| 34 | + private readonly string $campaignType, |
| 35 | + private readonly ?bool $bat, |
| 36 | + private readonly ?string $campaignId, |
| 37 | + private readonly ?bool $shortenUrls, |
| 38 | + private readonly ?bool $shortenWithProtocol, |
| 39 | + ?HttpClientInterface $client = null, |
| 40 | + ?EventDispatcherInterface $dispatcher = null, |
| 41 | + ) { |
| 42 | + parent::__construct($client, $dispatcher); |
| 43 | + } |
| 44 | + |
| 45 | + public function __toString(): string |
| 46 | + { |
| 47 | + return \sprintf('sweego://%s%s', $this->getEndpoint(), '?'.http_build_query([ |
| 48 | + SweegoOptions::REGION => $this->region, |
| 49 | + SweegoOptions::CAMPAIGN_TYPE => $this->campaignType, |
| 50 | + SweegoOptions::BAT => $this->bat, |
| 51 | + SweegoOptions::CAMPAIGN_ID => $this->campaignId, |
| 52 | + SweegoOptions::SHORTEN_URLS => $this->shortenUrls, |
| 53 | + SweegoOptions::SHORTEN_WITH_PROTOCOL => $this->shortenWithProtocol, |
| 54 | + ])); |
| 55 | + } |
| 56 | + |
| 57 | + public function supports(MessageInterface $message): bool |
| 58 | + { |
| 59 | + return $message instanceof SmsMessage |
| 60 | + && (null === $message->getOptions() || $message->getOptions() instanceof SweegoOptions); |
| 61 | + } |
| 62 | + |
| 63 | + protected function doSend(MessageInterface $message): SentMessage |
| 64 | + { |
| 65 | + if (!$message instanceof SmsMessage) { |
| 66 | + throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); |
| 67 | + } |
| 68 | + |
| 69 | + $options = $message->getOptions()?->toArray() ?? []; |
| 70 | + |
| 71 | + $body = [ |
| 72 | + 'recipients' => [ |
| 73 | + [ |
| 74 | + 'num' => $message->getPhone(), |
| 75 | + 'region' => $options[SweegoOptions::REGION] ?? $this->region, |
| 76 | + ], |
| 77 | + ], |
| 78 | + 'message-txt' => $message->getSubject(), |
| 79 | + 'channel' => 'sms', |
| 80 | + 'provider' => 'sweego', |
| 81 | + ]; |
| 82 | + |
| 83 | + $body = $this->setBat($body, $options); |
| 84 | + $body = $this->setCampaignType($body, $options); |
| 85 | + $body = $this->setCampaignId($body, $options); |
| 86 | + $body = $this->setShortenUrls($body, $options); |
| 87 | + $body = $this->setShortenWithProtocol($body, $options); |
| 88 | + |
| 89 | + $endpoint = \sprintf('https://%s/send', $this->getEndpoint()); |
| 90 | + $response = $this->client->request('POST', $endpoint, [ |
| 91 | + 'headers' => [ |
| 92 | + 'Api-Key' => $this->apiKey, |
| 93 | + ], |
| 94 | + 'json' => array_filter($body), |
| 95 | + ]); |
| 96 | + |
| 97 | + try { |
| 98 | + $statusCode = $response->getStatusCode(); |
| 99 | + } catch (TransportExceptionInterface $e) { |
| 100 | + throw new TransportException('Could not reach the remote Sweego server.', $response, 0, $e); |
| 101 | + } |
| 102 | + |
| 103 | + if (200 !== $statusCode) { |
| 104 | + throw new TransportException('Unable to send the SMS.', $response); |
| 105 | + } |
| 106 | + |
| 107 | + $success = $response->toArray(false); |
| 108 | + |
| 109 | + $sentMessage = new SentMessage($message, (string) $this); |
| 110 | + $sentMessage->setMessageId(array_values($success['swg_uids'])[0]); |
| 111 | + |
| 112 | + return $sentMessage; |
| 113 | + } |
| 114 | + |
| 115 | + private function setBat(array $body, array $options): array |
| 116 | + { |
| 117 | + $body['bat'] = (bool) ($options[SweegoOptions::BAT] ?? $this->bat); |
| 118 | + |
| 119 | + return $body; |
| 120 | + } |
| 121 | + |
| 122 | + private function setCampaignType(array $body, array $options): array |
| 123 | + { |
| 124 | + $body['campaign-type'] = $this->campaignType; |
| 125 | + |
| 126 | + if (\array_key_exists(SweegoOptions::CAMPAIGN_TYPE, $options) && \is_string($options[SweegoOptions::CAMPAIGN_TYPE])) { |
| 127 | + $body['campaign-type'] = $options[SweegoOptions::CAMPAIGN_TYPE]; |
| 128 | + } |
| 129 | + |
| 130 | + return $body; |
| 131 | + } |
| 132 | + |
| 133 | + private function setCampaignId(array $body, array $options): array |
| 134 | + { |
| 135 | + $body['campaign-id'] = $this->campaignId; |
| 136 | + |
| 137 | + if (\array_key_exists(SweegoOptions::CAMPAIGN_ID, $options) && \is_string($options[SweegoOptions::CAMPAIGN_ID])) { |
| 138 | + $body['campaign-id'] = $options[SweegoOptions::CAMPAIGN_ID]; |
| 139 | + } |
| 140 | + |
| 141 | + return $body; |
| 142 | + } |
| 143 | + |
| 144 | + private function setShortenUrls(array $body, array $options): array |
| 145 | + { |
| 146 | + $body['shorten_urls'] = (bool) ($options[SweegoOptions::SHORTEN_URLS] ?? $this->shortenUrls); |
| 147 | + |
| 148 | + return $body; |
| 149 | + } |
| 150 | + |
| 151 | + private function setShortenWithProtocol(array $body, array $options): array |
| 152 | + { |
| 153 | + $body['shorten_with_protocol'] = (bool) ($options[SweegoOptions::SHORTEN_WITH_PROTOCOL] ?? $this->shortenWithProtocol); |
| 154 | + |
| 155 | + return $body; |
| 156 | + } |
| 157 | +} |
0 commit comments