8000 [Notifier] Change notifier recipient handling · symfony/symfony@588607b · GitHub
[go: up one dir, main page]

Skip to content

Commit 588607b

Browse files
jschaedlfabpot
authored andcommitted
[Notifier] Change notifier recipient handling
1 parent e1cfbd2 commit 588607b

28 files changed

+330
-111
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory;
107107
use Symfony\Component\Notifier\Bridge\Zulip\ZulipTransportFactory;
108108
use Symfony\Component\Notifier\Notifier;
109-
use Symfony\Component\Notifier\Recipient\AdminRecipient;
109+
use Symfony\Component\Notifier\Recipient\Recipient;
110110
use Symfony\Component\PropertyAccess\PropertyAccessor;
111111
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
112112
use Symfony\Component\PropertyInfo\PropertyDescriptionExtractorInterface;
@@ -2096,7 +2096,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $
20962096
$notifier = $container->getDefinition('notifier');
20972097
foreach ($config['admin_recipients'] as $i => $recipient) {
20982098
$id = 'notifier.admin_recipient.'.$i;
2099-
$container->setDefinition($id, new Definition(AdminRecipient::class, [$recipient['email'], $recipient['phone']]));
2099+
$container->setDefinition($id, new Definition(Recipient::class, [$recipient['email'], $recipient['phone']]));
21002100
$notifier->addMethodCall('addAdminRecipient', [new Reference($id)]);
21012101
}
21022102
}

src/Symfony/Component/Notifier/CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ CHANGELOG
66

77
* [BC BREAK] The `TransportInterface::send()` and `AbstractTransport::doSend()` methods changed to return a `?SentMessage` instance instead of `void`.
88
* Added the Zulip notifier bridge
9+
* The `EmailRecipientInterface` and `RecipientInterface` were introduced.
10+
* Added `email` and and `phone` properties to `Recipient`.
11+
* [BC BREAK] Changed the type-hint of the `$recipient` argument in the `as*Message()`
12+
of the `EmailNotificationInterface` and `SmsNotificationInterface` to `EmailRecipientInterface`
13+
and `SmsRecipientInterface`.
14+
* [BC BREAK] Removed the `AdminRecipient`.
15+
* The `EmailRecipientInterface` and `SmsRecipientInterface` now extend the `RecipientInterface`.
16+
* The `EmailRecipient` and `SmsRecipient` were introduced.
17+
* [BC BREAK] Changed the type-hint of the `$recipient` argument in `NotifierInterface::send()`,
18+
`Notifier::getChannels()`, `ChannelInterface::notifiy()` and `ChannelInterface::supports()` to
19+
`RecipientInterface`.
20+
* Changed `EmailChannel` to only support recipients which implement the `EmailRecipientInterface`.
21+
* Changed `SmsChannel` to only support recipients which implement the `SmsRecipientInterface`.
22+
923

1024
5.1.0
1125
-----

src/Symfony/Component/Notifier/Channel/BrowserChannel.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use Symfony\Component\HttpFoundation\RequestStack;
1515
use Symfony\Component\Notifier\Notification\Notification;
16-
use Symfony\Component\Notifier\Recipient\Recipient;
16+
use Symfony\Component\Notifier\Recipient\RecipientInterface;
1717

1818
/**
1919
* @author Fabien Potencier <fabien@symfony.com>
@@ -29,7 +29,7 @@ public function __construct(RequestStack $stack)
2929
$this->stack = $stack;
3030
}
3131

32-
public function notify(Notification $notification, Recipient $recipient, string $transportName = null): void
32+
public function notify(Notification $notification, RecipientInterface $recipient, string $transportName = null): void
3333
{
3434
if (null === $request = $this->stack->getCurrentRequest()) {
3535
return;
@@ -42,7 +42,7 @@ public function notify(Notification $notification, Recipient $recipient, string
4242
$request->getSession()->getFlashBag()->add('notification', $message);
4343
}
4444

45-
public function supports(Notification $notification, Recipient $recipient): bool
45+
public function supports(Notification $notification, RecipientInterface $recipient): bool
4646
{
4747
return true;
4848
}

src/Symfony/Component/Notifier/Channel/ChannelInterface.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Component\Notifier\Channel;
1313

1414
use Symfony\Component\Notifier\Notification\Notification;
15-
use Symfony\Component\Notifier\Recipient\Recipient;
15+
use Symfony\Component\Notifier\Recipient\RecipientInterface;
1616

1717
/**
1818
* @author Fabien Potencier <fabien@symfony.com>
@@ -21,7 +21,7 @@
2121
*/
2222
interface ChannelInterface
2323
{
24-
public function notify(Notification $notification, Recipient $recipient, string $transportName = null): void;
24+
public function notify(Notification $notification, RecipientInterface $recipient, string $transportName = null): void;
2525

26-
public function supports(Notification $notification E377 , Recipient $recipient): bool;
26+
public function supports(Notification $notification, RecipientInterface $recipient): bool;
2727
}

