|
| 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\Novu; |
| 13 | + |
| 14 | +use Symfony\Component\Notifier\Exception\TransportException; |
| 15 | +use Symfony\Component\Notifier\Exception\TransportExceptionInterface; |
| 16 | +use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException; |
| 17 | +use Symfony\Component\Notifier\Message\MessageInterface; |
| 18 | +use Symfony\Component\Notifier\Message\PushMessage; |
| 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\HttpClientInterface; |
| 23 | + |
| 24 | +/** |
| 25 | + * @author Wouter van der Loop <woutervdl@toppy.nl> |
| 26 | + */ |
| 27 | +class NovuTransport extends AbstractTransport |
| 28 | +{ |
| 29 | + protected const HOST = 'web.novu.co'; |
| 30 | + |
| 31 | + private string $apiKey; |
| 32 | + |
| 33 | + public function __construct( |
| 34 | + #[\SensitiveParameter] string $apiKey, |
| 35 | + HttpClientInterface $client = null, |
| 36 | + EventDispatcherInterface $dispatcher = null |
| 37 | + ) { |
| 38 | + $this->apiKey = $apiKey; |
| 39 | + parent::__construct($client, $dispatcher); |
| 40 | + } |
| 41 | + |
| 42 | + public function __toString(): string |
| 43 | + { |
| 44 | + return sprintf('novu://%s', $this->getEndpoint()); |
| 45 | + } |
| 46 | + |
| 47 | + public function supports(MessageInterface $message): bool |
| 48 | + { |
| 49 | + return $message instanceof PushMessage && (null === $message->getOptions() || $message->getOptions() instanceof NovuOptions); |
| 50 | + } |
| 51 | + |
| 52 | + protected function doSend(MessageInterface $message): SentMessage |
| 53 | + { |
| 54 | + if (!$message instanceof PushMessage) { |
| 55 | + throw new UnsupportedMessageTypeException(__CLASS__, PushMessage::class, $message); |
| 56 | + } |
| 57 | + |
| 58 | + $options = $message->getOptions()?->toArray() ?? []; |
| 59 | + |
| 60 | + $body = [ |
| 61 | + 'name' => $message->getSubject(), |
| 62 | + 'to' => [ |
| 63 | + 'subscriberId' => $message->getRecipientId(), |
| 64 | + 'firstName' => $options['firstName'], |
| 65 | + 'lastName' => $options['lastName'], |
| 66 | + 'email' => $options['email'], |
| 67 | + 'phone' => $options['phone'], |
| 68 | + 'avatar' => $options['avatar'], |
| 69 | + 'locale' => $options['locale'], |
| 70 | + ], |
| 71 | + 'payload' => json_decode($message->getContent()), |
| 72 | + ]; |
| 73 | + |
| 74 | + $endpoint = sprintf('https://%s/v1/events/trigger', $this->getEndpoint()); |
| 75 | + $response = $this->client->request('POST', $endpoint, [ |
| 76 | + 'body' => $body, |
| 77 | + 'headers' => [ |
| 78 | + 'Authorization' => sprintf('ApiKey %s', $this->apiKey), |
| 79 | + 'Content-Type' => 'application/json', |
| 80 | + ], |
| 81 | + ]); |
| 82 | + |
| 83 | + try { |
| 84 | + $statusCode = $response->getStatusCode(); |
| 85 | + } catch (TransportExceptionInterface $e) { |
| 86 | + throw new TransportException('Could not reach the remote Novu server.', $response, 0, $e); |
| 87 | + } |
| 88 | + |
| 89 | + if (201 !== $statusCode) { |
| 90 | + $originalContent = $message->getSubject(); |
| 91 | + $result = $response->toArray(false); |
| 92 | + $error = $result['message']; |
| 93 | + throw new TransportException(sprintf('Unable to post the Novu message: "%s" (%d: "%s").', $originalContent, $statusCode, $error), $response); |
| 94 | + } |
| 95 | + |
| 96 | + return new SentMessage($message, (string) $this); |
| 97 | + } |
| 98 | +} |
0 commit comments