|
| 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 |
10000
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Notifier\Bridge\Ntfy; |
| 13 | + |
| 14 | +use Symfony\Component\Notifier\Exception\LogicException; |
| 15 | +use Symfony\Component\Notifier\Exception\TransportException; |
| 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\Exception\TransportExceptionInterface; |
| 23 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author Mickael Perraud <mikaelkael.fr@gmail.com> |
| 27 | + */ |
| 28 | +final class NtfyTransport extends AbstractTransport |
| 29 | +{ |
| 30 | + protected const HOST = 'ntfy.sh'; |
| 31 | + private ?string $user = null; |
| 32 | + private ?string $password = null; |
| 33 | + |
| 34 | + public function __construct(private string $topic, private bool $secureHttp = true, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null) |
| 35 | + { |
| 36 | + parent::__construct($client, $dispatcher); |
| 37 | + } |
| 38 | + |
| 39 | + public function getTopic(): string |
| 40 | + { |
| 41 | + return $this->topic; |
| 42 | + } |
| 43 | + |
| 44 | + public function setPassword(?string $password): self |
| 45 | + { |
| 46 | + $this->password = $password; |
| 47 | + |
| 48 | + return $this; |
| 49 | + } |
| 50 | + |
| 51 | + public function setUser(?string $user): self |
| 52 | + { |
| 53 | + $this->user = $user; |
| 54 | + |
| 55 | + return $this; |
| 56 | + } |
| 57 | + |
| 58 | + protected function doSend(MessageInterface $message): SentMessage |
| 59 | + { |
| 60 | + if (!$message instanceof PushMessage) { |
| 61 | + throw new UnsupportedMessageTypeException(__CLASS__, PushMessage::class, $message); |
| 62 | + } |
| 63 | + |
| 64 | + if ($message->getOptions() && !$message->getOptions() instanceof NtfyOptions) { |
| 65 | + throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, NtfyOptions::class)); |
| 66 | + } |
| 67 | + |
| 68 | + if (!($opts = $message->getOptions()) && $notification = $message->getNotification()) { |
| 69 | + $opts = NtfyOptions::fromNotification($notification); |
| 70 | + } |
| 71 | + |
| 72 | + $options = $opts ? $opts->toArray() : []; |
| 73 | + |
| 74 | + $options['topic'] = $this->getTopic(); |
| 75 | + |
| 76 | + if (!isset($options['title'])) { |
| 77 | + $options['title'] = $message->getSubject(); |
| 78 | + } |
| 79 | + if (!isset($options['message'])) { |
| 80 | + $options['message'] = $message->getContent(); |
| 81 | + } |
| 82 | + |
| 83 | + $headers = []; |
| 84 | + |
| 85 | + if (null !== $this->user && null !== $this->password) { |
| 86 | + $headers['Authorization'] = 'Basic '.rtrim(base64_encode($this->user.':'.$this->password), '='); |
| 87 | + } |
| 88 | + |
| 89 | + $response = $this->client->request('POST', ($this->secureHttp ? 'https' : 'http').'://'.$this->getEndpoint(), [ |
| 90 | + 'headers' => $headers, |
| 91 | + 'json' => $options, |
| 92 | + ]); |
| 93 | + |
| 94 | + try { |
| 95 | + $statusCode = $response->getStatusCode(); |
| 96 | + } catch (TransportExceptionInterface $e) { |
| 97 | + throw new TransportException('Could not reach the remote Ntfy server.', $response, 0, $e); |
| 98 | + } |
| 99 | + |
| 100 | + if (200 !== $statusCode) { |
| 101 | + throw new TransportException(sprintf('Unable to send the Ntfy push notification: "%s".', $response->getContent(false)), $response); |
| 102 | + } |
| 103 | + |
| 104 | + $result = $response->toArray(false); |
| 105 | + |
| 106 | + if (empty($result['id'])) { |
| 107 | + throw new TransportException(sprintf('Unable to send the Ntfy push notification: "%s".', $response->getContent(false)), $response); |
| 108 | + } |
| 109 | + |
| 110 | + $sentMessage = new SentMessage($message, (string) $this); |
| 111 | + $sentMessage->setMessageId($result['id']); |
| 112 | + |
| 113 | + return $sentMessage; |
| 114 | + } |
| 115 | + |
| 116 | + public function supports(MessageInterface $message): bool |
| 117 | + { |
| 118 | + return $message instanceof PushMessage && |
| 119 | + (null === $message->getOptions() || $message->getOptions() instanceof NtfyOptions); |
| 120 | + } |
| 121 | + |
| 122 | + public function __toString(): string |
| 123 | + { |
| 124 | + return sprintf('ntfy://%s/%s', $this->getEndpoint(), $this->getTopic()); |
| 125 | + } |
| 126 | +} |
0 commit comments