|
| 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\MessageMedia; |
| 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\HttpClientInterface; |
| 22 | + |
| 23 | +/** |
| 24 | + * @author Adrian Nguyen <vuphuong87@gmail.com> |
| 25 | + */ |
| 26 | +final class MessageMediaTransport extends AbstractTransport |
| 27 | +{ |
| 28 | + protected const HOST = 'api.messagemedia.com'; |
| 29 | + |
| 30 | + private $apiKey; |
| 31 | + private $apiSecret; |
| 32 | + private $from; |
| 33 | + |
| 34 | + public function __construct(string $apiKey, string $apiSecret, ?string $from = null, ?HttpClientInterface $client = null, ?EventDispatcherInterface $dispatcher = null) |
| 35
A79
code> | + { |
| 36 | + $this->apiKey = $apiKey; |
| 37 | + $this->apiSecret = $apiSecret; |
| 38 | + $this->from = $from; |
| 39 | + |
| 40 | + parent::__construct($client, $dispatcher); |
| 41 | + } |
| 42 | + |
| 43 | + public function __toString(): string |
| 44 | + { |
| 45 | + if (null !== $this->from) { |
| 46 | + return sprintf('messagemedia://%s?from=%s', $this->getEndpoint(), $this->from); |
| 47 | + } |
| 48 | + |
| 49 | + return sprintf('messagemedia://%s', $this->getEndpoint()); |
| 50 | + } |
| 51 | + |
| 52 | + public function supports(MessageInterface $message): bool |
| 53 | + { |
| 54 | + return $message instanceof SmsMessage; |
| 55 | + } |
| 56 | + |
| 57 | + protected function doSend(MessageInterface $message): SentMessage |
| 58 | + { |
| 59 | + if (!$message instanceof SmsMessage) { |
| 60 | + throw new UnsupportedMessageTypeException(__CLASS__, SmsMessage::class, $message); |
| 61 | + } |
| 62 | + |
| 63 | + $endpoint = sprintf('https://%s/v1/messages', $this->getEndpoint()); |
| 64 | + $response = $this->client->request( |
| 65 | + 'POST', |
| 66 | + $endpoint, |
| 67 | + [ |
| 68 | + 'auth_basic' => $this->apiKey.':'.$this->apiSecret, |
| 69 | + 'body' => [ |
| 70 | + 'destination_number' => $message->getPhone(), |
| 71 | + 'source_number' => $this->from, |
| 72 | + 'content' => $message->getSubject(), |
| 73 | + ], |
| 74 | + ] |
| 75 | + ); |
| 76 | + |
| 77 | + if (202 !== $response->getStatusCode()) { |
| 78 | + $error = $response->toArray(false); |
| 79 | + |
| 80 | + throw new TransportException(sprintf('Unable to send the SMS: "%s".', $error['message']), $response); |
| 81 | + } |
| 82 | + |
| 83 | + $success = $response->toArray(false)[0]; |
| 84 | + |
| 85 | + if (false === isset($success['message_id'])) { |
| 86 | + throw new TransportException(sprintf('Unable to send the SMS: "%s".', $success['message']), $response); |
| 87 | + } |
| 88 | + |
| 89 | + $sentMessage = new SentMessage($message, (string) $this); |
| 90 | + $sentMessage->setMessageId($success['message_id']); |
| 91 | + |
| 92 | + return $sentMessage; |
| 93 | + } |
| 94 | +} |
0 commit comments