Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4b4c3e6

Browse filesBrowse files
committed
Add push channel to notifier
1 parent 83bdd2e commit 4b4c3e6
Copy full SHA for 4b4c3e6

File tree

9 files changed

+356
-0
lines changed
Filter options

9 files changed

+356
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2422,6 +2422,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
24222422
$container->getDefinition('notifier.channel.email')->setArgument(0, null);
24232423
}
24242424
$container->getDefinition('notifier.channel.sms')->setArgument(0, null);
2425+
$container->getDefinition('notifier.channel.push')->setArgument(0, null);
24252426
}
24262427

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

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/notifier.php
+10Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Notifier/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ CHANGELOG
1616
* [BC BREAK] Remove `Dsn::fromString()` method
1717
* [BC BREAK] Changed the return type of `AbstractTransportFactory::getEndpoint()` from `?string` to `string`
1818
* Added `DSN::getRequiredOption` method which throws a new `MissingRequiredOptionException`.
19+
* Add `push` channel to notifier
1920

2021
5.2.0
2122
-----
+51Lines 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+
}
+118Lines 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+
}
+20Lines 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+
}
+20Lines 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+
}
+25Lines 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+
* (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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.