Skip to content

Commit 6ce677a

Browse files
committed
feature #35773 [Notifier] Change notifier recipient handling (jschaedl)
This PR was squashed before being merged into the 5.2-dev branch. Discussion ---------- [Notifier] Change notifier recipient handling | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #35558 <!-- prefix each issue number with "Fix #", if any --> | License | MIT | Doc PR | tbd. According to @wouterj's reasoning in #35558 I did the following to merge the `AdminRecipient` with the `Recipient` class: - introduced a `EmailRecipientInterface` with `getEmail(): string` methods - introduced a `RecipientInterface` that is extended by `EmailRecipientInterface` and `SmsRecipientInterface` - changed `Recipient` class which now holds an email and a phone number property and implements the `EmailRecipientInterface` and the `SmsRecipientInterface` - changed `NoRecipient` class which now implements the `RecipientInterface` - removed the `AdminRecipient` and fixed all related type-hints - introduced an `EmailRecipient` and `SmsRecipient` - changed the type-hint of the `$recipient` argument in `NotifierInterface::send()`, `Notifier::getChannels()`, `ChannelInterface::notifiy()` and `ChannelInterface::supports()` to `RecipientInterface` - changed the type hint of the `$recipient` argument in the `as*Message()` of the `EmailNotificationInterface` and `SmsNotificationInterface` which now uses the introduced interfaces - changed the type hint of the `$recipient` argument in the static `fromNotification()` factory methods in `EmailMessage` and `SmsMessage` which now uses the introduced interfaces - changed `EmailChannel` to only support recipients which implement the `EmailRecipientInterface` - changed `SmsChannel` only supports recipients which implement the `SmsRecipientInterface` Commits ------- 588607bdd1 [Notifier] Change notifier recipient handling
2 parents 12787a5 + eaac8e8 commit 6ce677a

27 files changed

+328
-109
lines changed

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
-----

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
}

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, Recipient $recipient): bool;
26+
public function supports(Notification $notification, RecipientInterface $recipient): bool;
2727
}

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
}

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;
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
}

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
}

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())

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;

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
}

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
}

0 commit comments

Comments
 (0)