Skip to content

Commit 0f07fd5

Browse files
mocodonicolas-grekas
authored andcommitted
[Notifier] Add Pushover bridge
1 parent 383ff0b commit 0f07fd5

File tree

16 files changed

+618
-0
lines changed

16 files changed

+618
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@
166166
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
167167
use Symfony\Component\Notifier\Bridge\PagerDuty\PagerDutyTransportFactory;
168168
use Symfony\Component\Notifier\Bridge\Plivo\PlivoTransportFactory;
169+
use Symfony\Component\Notifier\Bridge\Pushover\PushoverTransportFactory;
169170
use Symfony\Component\Notifier\Bridge\RingCentral\RingCentralTransportFactory;
170171
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
171172
use Symfony\Component\Notifier\Bridge\Sendberry\SendberryTransportFactory;
@@ -2606,6 +2607,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
26062607
OvhCloudTransportFactory::class => 'notifier.transport_factory.ovh-cloud',
26072608
PagerDutyTransportFactory::class => 'notifier.transport_factory.pager-duty',
26082609
PlivoTransportFactory::class => 'notifier.transport_factory.plivo',
2610+
PushoverTransportFactory::class => 'notifier.transport_factory.pushover',
26092611
RingCentralTransportFactory::class => 'notifier.transport_factory.ring-central',
26102612
RocketChatTransportFactory::class => 'notifier.transport_factory.rocket-chat',
26112613
SendberryTransportFactory::class => 'notifier.transport_factory.sendberry',

