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

Skip to content

Commit adab8bd

Browse files
committed
Add push channel to notifier
1 parent 98ed693 commit adab8bd

File tree

9 files changed

+356
-0
lines changed

9 files changed

+356
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2434,6 +2434,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
24342434
$container->getDefinition('notifier.channel.email')->setArgument(0, null);
24352435
}
24362436
$container->getDefinition('notifier.channel.sms')->setArgument(0, null);
2437+
$container->getDefinition('notifier.channel.push')->setArgument(0, null);
24372438
}
24382439

24392440
$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
@@ -16,12 +16,14 @@
1616
use Symfony\Component\Notifier\Channel\ChannelPolicy;
1717
use Symfony\Component\Notifier\Channel\ChatChannel;
1818
use Symfony\Component\Notifier\Channel\EmailChannel;
19+
use Symfony\Component\Notifier\Channel\PushChannel;
1920
use Symfony\Component\Notifier\Channel\SmsChannel;
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;
26+
use Symfony\Component\Notifier\Message\PushMessage;
2527
use Symfony\Component\Notifier\Message\SmsMessage;
2628
use Symfony\Component\Notifier\Messenger\MessageHandler;
2729
use Symfony\Component\Notifier\Notifier;
@@ -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.push', PushChannel::class)
63+
->args([service('texter.transports'), service('messenger.default_bus')->ignoreOnInvalid()])
64+
->tag('notifier.channel', ['channel' => '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.push_handler', MessageHandler::class)
113+
->args([service('texter.transports')])
114+
->tag('messenger.message_handler', ['handles' => PushMessage::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
@@ -17,6 +17,7 @@ CHANGELOG
1717
* [BC BREAK] Remove `Dsn::fromString()` method
1818
* [BC BREAK] Changed the return type of `AbstractTransportFactory::getEndpoint()` from `?string` to `string`
1919
* Added `DSN::getRequiredOption` method which throws a new `MissingRequiredOptionException`.
20+
* Add `push` channel to notifier
2021

2122
5.2.0
2223
-----
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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\PushMessage;
15+
use Symfony\Component\Notifier\Notification\Notification;
16+
use Symfony\Component\Notifier\Notification\PushNotificationInterface;
17+
use Symfony\Component\Notifier\Recipient\PushRecipientInterface;
18+
use Symfony\Component\Notifier\Recipient\RecipientInterface;
19+
20+
/**
21+
* @author Tomas Norkūnas <norkunas.tom@gmail.com>
22+
*/
23+
class PushChannel extends AbstractChannel
24+
{
25+
public function notify(Notification $notification, RecipientInterface $recipient, string $transportName = null): void
26+
{
27+
$message = null;
28+
if ($notification instanceof PushNotificationInterface) {
29+
$message = $notification->asPushMessage($recipient, $transportName);
30+
}
31+
32+
if (null === $message) {
33+
$message = PushMessage::fromNotification($notification, $recipient);
34+
}
35+
36+
if (null !== $transportName) {
37+
$message->transport($transportName);
38+
}
39+
40+
if (null === $this->bus) {
41+
$this->transport->send($message);
42+
} else {
43+
$this->bus->dispatch($message);
44+
}
45+
}
46+
47+
public function supports(Notification $notification, RecipientInterface $recipient): bool
48+
{
49+
return $recipient instanceof PushRecipientInterface;
50+
}
51+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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\Exception\InvalidArgumentException;
15+
use Symfony\Component\Notifier\Notification\Notification;
16+
use Symfony\Component\Notifier\Recipient\PushRecipientInterface;
17+
18+
/**
19+
* @author Tomas Norkūnas <norkunas.tom@gmail.com>
20+
*/
21+
final class PushMessage implements MessageInterface
22+
{
23+
private $transport;
24+
private $recipientId;
25+
private $subject;
26+
private $content;
27+
private $options;
28+
private $notification;
29+
30+
public function __construct(string $recipientId, string $subject, string $content, MessageOptionsInterface $options = null)
31+
{
32+
if ('' === $recipientId) {
33+
throw new InvalidArgumentException(sprintf('"%s" needs a recipient id, it cannot be empty.', static::class));
34+
}
35+
36+
$this->recipientId = $recipientId;
37+
$this->subject = $subject;
38+
$this->content = $content;
39+
$this->options = $options;
40+
}
41+
42+
public static function fromNotification(Notification $notification, PushRecipientInterface $recipient): self
43+
{
44+
$message = new self($recipient->getPushId(), $notification->getSubject(), $notification->getContent());
45+
$message->notification = $notification;
46+
47+
return $message;
48+
}
49+
50+
public function recipientId(string $recipientId): self
51+
{
52+
if ('' === $recipientId) {
53+
throw new InvalidArgumentException(sprintf('"%s" needs a recipient id, it cannot be empty.', static::class));
54+
}
55+
56+
$this->recipientId = $recipientId;
57+
58+
return $this;
59+
}
60+
61+
public function getRecipientId(): string
62+
{
63+
return $this->recipientId;
64+
}
65+
66+
public function subject(string $subject): self
67+
{
68+
$this->subject = $subject;
69+
70+
return $this;
71+
}
72+
73+
public function getSubject(): string
74+
{
75+
return $this->subject;
76+
}
77+
78+
public function content(string $content): self
79+
{
80+
$this->content = $content;
81+
82+
return $this;
83+
}
84+
85+
public function getContent(): string
86+
{
87+
return $this->content;
88+
}
89+
90+
public function options(MessageOptionsInterface $options): self
91+
{
92+
$this->options = $options;
93+
94+
return $this;
95+
}
96+
97+
public function getOptions(): ?MessageOptionsInterface
98+
{
99+
return $this->options;
100+
}
101+
102+
public function transport(?string $transport): self
103+
{
104+
$this->transport = $transport;
105+
106+
return $this;
107+
}
108+
109+
public function getTransport(): ?string
110+
{
111+
return $this->transport;
112+
}
113+
114+
public function getNotification(): ?Notification
115+
{
116+
return $this->notification;
117+
}
118+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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\PushMessage;
15+
use Symfony\Component\Notifier\Recipient\RecipientInterface;
16+
17+
interface PushNotificationInterface
18+
{
19+
public function asPushMessage(RecipientInterface $recipient, string $transport = null): ?PushMessage;
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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\Recipient;
13+
14+
/**
15+
* @author Tomas Norkūnas <norkunas.tom@gmail.com>
16+
*/
17+
interface PushRecipientInterface extends RecipientInterface
18+
{
19+
public function getPushId(): string;
20+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6 B8D4 +
* (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\Recipient;
13+
14+
/**
15+
* @author Tomas Norkūnas <norkunas.tom@gmail.com>
16+
*/
17+
trait PushRecipientTrait
18+
{
19+
private $pushId;
20+
21+
public function getPushId(): string
22+
{
23+
return $this->pushId;
24+
}
25+
}

0 commit comments

Comments
 (0)
0