vendor/symfony/firebase-notifier/FirebaseTransport.php line 34

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Notifier\Bridge\Firebase;
  11. use Symfony\Component\Notifier\Exception\InvalidArgumentException;
  12. use Symfony\Component\Notifier\Exception\TransportException;
  13. use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
  14. use Symfony\Component\Notifier\Message\ChatMessage;
  15. use Symfony\Component\Notifier\Message\MessageInterface;
  16. use Symfony\Component\Notifier\Message\SentMessage;
  17. use Symfony\Component\Notifier\Transport\AbstractTransport;
  18. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  19. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  20. use Symfony\Contracts\HttpClient\HttpClientInterface;
  21. /**
  22.  * @author Jeroen Spee <https://github.com/Jeroeny>
  23.  */
  24. final class FirebaseTransport extends AbstractTransport
  25. {
  26.     protected const HOST 'fcm.googleapis.com/fcm/send';
  27.     private string $token;
  28.     public function __construct(string $tokenHttpClientInterface $client nullEventDispatcherInterface $dispatcher null)
  29.     {
  30.         $this->token $token;
  31.         $this->client $client;
  32.         parent::__construct($client$dispatcher);
  33.     }
  34.     public function __toString(): string
  35.     {
  36.         return sprintf('firebase://%s'$this->getEndpoint());
  37.     }
  38.     public function supports(MessageInterface $message): bool
  39.     {
  40.         return $message instanceof ChatMessage;
  41.     }
  42.     protected function doSend(MessageInterface $message): SentMessage
  43.     {
  44.         if (!$message instanceof ChatMessage) {
  45.             throw new UnsupportedMessageTypeException(__CLASS__ChatMessage::class, $message);
  46.         }
  47.         $endpoint sprintf('https://%s'$this->getEndpoint());
  48.         $options = ($opts $message->getOptions()) ? $opts->toArray() : [];
  49.         if (!isset($options['to'])) {
  50.             $options['to'] = $message->getRecipientId();
  51.         }
  52.         if (null === $options['to']) {
  53.             throw new InvalidArgumentException(sprintf('The "%s" transport required the "to" option to be set.'__CLASS__));
  54.         }
  55.         $options['notification'] = $options['notification'] ?? [];
  56.         $options['notification']['body'] = $message->getSubject();
  57.         $options['data'] = $options['data'] ?? [];
  58.         $response $this->client->request('POST'$endpoint, [
  59.             'headers' => [
  60.                 'Authorization' => sprintf('key=%s'$this->token),
  61.             ],
  62.             'json' => array_filter($options),
  63.         ]);
  64.         try {
  65.             $statusCode $response->getStatusCode();
  66.         } catch (TransportExceptionInterface $e) {
  67.             throw new TransportException('Could not reach the remote Firebase server.'$response0$e);
  68.         }
  69.         $contentType $response->getHeaders(false)['content-type'][0] ?? '';
  70.         $jsonContents str_starts_with($contentType'application/json') ? $response->toArray(false) : null;
  71.         $errorMessage null;
  72.         if ($jsonContents && isset($jsonContents['results'][0]['error'])) {
  73.             $errorMessage $jsonContents['results'][0]['error'];
  74.         } elseif (200 !== $statusCode) {
  75.             $errorMessage $response->getContent(false);
  76.         }
  77.         if (null !== $errorMessage) {
  78.             throw new TransportException('Unable to post the Firebase message: '.$errorMessage$response);
  79.         }
  80.         $success $response->toArray(false);
  81.         $sentMessage = new SentMessage($message, (string) $this);
  82.         $sentMessage->setMessageId($success['results'][0]['message_id'] ?? '');
  83.         return $sentMessage;
  84.     }
  85. }