-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNtfyTransport.php
132 lines (105 loc) · 4.3 KB
/
NtfyTransport.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Notifier\Bridge\Ntfy;
use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Exception\TransportException;
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\PushMessage;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Component\Notifier\Transport\AbstractTransport;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
/**
* @author Mickael Perraud <[email protected]>
*/
final class NtfyTransport extends AbstractTransport
{
protected const HOST = 'ntfy.sh';
private ?string $user = null;
private ?string $password = null;
public function __construct(
private string $topic,
private bool $secureHttp = true,
?HttpClientInterface $client = null,
?EventDispatcherInterface $dispatcher = null,
) {
parent::__construct($client, $dispatcher);
}
public function getTopic(): string
{
return $this->topic;
}
public function setPassword(#[\SensitiveParameter] ?string $password): self
{
$this->password = $password;
return $this;
}
public function setUser(?string $user): self
{
$this->user = $user;
return $this;
}
protected function doSend(MessageInterface $message): SentMessage
{
if (!$message instanceof PushMessage) {
throw new UnsupportedMessageTypeException(__CLASS__, PushMessage::class, $message);
}
if ($message->getOptions() && !$message->getOptions() instanceof NtfyOptions) {
throw new LogicException(\sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, NtfyOptions::class));
}
if (!($opts = $message->getOptions()) && $notification = $message->getNotification()) {
$opts = NtfyOptions::fromNotification($notification);
}
$options = $opts ? $opts->toArray() : [];
$options['topic'] = $this->getTopic();
if (!isset($options['title'])) {
$options['title'] = $message->getSubject();
}
if (!isset($options['message'])) {
$options['message'] = $message->getContent();
}
$headers = [];
if (null !== $this->user && null !== $this->password) {
$headers['Authorization'] = 'Basic '.rtrim(base64_encode($this->user.':'.$this->password), '=');
} elseif (null !== $this->password) {
$headers['Authorization'] = 'Bearer '.$this->password;
}
$response = $this->client->request('POST', ($this->secureHttp ? 'https' : 'http').'://'.$this->getEndpoint(), [
'headers' => $headers,
'json' => $options,
]);
try {
$statusCode = $response->getStatusCode();
} catch (TransportExceptionInterface $e) {
throw new TransportException('Could not reach the remote Ntfy server.', $response, 0, $e);
}
if (200 !== $statusCode) {
throw new TransportException(\sprintf('Unable to send the Ntfy push notification: "%s".', $response->getContent(false)), $response);
}
$result = $response->toArray(false);
if (empty($result['id'])) {
throw new TransportException(\sprintf('Unable to send the Ntfy push notification: "%s".', $response->getContent(false)), $response);
}
$sentMessage = new SentMessage($message, (string) $this);
$sentMessage->setMessageId($result['id']);
return $sentMessage;
}
public function supports(MessageInterface $message): bool
{
return $message instanceof PushMessage
&& (null === $message->getOptions() || $message->getOptions() instanceof NtfyOptions);
}
public function __toString(): string
{
return \sprintf('ntfy://%s/%s', $this->getEndpoint(), $this->getTopic());
}
}