|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Ntfy\Symfony\Component\Notifier\Bridge\Ntfy; |
| 4 | + |
| 5 | +use Symfony\Component\Notifier\Exception\LogicException; |
| 6 | +use Symfony\Component\Notifier\Exception\TransportException; |
| 7 | +use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException; |
| 8 | +use Symfony\Component\Notifier\Message\MessageInterface; |
| 9 | +use Symfony\Component\Notifier\Message\PushMessage; |
| 10 | +use Symfony\Component\Notifier\Message\SentMessage; |
| 11 | +use Symfony\Component\Notifier\Notification\Notification; |
| 12 | +use Symfony\Component\Notifier\Transport\AbstractTransport; |
| 13 | +use Symfony\Component\Notifier\Transport\Dsn; |
| 14 | +use Symfony\Contracts\EventDispatcher\EventDispatcherInterface; |
| 15 | +use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; |
| 16 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 17 | + |
| 18 | +final class NtfyTransport extends AbstractTransport |
| 19 | +{ |
| 20 | + protected const HOST = 'ntfy.sh'; |
| 21 | + |
| 22 | + private $dsn; |
| 23 | + private $topic; |
| 24 | + private $user; |
| 25 | + private $password; |
| 26 | + private $scheme; |
| 27 | + |
| 28 | + public function __construct(Dsn $dsn, string $topic, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null) |
| 29 | + { |
| 30 | + $this->dsn = $dsn; |
| 31 | + $this->topic = $topic; |
| 32 | + |
| 33 | + parent::__construct($client, $dispatcher); |
| 34 | + } |
| 35 | + |
| 36 | + private static function getNtfyHeadersFromMessage(PushMessage $message): array |
| 37 | + { |
| 38 | + $notification = $message->getNotification(); |
| 39 | + $ntfyHeaders = []; |
| 40 | + |
| 41 | + $ntfyHeaders['Title'] = $notification->getSubject() ?? $message->getSubject(); |
| 42 | + |
| 43 | + $priority = $notification->getImportance() ?? Notification::IMPORTANCE_LOW; |
| 44 | + if ($priority === Notification::IMPORTANCE_MEDIUM) { |
| 45 | + $priority = 'default'; |
| 46 | + } |
| 47 | + $ntfyHeaders['Priority'] = $priority; |
| 48 | + |
| 49 | + if ($notification !== null && !empty($notification->getEmoji())) { |
| 50 | + $ntfyHeaders['Tags'] = [$notification->getEmoji()]; |
| 51 | + } |
| 52 | + |
| 53 | + return $ntfyHeaders; |
| 54 | + } |
| 55 | + |
| 56 | + public function setUser(?string $user): self |
| 57 | + { |
| 58 | + $this->user = $user; |
| 59 | + |
| 60 | + return $this; |
| 61 | + } |
| 62 | + |
| 63 | + public function setPassword(?string $password): self |
| 64 | + { |
| 65 | + $this->password = $password; |
| 66 | + |
| 67 | + return $this; |
| 68 | + } |
| 69 | + |
| 70 | + public function setScheme(?string $scheme): self |
| 71 | + { |
| 72 | + $this->scheme = $scheme; |
| 73 | + |
| 74 | + return $this; |
| 75 | + } |
| 76 | + |
| 77 | + public function __toString(): string |
| 78 | + { |
| 79 | + return $this->dsn->getOriginalDsn(); |
| 80 | + } |
| 81 | + |
| 82 | + public function supports(MessageInterface $message): bool |
| 83 | + { |
| 84 | + return $message instanceof PushMessage && |
| 85 | + (null === $message->getOptions() || $message->getOptions() instanceof NtfyOptions); |
| 86 | + } |
| 87 | + |
| 88 | + protected function doSend(MessageInterface $message): SentMessage |
| 89 | + { |
| 90 | + if (!$message instanceof PushMessage) { |
| 91 | + throw new UnsupportedMessageTypeException( |
| 92 | + __CLASS__, |
| 93 | + PushMessage::class, |
| 94 | + $message |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + if (($options = $message->getOptions()) && !$options instanceof NtfyOptions) { |
| 99 | + throw new LogicException( |
| 100 | + sprintf('The "%s" transport only supports instances of "%s" for options.', |
| 101 | + __CLASS__, |
| 102 | + NtfyOptions::class |
| 103 | + ) |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + if (null === $options) { |
| 108 | + $options = new NtfyOptions(); |
| 109 | + } |
| 110 | + |
| 111 | + $endpoint = $this->scheme . '://' . $this->getEndpoint(); |
| 112 | + |
| 113 | + $messageContent = $message->getContent(); |
| 114 | + |
| 115 | + $notificationHeaders = self::getNtfyHeadersFromMessage($message); |
| 116 | + $ntfyMessageOptionHeaders = (array)$options; |
| 117 | + |
| 118 | + if (isset($notificationHeaders['Tags'])) { // handle emoji from Notification |
| 119 | + $tags = $notificationHeaders['Tags']; |
| 120 | + if (isset($ntfyMessageOptionHeaders['Tags'])) { |
| 121 | + $tags .= ',' . $ntfyMessageOptionHeaders['Tags']; |
| 122 | + } |
| 123 | + $ntfyMessageOptionHeaders['Tags'] = $tags; |
| 124 | + unset($notificationHeaders['Tags']); |
| 125 | + } |
| 126 | + |
| 127 | + $messageOptions = [ |
| 128 | + 'headers' => array_merge($notificationHeaders, $ntfyMessageOptionHeaders), |
| 129 | + 'body' => $messageContent, |
| 130 | + ]; |
| 131 | + |
| 132 | + if (!empty($this->user) && !empty($this->password)) { // if DSN is configured to have user/pass, set it here |
| 133 | + $messageOptions['auth_basic'] = [$this->user, $this->password]; |
| 134 | + } |
| 135 | + |
| 136 | + // send off the message |
| 137 | + $response = $this->client->request('POST', $endpoint, $messageOptions); |
| 138 | + |
| 139 | + try { |
| 140 | + $statusCode = $response->getStatusCode(); |
| 141 | + } catch (TransportExceptionInterface $e) { |
| 142 | + throw new TransportException('Could not reach the ntfy server.', $response, 0, $e); |
| 143 | + } |
| 144 | + |
| 145 | + if (200 !== $statusCode) { |
| 146 | + $errorMessage = $response->getContent(false); |
| 147 | + |
| 148 | + throw new TransportException('Unable to post the ntfy message: ' . $errorMessage, $response); |
| 149 | + } |
| 150 | + |
| 151 | + $success = $response->toArray(false); |
| 152 | + |
| 153 | + $sentMessage = new SentMessage($message, (string)$this); |
| 154 | + $sentMessage->setMessageId($success['id']); |
| 155 | + |
| 156 | + return $sentMessage; |
| 157 | + } |
| 158 | + |
| 159 | + protected function getEndpoint(): string |
| 160 | + { |
| 161 | + $baseUri = parent::getEndpoint(); |
| 162 | + |
| 163 | + return $baseUri . $this->topic; |
| 164 | + } |
| 165 | +} |
0 commit comments