|
| 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\Smsmode; |
| 13 | + |
| 14 | +use Symfony\Component\Notifier\Exception\InvalidArgumentException; |
| 15 | +use Symfony\Component\Notifier\Exception\TransportException; |
| 16 | +use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException; |
| 17 | +use Symfony\Component\Notifier\Message\MessageInterface; |
| 18 | +use Symfony\Component\Notifier\Message\SentMessage; |
| 19 | +use Symfony\Component\Notifier\Message\SmsMessage; |
| 20 | +use Symfony\Component\Notifier\Transport\AbstractTransport; |
| 21 | +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
| 22 | +use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; |
| 23 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author gnito-org <https://github.com/gnito-org> |
| 27 | + */ |
| 28 | +final class SmsmodeTransport extends AbstractTransport |
| 29 | +{ |
| 30 | + protected const HOST = 'rest.smsmode.com'; |
| 31 | + |
| 32 | + public function __construct( |
| 33 | + #[\SensitiveParameter] private readonly string $apiKey, |
| 34 | + private readonly ?string $from = null, |
| 35 | + HttpClientInterface $client = null, |
| 36 | + EventDispatcherInterface $dispatcher = null |
| 37 | + ) { |
| 38 | + parent::__construct($client, $dispatcher); |
| 39 | + } |
| 40 | + |
| 41 | + public function __toString(): string |
| 42 | + { |
| 43 | + $queryParameters = []; |
| 44 | + if ($this->from) { |
| 45 | + $queryParameters['from'] = $this->from; |
| 46 | + } |
| 47 | + |
| 48 | + return sprintf('smsmode://%s', $this->getEndpoint()).($queryParameters ? '?'.http_build_query($queryParameters) : null); |
| 49 | + } |
| 50 | + |
| 51 | + public function supports(MessageInterface $message): bool |
| 52 | + { |
| 53 | + return $message instanceof SmsMessage && (null === $message->getOptions() || $message->getOptions() instanceof SmsmodeOptions); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * https://dev.smsmode.com/sms/v1/message. |
| 58 | + */ |
| 59 | + protected function doSend(MessageInterface $message): SentMessage |
| 60 | + { |
| 61 | + if (!$message instanceof SmsMessage) { |
| 62 | + throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); |
| 63 | + } |
| 64 | + |
| 65 | + $endpoint = sprintf('https://%s/sms/v1/messages', $this->getEndpoint()); |
| 66 | + |
| 67 | + $opts = $message->getOptions(); |
| 68 | + $options = $opts ? $opts->toArray() : []; |
| 69 | + $options['body']['text'] = $message->getSubject(); |
| 70 | + $options['recipient']['to'] = $message->getPhone(); |
| 71 | + |
| 72 | + if (!isset($options['from'])) { |
| 73 | + $options['from'] = $this->from; |
| 74 | + } |
| 75 | + |
| 76 | + if (!preg_match('/^[a-zA-Z0-9\s]{1,11}$/', $options['from'] ?? '')) { |
| 77 | + throw new InvalidArgumentException(sprintf('The "From" value "%s" is not a valid sender ID.', $this->from)); |
| 78 | + } |
| 79 | + |
| 80 | + if (isset($options['sent_date'])) { |
| 81 | + $options['sentDate'] = $options['sent_date']; |
| 82 | + unset($options['sent_date']); |
| 83 | + } |
| 84 | + |
| 85 | + if (isset($options['ref_client'])) { |
| 86 | + $options['refClient'] = $options['ref_client']; |
| 87 | + unset($options['ref_client']); |
| 88 | + } |
| 89 | + |
| 90 | + $response = $this->client->request('POST', $endpoint, [ |
| 91 | + 'headers' => ['X-Api-Key' => $this->apiKey], |
| 92 | + 'json' => array_filter($options), |
| 93 | + ]); |
| 94 | + |
| 95 | + try { |
| 96 | + $statusCode = $response->getStatusCode(); |
| 97 | + } catch (TransportExceptionInterface $e) { |
| 98 | + throw new TransportException('Could not reach the remote Smsmode server.', $response, 0, $e); |
| 99 | + } |
| 100 | + |
| 101 | + if (201 !== $statusCode) { |
| 102 | + $error = $response->getContent(false); |
| 103 | + throw new TransportException(sprintf('Unable to send the SMS - "%s".', $error ?: 'unknown failure'), $response); |
| 104 | + } |
| 105 | + |
| 106 | + $success = $response->toArray(false); |
| 107 | + |
| 108 | + $sentMessage = new SentMessage($message, (string) $this); |
| 109 | + $sentMessage->setMessageId($success['messageId']); |
| 110 | + |
| 111 | + return $sentMessage; |
| 112 | + } |
| 113 | +} |
0 commit comments