|
| 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\SpotHit; |
| 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\ClientExceptionInterface; |
| 22 | +use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface; |
| 23 | +use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; |
| 24 | +use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; |
| 25 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 26 | + |
| 27 | +/** |
| 28 | + * @author James Hemery <james@yieldstudio.fr> |
| 29 | + */ |
| 30 | +final class SpotHitTransport extends AbstractTransport |
| 31 | +{ |
| 32 | + protected const HOST = 'spot-hit.fr'; |
| 33 | + |
| 34 | + private $token; |
| 35 | + private $from; |
| 36 | + |
| 37 | + public function __construct(string $token, ?string $from = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null) |
| 38 | + { |
| 39 | + $this->token = $token; |
| 40 | + $this->from = $from; |
| 41 | + |
| 42 | + parent::__construct($client, $dispatcher); |
| 43 | + } |
| 44 | + |
| 45 | + public function __toString(): string |
| 46 | + { |
| 47 | + if (!$this->from) { |
| 48 | + return sprintf('spothit://%s', $this->getEndpoint()); |
| 49 | + } |
| 50 | + |
| 51 | + return sprintf('spothit://%s?from=%s', $this->getEndpoint(), $this->from); |
| 52 | + } |
| 53 | + |
| 54 | + public function supports(MessageInterface $message): bool |
| 55 | + { |
| 56 | + return $message instanceof SmsMessage; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @param MessageInterface|SmsMessage $message |
| 61 | + * |
| 62 | + * @throws TransportExceptionInterface |
| 63 | + * @throws ClientExceptionInterface |
| 64 | + * @throws RedirectionExceptionInterface |
| 65 | + * @throws ServerExceptionInterface |
| 66 | + */ |
| 67 | + protected function doSend(MessageInterface $message): SentMessage |
| 68 | + { |
| 69 | + if (!$this->supports($message)) { |
| 70 | + throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); |
| 71 | + } |
| 72 | + |
| 73 | + $endpoint = sprintf('https://www.%s/api/envoyer/sms', $this->getEndpoint()); |
| 74 | + $response = $this->client->request('POST', $endpoint, [ |
| 75 | + 'body' => [ |
| 76 | + 'key' => $this->token, |
| 77 | + 'destinataires' => $message->getPhone(), |
| 78 | + 'type' => 'premium', |
| 79 | + 'message' => $message->getSubject(), |
| 80 | + 'expediteur' => $this->from, |
| 81 | + ], |
| 82 | + ]); |
| 83 | + |
| 84 | + $data = json_decode($response->getContent(), true); |
| 85 | + |
| 86 | + if (!$data['resultat']) { |
| 87 | + $errors = \is_array($data['erreurs']) ? implode(',', $data['erreurs']) : $data['erreurs']; |
| 88 | + throw new TransportException(sprintf('[HTTP %d] Unable to send the SMS: error(s) "%s".', $response->getStatusCode(), $errors), $response); |
| 89 | + } |
| 90 | + |
| 91 | + $sentMessage = new SentMessage($message, (string) $this); |
| 92 | + $sentMessage->setMessageId($data['id']); |
| 93 | + |
| 94 | + return $sentMessage; |
| 95 | + } |
| 96 | +} |
0 commit comments