8000 Add Web push channel to notifier · symfony/symfony@523a29e · GitHub
[go: up one dir, main page]

Skip to content

Commit 523a29e

Browse files
committed
Add Web push channel to notifier
1 parent 8e90a80 commit 523a29e

File tree

6 files changed

+193
-0
lines changed

6 files changed

+193
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2219,6 +2219,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
22192219
$container->getDefinition('notifier.channel.email')->setArgument(0, null);
22202220
}
22212221
$container->getDefinition('notifier.channel.sms')->setArgument(0, null);
2222+
$container->getDefinition('notifier.channel.web_push')->setArgument(0, null);
22222223
}
22232224

22242225
$container->getDefinition('notifier.channel_policy')->setArgument(0, $config['channel_policy']);

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
use Symfony\Component\Notifier\Channel\ChatChannel;
1818
use Symfony\Component\Notifier\Channel\EmailChannel;
1919
use Symfony\Component\Notifier\Channel\SmsChannel;
20+
use Symfony\Component\Notifier\Channel\WebPushChannel;
2021
use Symfony\Component\Notifier\Chatter;
2122
use Symfony\Component\Notifier\ChatterInterface;
2223
use Symfony\Component\Notifier\EventListener\NotificationLoggerListener;
2324
use Symfony\Component\Notifier\EventListener\SendFailedMessageToNotifierListener;
2425
use Symfony\Component\Notifier\Message\ChatMessage;
2526
use Symfony\Component\Notifier\Message\SmsMessage;
27+
use Symfony\Component\Notifier\Message\WebPushMessage;
2628
use Symfony\Component\Notifier\Messenger\MessageHandler;
2729
use Symfony\Component\Notifier\Notifier;
2830
use Symfony\Component\Notifier\NotifierInterface;
@@ -57,6 +59,10 @@
5759
->args([service('mailer.transports'), service('messenger.default_bus')->ignoreOnInvalid()])
5860
->tag('notifier.channel', ['channel' => 'email'])
5961

62+
->set('notifier.channel.web_push', WebPushChannel::class)
63+
->args([service('texter.transports'), service('messenger.default_bus')->ignoreOnInvalid()])
64+
->tag('notifier.channel', ['channel' => 'web_push'])
65+
6066
->set('notifier.monolog_handler', NotifierHandler::class)
6167
->args([service('notifier')])
6268

@@ -103,6 +109,10 @@
103109
->args([service('texter.transports')])
104110
->tag('messenger.message_handler', ['handles' => SmsMessage::class])
105111

112+
->set('texter.messenger.web_push_handler', MessageHandler::class)
113+
->args([service('texter.transports')])
114+
->tag('messenger.message_handler', ['handles' => WebPushMessage::class])
115+
106116
->set('notifier.logger_notification_listener', NotificationLoggerListener::class)
107117
->tag('kernel.event_subscriber')
108118
;

src/Symfony/Component/Notifier/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ CHANGELOG
1212
* [BC BREAK] Remove `Dsn::fromString()` method
1313
* [BC BREAK] Changed the return type of `AbstractTransportFactory::getEndpoint()` from `?string` to `string`
1414
* Added `DSN::getRequiredOption` method which throws a new `MissingRequiredOptionException`.
15+
* Add Web push channel to notifier
1516

1617
5.2.0
1718
-----
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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\Channel;
13+
14+
use Symfony\Component\Notifier\Message\WebPushMessage;
15+
use Symfony\Component\Notifier\Notification\Notification;
16+
use Symfony\Component\Notifier\Notification\WebPushNotificationInterface;
17+
use Symfony\Component\Notifier\Recipient\RecipientInterface;
18+
19+
/**
20+
* @author Tomas Norkūnas <norkunas.tom@gmail.com>
21+
*/
22+
class WebPushChannel extends AbstractChannel
23+
{
24+
public function notify(Notification $notification, RecipientInterface $recipient, string $transportName = null): void
25+
{
26+
$message = null;
27+
if ($notification instanceof WebPushNotificationInterface) {
28+
$message = $notification->asWebPushMessage($recipient, $transportName);
29+
}
30+
31+
if (null === $message) {
32+
$message = WebPushMessage::fromNotification($notification);
33+
}
34+
35+
if (null !== $transportName) {
36+
$message->transport($transportName);
37+
}
38+
39+
if (null === $this->bus) {
40+
$this->transport->send($message);
41+
} else {
42+
$this->bus->dispatch($message);
43+
}
44+
}
45+
46+
public function supports(Notification $notification, RecipientInterface $recipient): bool
47+
{
48+
return true;
49+
}
50+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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\Message;
13+
14+
use Symfony\Component\Notifier\Notification\Notification;
15+
16+
/**
17+
* @author Tomas Norkūnas <norkunas.tom@gmail.com>
18+
*/
19+
final class WebPushMessage implements MessageInterface
20+
{
21+
private $transport;
22+
private $subject;
23+
private $content;
24+
private $options;
25+
private $notification;
26+
27+
public function __construct(string $subject, string $content, MessageOptionsInterface $options = null)
28+
{
29+
$this->subject = $subject;
30+
$this->content = $content;
31+
$this->options = $options;
32+
}
33+
34+
public static function fromNotification(Notification $notification): self
35+
{
36+
$message = new self($notification->getSubject(), $notification->getContent());
37+
$message->notification = $notification;
38+
39+
return $message;
40+
}
41+
42+
/**
43+
* @return $this
44+
*/
45+
public function subject(string $subject): self
46+
{
47+
$this->subject = $subject;
48+
49+
return $this;
50+
}
51+
52+
public function getSubject(): string
53+
{
54+
return $this->subject;
55+
}
56+
57+
public function content(string $content): self
58+
{
59+
$this->content = $content;
60+
61+
return $this;
62+
}
63+
64+
public function getContent(): string
65+
{
66+
return $this->content;
67+
}
68+
69+
public function getRecipientId(): ?string
70+
{
71+
return $this->options ? $this->options->getRecipientId() : null;
72+
}
73+
74+
/**
75+
* @return $this
76+
*/
77+
public function options(MessageOptionsInterface $options): self
78+
{
79+
$this->options = $options;
80+
81+
return $this;
82+
}
83+
84+
public function getOptions(): ?MessageOptionsInterface
85+
{
86+
return $this->options;
87+
}
88+
89+
/**
90+
* @return $this
91+
*/
92+
public function transport(?string $transport): self
93+
{
94+
$this->transport = $transport;
95+
96+
return $this;
97+
}
98+
99+
public function getTransport(): ?string
100+
{
101+
return $this->transport;
102+
}
103+
104+
public function getNotification(): ?Notification
105+
{
106+
return $this->notification;
107+
}
108+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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\Notification;
13+
14+
use Symfony\Component\Notifier\Message\WebPushMessage;
15+
use Symfony\Component\Notifier\Recipient\RecipientInterface;
16+
17+
/**
18+
* @experimental in 5.3
19+
*/
20+
interface WebPushNotificationInterface
21+
{
22+
public function asWebPushMessage(RecipientInterface $recipient, string $transport = null): ?WebPushMessage;
23+
}

0 commit comments

Comments
 (0)
0