|
| 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\Expo; |
| 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\ChatMessage; |
| 18 | +use Symfony\Component\Notifier\Message\MessageInterface; |
| 19 | +use Symfony\Component\Notifier\Message\SentMessage; |
| 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 Imad ZAIRIG <https://github.com/zairigimad> |
| 27 | + */ |
| 28 | +final class ExpoTransport extends AbstractTransport |
| 29 | +{ |
| 30 | + protected const HOST = 'exp.host/--/api/v2/push/send'; |
| 31 | + |
| 32 | + /** @var string */ |
| 33 | + private $token; |
| 34 | + |
| 35 | + public function __construct(string $token = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null) |
| 36 | + { |
| 37 | + $this->token = $token; |
| 38 | + $this->client = $client; |
| 39 | + |
| 40 | + parent::__construct($client, $dispatcher); |
| 41 | + } |
| 42 | + |
| 43 | + public function __toString(): string |
| 44 | + { |
| 45 | + return sprintf('expo://%s', $this->getEndpoint()); |
| 46 | + } |
| 47 | + |
| 48 | + public function supports(MessageInterface $message): bool |
| 49 | + { |
| 50 | + return $message instanceof ChatMessage; |
| 51 | + } |
| 52 | + |
| 53 | + protected function doSend(MessageInterface $message): SentMessage |
| 54 | + { |
| 55 | + if (!$message instanceof ChatMessage) { |
| 56 | + throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message); |
| 57 | + } |
| 58 | + |
| 59 | + $endpoint = sprintf('https://%s', $this->getEndpoint()); |
| 60 | + $options = ($opts = $message->getOptions()) ? $opts->toArray() : []; |
| 61 | + if (!isset($options['to'])) { |
| 62 | + $options['to'] = $message->getRecipientId(); |
| 63 | + } |
| 64 | + if (null === $options['to']) { |
| 65 | + throw new InvalidArgumentException(sprintf('The "%s" transport required the "to" option to be set.', __CLASS__)); |
| 66 | + } |
| 67 | + $options['body'] = $message->getSubject(); |
| 68 | + $options['data'] = $options['data'] ?? []; |
| 69 | + |
| 70 | + $response = $this->client->request('POST', $endpoint, [ |
| 71 | + 'headers' => [ |
| 72 | + 'Authorization' => $this->token ? sprintf('Bearer %s', $this->token) : null, |
| 73 | + ], |
| 74 | + 'json' => array_filter($options), |
| 75 | + ]); |
| 76 | + |
| 77 | + try { |
| 78 | + $statusCode = $response->getStatusCode(); |
| 79 | + } catch (TransportExceptionInterface $e) { |
| 80 | + throw new TransportException('Could not reach the remote Expo server.', $response, 0, $e); |
| 81 | + } |
| 82 | + |
| 83 | + $contentType = $response->getHeaders(false)['content-type'][0] ?? ''; |
| 84 | + $jsonContents = 0 === strpos($contentType, 'application/json') ? $response->toArray(false) : null; |
| 85 | + |
| 86 | + if (200 !== $statusCode) { |
| 87 | + $errorMessage = $jsonContents['error']['message'] ?? $response->getContent(false); |
| 88 | + |
| 89 | + throw new TransportException('Unable to post the Expo message: '.$errorMessage, $response); |
| 90 | + } |
| 91 | + |
| 92 | + $success = $response->toArray(false); |
| 93 | + |
| 94 | + $sentMessage = new SentMessage($message, (string) $this); |
| 95 | + $sentMessage->setMessageId($success['data']['id']); |
| 96 | + |
| 97 | + return $sentMessage; |
| 98 | + } |
| 99 | +} |
0 commit comments