src/Symfony/Component/Notifier/Channel/ChatChannel.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Symfony\Component\Notifier\Message\ChatMessage;
1515
use Symfony\Component\Notifier\Notification\ChatNotificationInterface;
1616
use Symfony\Component\Notifier\Notification\Notification;
17-
use Symfony\Component\Notifier\Recipient\Recipient;
17+
use Symfony\Component\Notifier\Recipient\RecipientInterface;
1818

1919
/**
2020
* @author Fabien Potencier <fabien@symfony.com>
@@ -23,7 +23,7 @@
2323
*/
2424
class ChatChannel extends AbstractChannel
2525
{
26-
public function notify(Notification $notification, Recipient $recipient, string $transportName = null): void
26+
public function notify(Notification $notification, RecipientInterface $recipient, string $transportName = null): void
2727
{
2828
$message = null;
2929
if ($notification instanceof ChatNotificationInterface) {
@@ -45,7 +45,7 @@ public function notify(Notification $notification, Recipient $recipient, string
4545
}
4646
}
4747

48-
public function supports(Notification $notification, Recipient $recipient): bool
48+
public function supports(Notification $notification, RecipientInterface $recipient): bool
4949
{
5050
return true;
5151
}

src/Symfony/Component/Notifier/Channel/EmailChannel.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
use Symfony\Component\Notifier\Message\EmailMessage; 2851
2121
use Symfony\Component\Notifier\Notification\EmailNotificationInterface;
2222
use Symfony\Component\Notifier\Notification\Notification;
23-
use Symfony\Component\Notifier\Recipient\Recipient;
23+
use Symfony\Component\Notifier\Recipient\EmailRecipientInterface;
24+
use Symfony\Component\Notifier\Recipient\RecipientInterface;
2425

2526
/**
2627
* @author Fabien Potencier <fabien@symfony.com>
@@ -46,7 +47,7 @@ public function __construct(TransportInterface $transport = null, MessageBusInte
4647
$this->envelope = $envelope;
4748
}
4849

49-
public function notify(Notification $notification, Recipient $recipient, string $transportName = null): void
50+
public function notify(Notification $notification, RecipientInterface $recipient, string $transportName = null): void
5051
{
5152
$message = null;
5253
if ($notification instanceof EmailNotificationInterface) {
@@ -84,8 +85,8 @@ public function notify(Notification $notification, Recipient $recipient, string
8485
}
8586
}
8687

87-
public function supports(Notification $notification, Recipient $recipient): bool
88+
public function supports(Notification $notification, RecipientInterface $recipient): bool
8889
{
89-
return '' !== $recipient->getEmail();
90+
return $recipient instanceof EmailRecipientInterface;
9091
}
9192
}

src/Symfony/Component/Notifier/Channel/SmsChannel.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Symfony\Component\Notifier\Message\SmsMessage;
1515
use Symfony\Component\Notifier\Notification\Notification;
1616
use Symfony\Component\Notifier\Notification\SmsNotificationInterface;
17-
use Symfony\Component\Notifier\Recipient\Recipient;
17+
use Symfony\Component\Notifier\Recipient\RecipientInterface;
1818
use Symfony\Component\Notifier\Recipient\SmsRecipientInterface;
1919

2020
/**
@@ -24,7 +24,7 @@
2424
*/
2525
class SmsChannel extends AbstractChannel
2626
{
27-
public function notify(Notification $notification, Recipient $recipient, string $transportName = null): void
27+
public function notify(Notification $notification, RecipientInterface $recipient, string $transportName = null): void
2828
{
2929
$message = null;
3030
if ($notification instanceof SmsNotificationInterface) {
@@ -46,8 +46,8 @@ public function notify(Notification $notification, Recipient $recipient, string
4646
}
4747
}
4848

49-
public function supports(Notification $notification, Recipient $recipient): bool
49+
public function supports(Notification $notification, RecipientInterface $recipient): bool
5050
{
51-
return $recipient instanceof SmsRecipientInterface && '' !== $recipient->getPhone();
51+
return $recipient instanceof SmsRecipientInterface;
5252
}
5353
}

src/Symfony/Component/Notifier/Message/EmailMessage.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
use Symfony\Component\Mailer\Envelope;
1616
use Symfony\Component\Mime\Email;
1717
use Symfony\Component\Mime\RawMessage;
18+
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
1819
use Symfony\Component\Notifier\Exception\LogicException;
1920
use Symfony\Component\Notifier\Notification\Notification;
20-
use Symfony\Component\Notifier\Recipient\Recipient;
21+
use Symfony\Component\Notifier\Recipient\EmailRecipientInterface;
2122

2223
/**
2324
* @author Fabien Potencier <fabien@symfony.com>
@@ -35,8 +36,12 @@ public function __construct(RawMessage $message, Envelope $envelope = null)
3536
$this->envelope = $envelope;
3637
}
3738

38-
public static function fromNotification(Notification $notification, Recipient $recipient): self
39+
public static function fromNotification(Notification $notification, EmailRecipientInterface $recipient): self
3940
{
41+
if ('' === $recipient->getEmail()) {
42+
throw new InvalidArgumentException(sprintf('"%s" needs an email, it cannot be empty.', static::class));
43+
}
44+
4045
if (!class_exists(NotificationEmail::class)) {
4146
$email = (new Email())
4247
->to($recipient->getEmail())

src/Symfony/Component/Notifier/Message/SmsMessage.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111

1212
namespace Symfony\Component\Notifier\Message;
1313

14-
use Symfony\Component\Notifier\Exception\LogicException;
14+
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
1515
use Symfony\Component\Notifier\Notification\Notification;
16-
use Symfony\Component\Notifier\Notification\SmsNotificationInterface;
17-
use Symfony\Component\Notifier\Recipient\Recipient;
1816
use Symfony\Component\Notifier\Recipient\SmsRecipientInterface;
1917

2018
/**
@@ -30,16 +28,16 @@ final class SmsMessage implements MessageInterface
3028

3129
public function __construct(string $phone, string $subject)
3230
{
31+
if ('' === $phone) {
32+
throw new InvalidArgumentException(sprintf('"%s" needs a phone number, it cannot be empty.', static::class));
33+
}
34+
3335
$this->subject = $subject;
3436
$this->phone = $phone;
3537
}
3638

37-
public static function fromNotification(Notification $notification, Recipient $recipient): self
39+
public static function fromNotification(Notification $notification, SmsRecipientInterface $recipient): self
3840
{
39-
if (!$recipient instanceof SmsRecipientInterface) {
40-
throw new LogicException(sprintf('To send a SMS message, "%s" should implement "%s" or the recipient should implement "%s".', get_debug_type($notification), SmsNotificationInterface::class, SmsRecipientInterface::class));
41-
}
42-
4341
return new self($recipient->getPhone(), $notification->getSubject());
4442
}
4543

@@ -48,6 +46,10 @@ public static function fromNotification(Notification $notification, Recipient $r
4846
*/
4947
public function phone(string $phone): self
5048
{
49+
if ('' === $phone) {
50+
throw new InvalidArgumentException(sprintf('"%s" needs a phone number, it cannot be empty.', static::class));
51+
}
52+
5153
$this->phone = $phone;
5254

5355
return $this;

src/Symfony/Component/Notifier/Notification/ChatNotificationInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Component\Notifier\Notification;
1313

1414
use Symfony\Component\Notifier\Message\ChatMessage;
15-
use Symfony\Component\Notifier\Recipient\Recipient;
15+
use Symfony\Component\Notifier\Recipient\RecipientInterface;
1616

1717
/**
1818
* @author Fabien Potencier <fabien@symfony.com>
@@ -21,5 +21,5 @@
2121
*/
2222
interface ChatNotificationInterface
2323
{
24-
public function asChatMessage(Recipient $recipient, string $transport = null): ?ChatMessage;
24+
public function asChatMessage(RecipientInterface $recipient, string $transport = null): ?ChatMessage;
2525
}

src/Symfony/Component/Notifier/Notification/EmailNotificationInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Component\Notifier\Notification;
1313

1414
use Symfony\Component\Notifier\Message\EmailMessage;
15-
use Symfony\Component\Notifier\Recipient\Recipient;
15+
use Symfony\Component\Notifier\Recipient\EmailRecipientInterface;
1616

1717
/**
1818
* @author Fabien Potencier <fabien@symfony.com>
@@ -21,5 +21,5 @@
2121
*/
2222
interface EmailNotificationInterface
2323
{
24-
public function asEmailMessage(Recipient $recipient, string $transport = null): ?EmailMessage;
24+
public function asEmailMessage(EmailRecipientInterface $recipient, string $transport = null): ?EmailMessage;
2525
}

src/Symfony/Component/Notifier/Notification/Notification.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
use Psr\Log\LogLevel;
1515
use Symfony\Component\ErrorHandler\Exception\FlattenException;
16-
use Symfony\Component\Notifier\Recipient\Recipient;
16+
use Symfony\Component\Notifier\Recipient\RecipientInterface;
1717

1818
/**
1919
* @author Fabien Potencier <fabien@symfony.com>
@@ -158,7 +158,7 @@ public function channels(array $channels): self
158158
return $this;
159159
}
160160

161-
public function getChannels(Recipient $recipient): array
161+
public function getChannels(RecipientInterface $recipient): array
162162
{
163163
return $this->channels;
164164
}

src/Symfony/Component/Notifier/Notification/SmsNotificationInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Component\Notifier\Notification;
1313

1414
use Symfony\Component\Notifier\Message\SmsMessage;
15-
use Symfony\Component\Notifier\Recipient\Recipient;
15+
use Symfony\Component\Notifier\Recipient\SmsRecipientInterface;
1616

1717
/**
1818
* @author Fabien Potencier <fabien@symfony.com>
@@ -21,5 +21,5 @@
2121
*/
2222
interface SmsNotificationInterface
2323
{
24-
public function asSmsMessage(Recipient $recipient, string $transport = null): ?SmsMessage;
24+
public function asSmsMessage(SmsRecipientInterface $recipient, string $transport = null): ?SmsMessage;
2525
}

src/Symfony/Component/Notifier/Notifier.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@
1717
use Symfony\Component\Notifier\Channel\ChannelPolicyInterface;
1818
use Symfony\Component\Notifier\Exception\LogicException;
1919
use Symfony\Component\Notifier\Notification\Notification;
20-
use Symfony\Component\Notifier\Recipient\AdminRecipient;
2120
use Symfony\Component\Notifier\Recipient\NoRecipient;
22-
use Symfony\Component\Notifier\Recipient\Recipient;
21+
use Symfony\Component\Notifier\Recipient\RecipientInterface;
2322

2423
/**
2524
* @author Fabien Potencier <fabien@symfony.com>
@@ -41,7 +40,7 @@ public function __construct($channels, ChannelPolicyInterface $policy = null)
4140
$this->policy = $policy;
4241
}
4342

44-
public function send(Notification $notification, Recipient ...$recipients): void
43+
public function send(Notification $notification, RecipientInterface ...$recipients): void
4544
{
4645
if (!$recipients) {
4746
$recipients = [new NoRecipient()];
@@ -54,20 +53,20 @@ public function send(Notification $notification, Recipient ...$recipients): void
5453
}
5554
}
5655

57-
public function addAdminRecipient(AdminRecipient $recipient): void
56+
public function addAdminRecipient(RecipientInterface $recipient): void
5857
{
5958
$this->adminRecipients[] = $recipient;
6059
}
6160

6261
/**
63-
* @return AdminRecipient[]
62+
* @return RecipientInterface[]
6463
*/
6564
public function getAdminRecipients(): array
6665
{
6766
return $this->adminRecipients;
6867
}
6968

70-
private function getChannels(Notification $notification, Recipient $recipient): iterable
69+
private function getChannels(Notification $notification, RecipientInterface $recipient): iterable
7170
{
7271
$channels = $notification->getChannels($recipient);
7372
if (!$channels) {

src/Symfony/Component/Notifier/NotifierInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
namespace Symfony\Component\Notifier;
1313

1414
use Symfony\Component\Notifier\Notification\Notification;
15-
use Symfony\Component\Notifier\Recipient\Recipient;
15+
use Symfony\Component\Notifier\Recipient\RecipientInterface;
1616

1717
/**
1818
* Interface for the Notifier system.
@@ -23,5 +23,5 @@
2323
*/
2424
interface NotifierInterface
2525
{
26-
public function send(Notification $notification, Recipient ...$recipients): void;
26+
public function send(Notification $notification, RecipientInterface ...$recipients): void;
2727
}

0 commit comments

Comments
 (0)
0