src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier_transports.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
5151
use Symfony\Component\Notifier\Bridge\PagerDuty\PagerDutyTransportFactory;
5252
use Symfony\Component\Notifier\Bridge\Plivo\PlivoTransportFactory;
53+
use Symfony\Component\Notifier\Bridge\Pushover\PushoverTransportFactory;
5354
use Symfony\Component\Notifier\Bridge\RingCentral\RingCentralTransportFactory;
5455
use Symfony\Component\Notifier\Bridge\RocketChat\RocketChatTransportFactory;
5556
use Symfony\Component\Notifier\Bridge\Sendberry\SendberryTransportFactory;
@@ -331,5 +332,9 @@
331332
->set('notifier.transport_factory.pager-duty', PagerDutyTransportFactory::class)
332333
->parent('notifier.transport_factory.abstract')
333334
->tag('chatter.transport_factory')
335+
336+
->set('notifier.transport_factory.pushover', PushoverTransportFactory::class)
337+
->parent('notifier.transport_factory.abstract')
338+
->tag('texter.transport_factory')
334339
;
335340
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.gitattributes export-ignore
4+
/.gitignore export-ignore
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
vendor/
2+
composer.lock
3+
phpunit.xml
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CHANGELOG
2+
=========
3+
4+
6.3
5+
---
6+
7+
* Add the bridge
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2023-present Fabien Potencier
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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\Pushover;
13+
14+
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
15+
use Symfony\Component\Notifier\Message\MessageOptionsInterface;
16+
use Symfony\Component\Notifier\Notification\Notification;
17+
18+
/**
19+
* @author mocodo <https://github.com/mocodo>
20+
*
21+
* @see https://pushover.net/api
22+
*/
23+
final class PushoverOptions implements MessageOptionsInterface
24+
{
25+
private const PRIORITIES = [-2, -1, 0, 1, 2];
26+
27+
private const SOUNDS = [
28+
'pushover',
29+
'bike',
30+
'bugle',
31+
'cashregister',
32+
'classical',
33+
'cosmic',
34+
'falling',
35+
'gamelan',
36+
'incoming',
37+
'intermission',
38+
'magic',
39+
'mechanical',
40+
'pianobar',
41+
'siren',
42+
'spacealarm',
43+
'tugboat',
44+
'alien',
45+
'climb',
46+
'persistent',
47+
'echo',
48+
'vibrate',
49+
'none',
50+
];
51+
52+
private array $options;
53+
54+
public function __construct(array $options = [])
55+
{
56+
$this->options = $options;
57+
}
58+
59+
public static function fromNotification(Notification $notification): self
60+
{
61+
$options = new self();
62+
$options->title($notification->getSubject());
63+
$priority = match ($notification->getImportance()) {
64+
Notification::IMPORTANCE_URGENT => 2,
65+
Notification::IMPORTANCE_HIGH => 1,
66+
Notification::IMPORTANCE_MEDIUM => 0,
67+
Notification::IMPORTANCE_LOW => -1
68+
};
69+
$options->priority($priority);
70+
71+
return $options;
72+
}
73+
74+
public function toArray(): array
75+
{
76+
$options = $this->options;
77+
unset($options['attachment']);
78+
79+
return $options;
80+
}
81+
82+
public function getRecipientId(): ?string
83+
{
84+
return $this->options['device'] ?? null;
85+
}
86+
87+
/**
88+
* @see https://pushover.net/api#identifiers
89+
*
90+
* @return $this
91+
*/
92+
public function device(string $device): static
93+
{
94+
$this->options['device'] = $device;
95+
96+
return $this;
97+
}
98+
99+
/**
100+
* @see https://pushover.net/api#html
101+
*
102+
* @return $this
103+
*/
104+
public function asHtml(bool $bool): static
105+
{
106+
$this->options['html'] = $bool ? 1 : 0;
107+
108+
return $this;
109+
}
110+
111+
/**
112+
* @see https://pushover.net/api#priority
113+
*
114+
* @return $this
115+
*/
116+
public function priority(int $priority): static
117+
{
118+
if (!\in_array($priority, self::PRIORITIES, true)) {
119+
throw new InvalidArgumentException(sprintf('Pushover notification priority must be one of "%s".', implode(', ', self::PRIORITIES)));
120+
}
121+
122+
$this->options['priority'] = $priority;
123+
124+
return $this;
125+
}
126+
127+
/**
128+
* @see https://pushover.net/api#sounds
129+
*
130+
* @return $this
131+
*/
132+
public function sound(string $sound): static
133+
{
134+
if (!\in_array($sound, self::SOUNDS, true)) {
135+
throw new InvalidArgumentException(sprintf('Pushover notification sound must be one of "%s".', implode(', ', self::SOUNDS)));
136+
}
137+
138+
$this->options['sound'] = $sound;
139+
140+
return $this;
141+
}
142+
143+
/**
144+
* @see https://pushover.net/api#timestamp
145+
*
146+
* @return $this
147+
*/
148+
public function timestamp(int $timestamp): static
149+
{
150+
$this->options['timestamp'] = $timestamp;
151+
152+
return $this;
153+
}
154+
155+
/**
156+
* @return $this
157+
*/
158+
public function title(string $title): static
159+
{
160+
$this->options['title'] = $title;
161+
162+
return $this;
163+
}
164+
165+
/**
166+
* @see https://pushover.net/api#urls
167+
*
168+
* @return $this
169+
*/
170+
public function url(string $url): static
171+
{
172+
$this->options['url'] = $url;
173+
174+
return $this;
175+
}
176+
177+
/**
178+
* @see https://pushover.net/api#urls
179+
*
180+
* @return $this
181+
*/
182+
public function urlTitle(string $urlTitle): static
183+
{
184+
$this->options['url_title'] = $urlTitle;
185+
186+
return $this;
187+
}
188+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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\Pushover;
13+
14+
use Symfony\Component\Notifier\Exception\TransportException;
15+
use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException;
16+
use Symfony\Component\Notifier\Message\MessageInterface;
17+
use Symfony\Component\Notifier\Message\PushMessage;
18+
use Symfony\Component\Notifier\Message\SentMessage;
19+
use Symfony\Component\Notifier\Transport\AbstractTransport;
20+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
21+
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
22+
use Symfony\Contracts\HttpClient\HttpClientInterface;
23+
24+
/**
25+
* @author mocodo <https://github.com/mocodo>
26+
*/
27+
final class PushoverTransport extends AbstractTransport
28+
{
29+
protected const HOST = 'api.pushover.net';
30+
31+
public function __construct(
32+
#[\SensitiveParameter] private readonly string $userKey,
33+
#[\SensitiveParameter] private readonly string $appToken,
34+
HttpClientInterface $client = null,
35+
EventDispatcherInterface $dispatcher = null,
36+
) {
37+
parent::__construct($client, $dispatcher);
38+
}
39+
40+
public function supports(MessageInterface $message): bool
41+
{
42+
return $message instanceof PushMessage;
43+
}
44+
45+
public function __toString(): string
46+
{
47+
return sprintf('pushover://%s', $this->getEndpoint());
48+
}
49+
50+
protected function doSend(MessageInterface $message): SentMessage
51+
{
52+
if (!$message instanceof PushMessage) {
53+
throw new UnsupportedMessageTypeException(__CLASS__, PushMessage::class, $message);
54+
}
55+
56+
$opts = $message->getOptions();
57+
$options = $opts ? $opts->toArray() : [];
58+
$options['message'] = $message->getContent();
59+
$options['title'] = $message->getSubject();
60+
$options['token'] = $this->appToken;
61+
$options['user'] = $this->userKey;
62+
63+
$endpoint = sprintf('https://%s/1/messages.json', self::HOST);
64+
$response = $this->client->request('POST', $endpoint, [
65+
'body' => $options,
66+
]);
67+
68+
try {
69+
$statusCode = $response->getStatusCode();
70+
} catch (TransportExceptionInterface $e) {
71+
throw new TransportException('Could not reach the remote Pushover server.', $response, 0, $e);
72+
}
73+
74+
if (200 !== $statusCode) {
75+
throw new TransportException(sprintf('Unable to send the Pushover push notification: "%s".', $response->getContent(false)), $response);
76+
}
77+
78+
$result = $response->toArray(false);
79+
80+
if (!isset($result['request'])) {
81+
throw new TransportException(sprintf('Unable to send the Pushover push notification: "%s".', $result->getContent(false)), $response);
82+
}
83+
84+
$sentMessage = new SentMessage($message, (string) $this);
85+
$sentMessage->setMessageId($result['request']);
86+
87+
return $sentMessage;
88+
}
89+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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\Pushover;
13+
14+
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
15+
use Symfony\Component\Notifier\Transport\AbstractTransportFactory;
16+
use Symfony\Component\Notifier\Transport\Dsn;
17+
use Symfony\Component\Notifier\Transport\TransportInterface;
18+
19+
/**
20+
* @author mocodo <https://github.com/mocodo>
21+
*/
22+
final class PushoverTransportFactory extends AbstractTransportFactory
23+
{
24+
public function create(Dsn $dsn): TransportInterface
25+
{
26+
if ('pushover' !== $dsn->getScheme()) {
27+
throw new UnsupportedSchemeException($dsn, 'pushover', $this->getSupportedSchemes());
28+
}
29+
30+
$userKey = $dsn->getUser();
31+
$appToken = $dsn->getPassword();
32+
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
33+
$port = $dsn->getPort();
34+
35+
return (new PushoverTransport($userKey, $appToken, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
36+
}
37+
38+
protected function getSupportedSchemes(): array
39+
{
40+
return ['pushover'];
41+
}
42+
}

0 commit comments

Comments
 (0)