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

Skip to content

Commit 0dbfd78

Browse files
committed
Add Web push channel to notifier
1 parent c82567b commit 0dbfd78

File tree

6 files changed

+203
-0
lines changed

6 files changed

+203
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2215,6 +2215,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
22152215
$container->getDefinition('notifier.channel.email')->setArgument(0, null);
22162216
}
22172217
$container->getDefinition('notifier.channel.sms')->setArgument(0, null);
2218+
$container->getDefinition('notifier.channel.web_push')->setArgument(0, null);
22182219
}
22192220

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

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,23 @@
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;
2931
use Symfony\Component\Notifier\Texter;
3032
use Symfony\Component\Notifier\TexterInterface;
3133
use Symfony\Component\Notifier\Transport;
3234
use Symfony\Component\Notifier\Transport\Transports;
35+
use Symfony\Component\Notifier\WebPusher;
36+
use Symfony\Component\Notifier\WebPusherInterface;
3337

3438
return static function (ContainerConfigurator $container) {
3539
$container->services()
@@ -57,6 +61,10 @@
5761
->args([service('mailer.transports'), service('messenger.default_bus')->ignoreOnInvalid()])
5862
->tag('notifier.channel', ['channel' => 'email'])
5963

64+
->set('notifier.channel.web_push', WebPushChannel::class)
65+
->args([service('texter.transports'), service('messenger.default_bus')->ignoreOnInvalid()])
66+
->tag('notifier.channel', ['channel' => 'web_push'])
67+
6068
->set('notifier.monolog_handler', NotifierHandler::class)
6169
->args([service('notifier')])
6270

@@ -103,6 +111,10 @@
103111
->args([service('texter.transports')])
104112
->tag('messenger.message_handler', ['handles' => SmsMessage::class])
105113

114+
->set('texter.messenger.web_push_handler', MessageHandler::class)
115+
->args([service('texter.transports')])
116+
->tag('messenger.message_handler', ['handles' => WebPushMessage::class])
117+
106118
->set('notifier.logger_notification_listener', NotificationLoggerListener::class)
107119
->tag('kernel.event_subscriber')
108120
;

src/Symfony/Component/Notifier/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.3.0
5+
-----
6+
7+
* Add Web push channel to notifier
8+
49
5.2.0
510
-----
611

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