From 588607bdd1011acdb6309d9e268d9f5aabc37f51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Sch=C3=A4dlich?= Date: Tue, 18 Feb 2020 11:49:59 +0100 Subject: [PATCH] [Notifier] Change notifier recipient handling --- .../FrameworkExtension.php | 4 +- src/Symfony/Component/Notifier/CHANGELOG.md | 14 +++++ .../Notifier/Channel/BrowserChannel.php | 6 +- .../Notifier/Channel/ChannelInterface.php | 6 +- .../Notifier/Channel/ChatChannel.php | 6 +- .../Notifier/Channel/EmailChannel.php | 9 +-- .../Component/Notifier/Channel/SmsChannel.php | 8 +-- .../Notifier/Message/EmailMessage.php | 9 ++- .../Component/Notifier/Message/SmsMessage.php | 18 +++--- .../ChatNotificationInterface.php | 4 +- .../EmailNotificationInterface.php | 4 +- .../Notifier/Notification/Notification.php | 4 +- .../Notification/SmsNotificationInterface.php | 4 +- src/Symfony/Component/Notifier/Notifier.php | 11 ++-- .../Component/Notifier/NotifierInterface.php | 4 +- .../Notifier/Recipient/AdminRecipient.php | 44 ------------- .../Recipient/EmailRecipientInterface.php | 22 +++++++ .../Recipient/EmailRecipientTrait.php | 27 ++++++++ .../Notifier/Recipient/NoRecipient.php | 6 +- .../Notifier/Recipient/Recipient.php | 26 ++++++-- .../Notifier/Recipient/RecipientInterface.php | 21 +++++++ .../Recipient/SmsRecipientInterface.php | 10 +-- .../Notifier/Recipient/SmsRecipientTrait.php | 27 ++++++++ .../Tests/Channel/AbstractChannelTest.php | 6 +- .../Tests/Message/EmailMessageTest.php | 24 ++++++++ .../Notifier/Tests/Message/SmsMessageTest.php | 53 ++++++++++++++++ .../Tests/Recipient/RecipientTest.php | 61 +++++++++++++++++++ src/Symfony/Component/Notifier/composer.json | 3 +- 28 files changed, 330 insertions(+), 111 deletions(-) delete mode 100644 src/Symfony/Component/Notifier/Recipient/AdminRecipient.php create mode 100644 src/Symfony/Component/Notifier/Recipient/EmailRecipientInterface.php create mode 100644 src/Symfony/Component/Notifier/Recipient/EmailRecipientTrait.php create mode 100644 src/Symfony/Component/Notifier/Recipient/RecipientInterface.php create mode 100644 src/Symfony/Component/Notifier/Recipient/SmsRecipientTrait.php create mode 100644 src/Symfony/Component/Notifier/Tests/Message/EmailMessageTest.php create mode 100644 src/Symfony/Component/Notifier/Tests/Message/SmsMessageTest.php create mode 100644 src/Symfony/Component/Notifier/Tests/Recipient/RecipientTest.php diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index 6c30dd8ff1a14..920d40faf5b02 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -106,7 +106,7 @@ use Symfony\Component\Notifier\Bridge\Twilio\TwilioTransportFactory; use Symfony\Component\Notifier\Bridge\Zulip\ZulipTransportFactory; use Symfony\Component\Notifier\Notifier; -use Symfony\Component\Notifier\Recipient\AdminRecipient; +use Symfony\Component\Notifier\Recipient\Recipient; use Symfony\Component\PropertyAccess\PropertyAccessor; use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface; use Symfony\Component\PropertyInfo\PropertyDescriptionExtractorInterface; @@ -2096,7 +2096,7 @@ private function registerNotifierConfiguration(array $config, ContainerBuilder $ $notifier = $container->getDefinition('notifier'); foreach ($config['admin_recipients'] as $i => $recipient) { $id = 'notifier.admin_recipient.'.$i; - $container->setDefinition($id, new Definition(AdminRecipient::class, [$recipient['email'], $recipient['phone']])); + $container->setDefinition($id, new Definition(Recipient::class, [$recipient['email'], $recipient['phone']])); $notifier->addMethodCall('addAdminRecipient', [new Reference($id)]); } } diff --git a/src/Symfony/Component/Notifier/CHANGELOG.md b/src/Symfony/Component/Notifier/CHANGELOG.md index 82903cafc809a..441a30d9c3da7 100644 --- a/src/Symfony/Component/Notifier/CHANGELOG.md +++ b/src/Symfony/Component/Notifier/CHANGELOG.md @@ -6,6 +6,20 @@ CHANGELOG * [BC BREAK] The `TransportInterface::send()` and `AbstractTransport::doSend()` methods changed to return a `?SentMessage` instance instead of `void`. * Added the Zulip notifier bridge + * The `EmailRecipientInterface` and `RecipientInterface` were introduced. + * Added `email` and and `phone` properties to `Recipient`. + * [BC BREAK] Changed the type-hint of the `$recipient` argument in the `as*Message()` + of the `EmailNotificationInterface` and `SmsNotificationInterface` to `EmailRecipientInterface` + and `SmsRecipientInterface`. + * [BC BREAK] Removed the `AdminRecipient`. + * The `EmailRecipientInterface` and `SmsRecipientInterface` now extend the `RecipientInterface`. + * The `EmailRecipient` and `SmsRecipient` were introduced. + * [BC BREAK] Changed the type-hint of the `$recipient` argument in `NotifierInterface::send()`, + `Notifier::getChannels()`, `ChannelInterface::notifiy()` and `ChannelInterface::supports()` to + `RecipientInterface`. + * Changed `EmailChannel` to only support recipients which implement the `EmailRecipientInterface`. + * Changed `SmsChannel` to only support recipients which implement the `SmsRecipientInterface`. + 5.1.0 ----- diff --git a/src/Symfony/Component/Notifier/Channel/BrowserChannel.php b/src/Symfony/Component/Notifier/Channel/BrowserChannel.php index b282daff98e01..29e54309f477d 100644 --- a/src/Symfony/Component/Notifier/Channel/BrowserChannel.php +++ b/src/Symfony/Component/Notifier/Channel/BrowserChannel.php @@ -13,7 +13,7 @@ use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\Notifier\Notification\Notification; -use Symfony\Component\Notifier\Recipient\Recipient; +use Symfony\Component\Notifier\Recipient\RecipientInterface; /** * @author Fabien Potencier @@ -29,7 +29,7 @@ public function __construct(RequestStack $stack) $this->stack = $stack; } - public function notify(Notification $notification, Recipient $recipient, string $transportName = null): void + public function notify(Notification $notification, RecipientInterface $recipient, string $transportName = null): void { if (null === $request = $this->stack->getCurrentRequest()) { return; @@ -42,7 +42,7 @@ public function notify(Notification $notification, Recipient $recipient, string $request->getSession()->getFlashBag()->add('notification', $message); } - public function supports(Notification $notification, Recipient $recipient): bool + public function supports(Notification $notification, RecipientInterface $recipient): bool { return true; } diff --git a/src/Symfony/Component/Notifier/Channel/ChannelInterface.php b/src/Symfony/Component/Notifier/Channel/ChannelInterface.php index 8d35ecc1aeb05..f6c045b8628ef 100644 --- a/src/Symfony/Component/Notifier/Channel/ChannelInterface.php +++ b/src/Symfony/Component/Notifier/Channel/ChannelInterface.php @@ -12,7 +12,7 @@ namespace Symfony\Component\Notifier\Channel; use Symfony\Component\Notifier\Notification\Notification; -use Symfony\Component\Notifier\Recipient\Recipient; +use Symfony\Component\Notifier\Recipient\RecipientInterface; /** * @author Fabien Potencier @@ -21,7 +21,7 @@ */ interface ChannelInterface { - public function notify(Notification $notification, Recipient $recipient, string $transportName = null): void; + public function notify(Notification $notification, RecipientInterface $recipient, string $transportName = null): void; - public function supports(Notification $notification, Recipient $recipient): bool; + public function supports(Notification $notification, RecipientInterface $recipient): bool; } diff --git a/src/Symfony/Component/Notifier/Channel/ChatChannel.php b/src/Symfony/Component/Notifier/Channel/ChatChannel.php index dade3e17d7dcc..90b5524a56d79 100644 --- a/src/Symfony/Component/Notifier/Channel/ChatChannel.php +++ b/src/Symfony/Component/Notifier/Channel/ChatChannel.php @@ -14,7 +14,7 @@ use Symfony\Component\Notifier\Message\ChatMessage; use Symfony\Component\Notifier\Notification\ChatNotificationInterface; use Symfony\Component\Notifier\Notification\Notification; -use Symfony\Component\Notifier\Recipient\Recipient; +use Symfony\Component\Notifier\Recipient\RecipientInterface; /** * @author Fabien Potencier @@ -23,7 +23,7 @@ */ class ChatChannel extends AbstractChannel { - public function notify(Notification $notification, Recipient $recipient, string $transportName = null): void + public function notify(Notification $notification, RecipientInterface $recipient, string $transportName = null): void { $message = null; if ($notification instanceof ChatNotificationInterface) { @@ -45,7 +45,7 @@ public function notify(Notification $notification, Recipient $recipient, string } } - public function supports(Notification $notification, Recipient $recipient): bool + public function supports(Notification $notification, RecipientInterface $recipient): bool { return true; } diff --git a/src/Symfony/Component/Notifier/Channel/EmailChannel.php b/src/Symfony/Component/Notifier/Channel/EmailChannel.php index 74d48c1c9570e..e3eba4f4f319e 100644 --- a/src/Symfony/Component/Notifier/Channel/EmailChannel.php +++ b/src/Symfony/Component/Notifier/Channel/EmailChannel.php @@ -20,7 +20,8 @@ use Symfony\Component\Notifier\Message\EmailMessage; use Symfony\Component\Notifier\Notification\EmailNotificationInterface; use Symfony\Component\Notifier\Notification\Notification; -use Symfony\Component\Notifier\Recipient\Recipient; +use Symfony\Component\Notifier\Recipient\EmailRecipientInterface; +use Symfony\Component\Notifier\Recipient\RecipientInterface; /** * @author Fabien Potencier @@ -46,7 +47,7 @@ public function __construct(TransportInterface $transport = null, MessageBusInte $this->envelope = $envelope; } - public function notify(Notification $notification, Recipient $recipient, string $transportName = null): void + public function notify(Notification $notification, RecipientInterface $recipient, string $transportName = null): void { $message = null; if ($notification instanceof EmailNotificationInterface) { @@ -84,8 +85,8 @@ public function notify(Notification $notification, Recipient $recipient, string } } - public function supports(Notification $notification, Recipient $recipient): bool + public function supports(Notification $notification, RecipientInterface $recipient): bool { - return '' !== $recipient->getEmail(); + return $recipient instanceof EmailRecipientInterface; } } diff --git a/src/Symfony/Component/Notifier/Channel/SmsChannel.php b/src/Symfony/Component/Notifier/Channel/SmsChannel.php index d4fffabe1c9bf..c07437e362fb6 100644 --- a/src/Symfony/Component/Notifier/Channel/SmsChannel.php +++ b/src/Symfony/Component/Notifier/Channel/SmsChannel.php @@ -14,7 +14,7 @@ use Symfony\Component\Notifier\Message\SmsMessage; use Symfony\Component\Notifier\Notification\Notification; use Symfony\Component\Notifier\Notification\SmsNotificationInterface; -use Symfony\Component\Notifier\Recipient\Recipient; +use Symfony\Component\Notifier\Recipient\RecipientInterface; use Symfony\Component\Notifier\Recipient\SmsRecipientInterface; /** @@ -24,7 +24,7 @@ */ class SmsChannel extends AbstractChannel { - public function notify(Notification $notification, Recipient $recipient, string $transportName = null): void + public function notify(Notification $notification, RecipientInterface $recipient, string $transportName = null): void { $message = null; if ($notification instanceof SmsNotificationInterface) { @@ -46,8 +46,8 @@ public function notify(Notification $notification, Recipient $recipient, string } } - public function supports(Notification $notification, Recipient $recipient): bool + public function supports(Notification $notification, RecipientInterface $recipient): bool { - return $recipient instanceof SmsRecipientInterface && '' !== $recipient->getPhone(); + return $recipient instanceof SmsRecipientInterface; } } diff --git a/src/Symfony/Component/Notifier/Message/EmailMessage.php b/src/Symfony/Component/Notifier/Message/EmailMessage.php index 5248b47a34f3c..199030e775f98 100644 --- a/src/Symfony/Component/Notifier/Message/EmailMessage.php +++ b/src/Symfony/Component/Notifier/Message/EmailMessage.php @@ -15,9 +15,10 @@ use Symfony\Component\Mailer\Envelope; use Symfony\Component\Mime\Email; use Symfony\Component\Mime\RawMessage; +use Symfony\Component\Notifier\Exception\InvalidArgumentException; use Symfony\Component\Notifier\Exception\LogicException; use Symfony\Component\Notifier\Notification\Notification; -use Symfony\Component\Notifier\Recipient\Recipient; +use Symfony\Component\Notifier\Recipient\EmailRecipientInterface; /** * @author Fabien Potencier @@ -35,8 +36,12 @@ public function __construct(RawMessage $message, Envelope $envelope = null) $this->envelope = $envelope; } - public static function fromNotification(Notification $notification, Recipient $recipient): self + public static function fromNotification(Notification $notification, EmailRecipientInterface $recipient): self { + if ('' === $recipient->getEmail()) { + throw new InvalidArgumentException(sprintf('"%s" needs an email, it cannot be empty.', static::class)); + } + if (!class_exists(NotificationEmail::class)) { $email = (new Email()) ->to($recipient->getEmail()) diff --git a/src/Symfony/Component/Notifier/Message/SmsMessage.php b/src/Symfony/Component/Notifier/Message/SmsMessage.php index d48f38e0cf937..30d374f4283ca 100644 --- a/src/Symfony/Component/Notifier/Message/SmsMessage.php +++ b/src/Symfony/Component/Notifier/Message/SmsMessage.php @@ -11,10 +11,8 @@ namespace Symfony\Component\Notifier\Message; -use Symfony\Component\Notifier\Exception\LogicException; +use Symfony\Component\Notifier\Exception\InvalidArgumentException; use Symfony\Component\Notifier\Notification\Notification; -use Symfony\Component\Notifier\Notification\SmsNotificationInterface; -use Symfony\Component\Notifier\Recipient\Recipient; use Symfony\Component\Notifier\Recipient\SmsRecipientInterface; /** @@ -30,16 +28,16 @@ final class SmsMessage implements MessageInterface public function __construct(string $phone, string $subject) { + if ('' === $phone) { + throw new InvalidArgumentException(sprintf('"%s" needs a phone number, it cannot be empty.', static::class)); + } + $this->subject = $subject; $this->phone = $phone; } - public static function fromNotification(Notification $notification, Recipient $recipient): self + public static function fromNotification(Notification $notification, SmsRecipientInterface $recipient): self { - if (!$recipient instanceof SmsRecipientInterface) { - 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)); - } - return new self($recipient->getPhone(), $notification->getSubject()); } @@ -48,6 +46,10 @@ public static function fromNotification(Notification $notification, Recipient $r */ public function phone(string $phone): self { + if ('' === $phone) { + throw new InvalidArgumentException(sprintf('"%s" needs a phone number, it cannot be empty.', static::class)); + } + $this->phone = $phone; return $this; diff --git a/src/Symfony/Component/Notifier/Notification/ChatNotificationInterface.php b/src/Symfony/Component/Notifier/Notification/ChatNotificationInterface.php index 0f69af7210dc3..c0dad4ed53674 100644 --- a/src/Symfony/Component/Notifier/Notification/ChatNotificationInterface.php +++ b/src/Symfony/Component/Notifier/Notification/ChatNotificationInterface.php @@ -12,7 +12,7 @@ namespace Symfony\Component\Notifier\Notification; use Symfony\Component\Notifier\Message\ChatMessage; -use Symfony\Component\Notifier\Recipient\Recipient; +use Symfony\Component\Notifier\Recipient\RecipientInterface; /** * @author Fabien Potencier @@ -21,5 +21,5 @@ */ interface ChatNotificationInterface { - public function asChatMessage(Recipient $recipient, string $transport = null): ?ChatMessage; + public function asChatMessage(RecipientInterface $recipient, string $transport = null): ?ChatMessage; } diff --git a/src/Symfony/Component/Notifier/Notification/EmailNotificationInterface.php b/src/Symfony/Component/Notifier/Notification/EmailNotificationInterface.php index fb7c6f17b7414..8097ff4ef1de8 100644 --- a/src/Symfony/Component/Notifier/Notification/EmailNotificationInterface.php +++ b/src/Symfony/Component/Notifier/Notification/EmailNotificationInterface.php @@ -12,7 +12,7 @@ namespace Symfony\Component\Notifier\Notification; use Symfony\Component\Notifier\Message\EmailMessage; -use Symfony\Component\Notifier\Recipient\Recipient; +use Symfony\Component\Notifier\Recipient\EmailRecipientInterface; /** * @author Fabien Potencier @@ -21,5 +21,5 @@ */ interface EmailNotificationInterface { - public function asEmailMessage(Recipient $recipient, string $transport = null): ?EmailMessage; + public function asEmailMessage(EmailRecipientInterface $recipient, string $transport = null): ?EmailMessage; } diff --git a/src/Symfony/Component/Notifier/Notification/Notification.php b/src/Symfony/Component/Notifier/Notification/Notification.php index 7ff4403cbf59e..754626bf53006 100644 --- a/src/Symfony/Component/Notifier/Notification/Notification.php +++ b/src/Symfony/Component/Notifier/Notification/Notification.php @@ -13,7 +13,7 @@ use Psr\Log\LogLevel; use Symfony\Component\ErrorHandler\Exception\FlattenException; -use Symfony\Component\Notifier\Recipient\Recipient; +use Symfony\Component\Notifier\Recipient\RecipientInterface; /** * @author Fabien Potencier @@ -158,7 +158,7 @@ public function channels(array $channels): self return $this; } - public function getChannels(Recipient $recipient): array + public function getChannels(RecipientInterface $recipient): array { return $this->channels; } diff --git a/src/Symfony/Component/Notifier/Notification/SmsNotificationInterface.php b/src/Symfony/Component/Notifier/Notification/SmsNotificationInterface.php index b316f773f8924..83ea7d5c7bc38 100644 --- a/src/Symfony/Component/Notifier/Notification/SmsNotificationInterface.php +++ b/src/Symfony/Component/Notifier/Notification/SmsNotificationInterface.php @@ -12,7 +12,7 @@ namespace Symfony\Component\Notifier\Notification; use Symfony\Component\Notifier\Message\SmsMessage; -use Symfony\Component\Notifier\Recipient\Recipient; +use Symfony\Component\Notifier\Recipient\SmsRecipientInterface; /** * @author Fabien Potencier @@ -21,5 +21,5 @@ */ interface SmsNotificationInterface { - public function asSmsMessage(Recipient $recipient, string $transport = null): ?SmsMessage; + public function asSmsMessage(SmsRecipientInterface $recipient, string $transport = null): ?SmsMessage; } diff --git a/src/Symfony/Component/Notifier/Notifier.php b/src/Symfony/Component/Notifier/Notifier.php index 8ae1b9592178b..6ce56d2afa56e 100644 --- a/src/Symfony/Component/Notifier/Notifier.php +++ b/src/Symfony/Component/Notifier/Notifier.php @@ -17,9 +17,8 @@ use Symfony\Component\Notifier\Channel\ChannelPolicyInterface; use Symfony\Component\Notifier\Exception\LogicException; use Symfony\Component\Notifier\Notification\Notification; -use Symfony\Component\Notifier\Recipient\AdminRecipient; use Symfony\Component\Notifier\Recipient\NoRecipient; -use Symfony\Component\Notifier\Recipient\Recipient; +use Symfony\Component\Notifier\Recipient\RecipientInterface; /** * @author Fabien Potencier @@ -41,7 +40,7 @@ public function __construct($channels, ChannelPolicyInterface $policy = null) $this->policy = $policy; } - public function send(Notification $notification, Recipient ...$recipients): void + public function send(Notification $notification, RecipientInterface ...$recipients): void { if (!$recipients) { $recipients = [new NoRecipient()]; @@ -54,20 +53,20 @@ public function send(Notification $notification, Recipient ...$recipients): void } } - public function addAdminRecipient(AdminRecipient $recipient): void + public function addAdminRecipient(RecipientInterface $recipient): void { $this->adminRecipients[] = $recipient; } /** - * @return AdminRecipient[] + * @return RecipientInterface[] */ public function getAdminRecipients(): array { return $this->adminRecipients; } - private function getChannels(Notification $notification, Recipient $recipient): iterable + private function getChannels(Notification $notification, RecipientInterface $recipient): iterable { $channels = $notification->getChannels($recipient); if (!$channels) { diff --git a/src/Symfony/Component/Notifier/NotifierInterface.php b/src/Symfony/Component/Notifier/NotifierInterface.php index 74cf3bbbe23f3..d07bf6feff4cd 100644 --- a/src/Symfony/Component/Notifier/NotifierInterface.php +++ b/src/Symfony/Component/Notifier/NotifierInterface.php @@ -12,7 +12,7 @@ namespace Symfony\Component\Notifier; use Symfony\Component\Notifier\Notification\Notification; -use Symfony\Component\Notifier\Recipient\Recipient; +use Symfony\Component\Notifier\Recipient\RecipientInterface; /** * Interface for the Notifier system. @@ -23,5 +23,5 @@ */ interface NotifierInterface { - public function send(Notification $notification, Recipient ...$recipients): void; + public function send(Notification $notification, RecipientInterface ...$recipients): void; } diff --git a/src/Symfony/Component/Notifier/Recipient/AdminRecipient.php b/src/Symfony/Component/Notifier/Recipient/AdminRecipient.php deleted file mode 100644 index 0974542a42b6e..0000000000000 --- a/src/Symfony/Component/Notifier/Recipient/AdminRecipient.php +++ /dev/null @@ -1,44 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Symfony\Component\Notifier\Recipient; - -/** - * @author Fabien Potencier - * - * @experimental in 5.1 - */ -class AdminRecipient extends Recipient implements SmsRecipientInterface -{ - private $phone; - - public function __construct(string $email = '', string $phone = '') - { - parent::__construct($email); - - $this->phone = $phone; - } - - /** - * @return $this - */ - public function phone(string $phone): SmsRecipientInterface - { - $this->phone = $phone; - - return $this; - } - - public function getPhone(): string - { - return $this->phone; - } -} diff --git a/src/Symfony/Component/Notifier/Recipient/EmailRecipientInterface.php b/src/Symfony/Component/Notifier/Recipient/EmailRecipientInterface.php new file mode 100644 index 0000000000000..5a7c3d4cb89fb --- /dev/null +++ b/src/Symfony/Component/Notifier/Recipient/EmailRecipientInterface.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Notifier\Recipient; + +/** + * @author Jan Schädlich + * + * @experimental in 5.2 + */ +interface EmailRecipientInterface extends RecipientInterface +{ + public function getEmail(): string; +} diff --git a/src/Symfony/Component/Notifier/Recipient/EmailRecipientTrait.php b/src/Symfony/Component/Notifier/Recipient/EmailRecipientTrait.php new file mode 100644 index 0000000000000..73e5bd98e9c9c --- /dev/null +++ b/src/Symfony/Component/Notifier/Recipient/EmailRecipientTrait.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Notifier\Recipient; + +/** + * @author Jan Schädlich + * + * @experimental in 5.2 + */ +trait EmailRecipientTrait +{ + private $email; + + public function getEmail(): string + { + return $this->email; + } +} diff --git a/src/Symfony/Component/Notifier/Recipient/NoRecipient.php b/src/Symfony/Component/Notifier/Recipient/NoRecipient.php index 37da6d379a8a0..dfdcab04dfdc9 100644 --- a/src/Symfony/Component/Notifier/Recipient/NoRecipient.php +++ b/src/Symfony/Component/Notifier/Recipient/NoRecipient.php @@ -16,10 +16,6 @@ * * @experimental in 5.1 */ -class NoRecipient extends Recipient +class NoRecipient implements RecipientInterface { - public function getEmail(): string - { - return ''; - } } diff --git a/src/Symfony/Component/Notifier/Recipient/Recipient.php b/src/Symfony/Component/Notifier/Recipient/Recipient.php index 0720d59b0bae0..23694d9fc28de 100644 --- a/src/Symfony/Component/Notifier/Recipient/Recipient.php +++ b/src/Symfony/Component/Notifier/Recipient/Recipient.php @@ -11,18 +11,27 @@ namespace Symfony\Component\Notifier\Recipient; +use Symfony\Component\Notifier\Exception\InvalidArgumentException; + /** * @author Fabien Potencier + * @author Jan Schädlich * * @experimental in 5.1 */ -class Recipient +class Recipient implements EmailRecipientInterface, SmsRecipientInterface { - private $email; + use EmailRecipientTrait; + use SmsRecipientTrait; - public function __construct(string $email = '') + public function __construct(string $email = '', string $phone = '') { + if ('' === $email && '' === $phone) { + throw new InvalidArgumentException(sprintf('"%s" needs an email or a phone but both cannot be empty.', static::class)); + } + $this->email = $email; + $this->phone = $phone; } /** @@ -35,8 +44,15 @@ public function email(string $email): self return $this; } - public function getEmail(): string + /** + * Sets the phone number (no spaces, international code like in +3312345678). + * + * @return $this + */ + public function phone(string $phone): self { - return $this->email; + $this->phone = $phone; + + return $this; } } diff --git a/src/Symfony/Component/Notifier/Recipient/RecipientInterface.php b/src/Symfony/Component/Notifier/Recipient/RecipientInterface.php new file mode 100644 index 0000000000000..53e4f60a3ef44 --- /dev/null +++ b/src/Symfony/Component/Notifier/Recipient/RecipientInterface.php @@ -0,0 +1,21 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Notifier\Recipient; + +/** + * @author Jan Schädlich + * + * @experimental in 5.2 + */ +interface RecipientInterface +{ +} diff --git a/src/Symfony/Component/Notifier/Recipient/SmsRecipientInterface.php b/src/Symfony/Component/Notifier/Recipient/SmsRecipientInterface.php index 15a7331dc30c6..059288b4e9fe7 100644 --- a/src/Symfony/Component/Notifier/Recipient/SmsRecipientInterface.php +++ b/src/Symfony/Component/Notifier/Recipient/SmsRecipientInterface.php @@ -13,17 +13,11 @@ /** * @author Fabien Potencier + * @author Jan Schädlich * * @experimental in 5.1 */ -interface SmsRecipientInterface +interface SmsRecipientInterface extends RecipientInterface { - /** - * Sets the phone number (no spaces, international code like in +3312345678). - * - * @return $this - */ - public function phone(string $phone): self; - public function getPhone(): string; } diff --git a/src/Symfony/Component/Notifier/Recipient/SmsRecipientTrait.php b/src/Symfony/Component/Notifier/Recipient/SmsRecipientTrait.php new file mode 100644 index 0000000000000..9f1ad1651805f --- /dev/null +++ b/src/Symfony/Component/Notifier/Recipient/SmsRecipientTrait.php @@ -0,0 +1,27 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Notifier\Recipient; + +/** + * @author Jan Schädlich + * + * @experimental in 5.2 + */ +trait SmsRecipientTrait +{ + private $phone; + + public function getPhone(): string + { + return $this->phone; + } +} diff --git a/src/Symfony/Component/Notifier/Tests/Channel/AbstractChannelTest.php b/src/Symfony/Component/Notifier/Tests/Channel/AbstractChannelTest.php index abaf5a16928b5..f704bb0401efd 100644 --- a/src/Symfony/Component/Notifier/Tests/Channel/AbstractChannelTest.php +++ b/src/Symfony/Component/Notifier/Tests/Channel/AbstractChannelTest.php @@ -15,7 +15,7 @@ use Symfony\Component\Notifier\Channel\AbstractChannel; use Symfony\Component\Notifier\Exception\LogicException; use Symfony\Component\Notifier\Notification\Notification; -use Symfony\Component\Notifier\Recipient\Recipient; +use Symfony\Component\Notifier\Recipient\RecipientInterface; /** * @author Jan Schädlich @@ -32,12 +32,12 @@ public function testChannelCannotBeConstructedWithoutTransportAndBus() class DummyChannel extends AbstractChannel { - public function notify(Notification $notification, Recipient $recipient, string $transportName = null): void + public function notify(Notification $notification, RecipientInterface $recipient, string $transportName = null): void { return; } - public function supports(Notification $notification, Recipient $recipient): bool + public function supports(Notification $notification, RecipientInterface $recipient): bool { return false; } diff --git a/src/Symfony/Component/Notifier/Tests/Message/EmailMessageTest.php b/src/Symfony/Component/Notifier/Tests/Message/EmailMessageTest.php new file mode 100644 index 0000000000000..2f63d17947f1b --- /dev/null +++ b/src/Symfony/Component/Notifier/Tests/Message/EmailMessageTest.php @@ -0,0 +1,24 @@ + + */ +class EmailMessageTest extends TestCase +{ + public function testEnsureNonEmptyEmailOnCreationFromNotification() + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('"Symfony\Component\Notifier\Message\EmailMessage" needs an email, it cannot be empty.'); + + EmailMessage::fromNotification(new Notification(), new Recipient('', '+3312345678')); + } +} diff --git a/src/Symfony/Component/Notifier/Tests/Message/SmsMessageTest.php b/src/Symfony/Component/Notifier/Tests/Message/SmsMessageTest.php new file mode 100644 index 0000000000000..bbc7c7b861cb9 --- /dev/null +++ b/src/Symfony/Component/Notifier/Tests/Message/SmsMessageTest.php @@ -0,0 +1,53 @@ + + */ +class SmsMessageTest extends TestCase +{ + public function testCanBeConstructed() + { + $message = new SmsMessage('+3312345678', 'subject'); + + $this->assertSame('subject', $message->getSubject()); + $this->assertSame('+3312345678', $message->getPhone()); + } + + public function testEnsureNonEmptyPhoneOnConstruction() + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('"Symfony\Component\Notifier\Message\SmsMessage" needs a phone number, it cannot be empty.'); + + new SmsMessage('', 'subject'); + } + + public function testSetPhone() + { + $message = new SmsMessage('+3312345678', 'subject'); + + $this->assertSame('+3312345678', $message->getPhone()); + + $message->phone('+4912345678'); + + $this->assertSame('+4912345678', $message->getPhone()); + } + + public function testEnsureNonEmptyPhoneOnSet() + { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage('"Symfony\Component\Notifier\Message\SmsMessage" needs a phone number, it cannot be empty.'); + + $message = new SmsMessage('+3312345678', 'subject'); + + $this->assertSame('+3312345678', $message->getPhone()); + + $message->phone(''); + } +} diff --git a/src/Symfony/Component/Notifier/Tests/Recipient/RecipientTest.php b/src/Symfony/Component/Notifier/Tests/Recipient/RecipientTest.php new file mode 100644 index 0000000000000..f4e93ccd5127f --- /dev/null +++ b/src/Symfony/Component/Notifier/Tests/Recipient/RecipientTest.php @@ -0,0 +1,61 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Notifier\Tests\Recipient; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Notifier\Exception\InvalidArgumentException; +use Symfony\Component\Notifier\Recipient\Recipient; + +/** + * @author Jan Schädlich + */ +class RecipientTest extends TestCase +{ + public function testCannotBeConstructedWithoutEmailAndWithoutPhone() + { + $this->expectException(InvalidArgumentException::class); + + new Recipient('', ''); + } + + /** + * @dataProvider provideValidEmailAndPhone + */ + public function testCanBeConstructed(string $email, string $phone) + { + $recipient = new Recipient($email, $phone); + + $this->assertSame($email, $recipient->getEmail()); + $this->assertSame($phone, $recipient->getPhone()); + } + + public function provideValidEmailAndPhone() + { + yield ['test@test.de', '+0815']; + yield ['test@test.de', '']; + yield ['', '+0815']; + } + + public function testEmailAndPhoneAreNotImmutable() + { + $recipient = new Recipient('test@test.de', '+0815'); + + $this->assertSame('test@test.de', $recipient->getEmail()); + $this->assertSame('+0815', $recipient->getPhone()); + + $recipient->email('test@test.com'); + $recipient->phone('+49815'); + + $this->assertSame('test@test.com', $recipient->getEmail()); + $this->assertSame('+49815', $recipient->getPhone()); + } +} diff --git a/src/Symfony/Component/Notifier/composer.json b/src/Symfony/Component/Notifier/composer.json index 4c982dc1d8e15..612baaaf73224 100644 --- a/src/Symfony/Component/Notifier/composer.json +++ b/src/Symfony/Component/Notifier/composer.json @@ -17,7 +17,8 @@ ], "require": { "php": ">=7.2.5", - "symfony/polyfill-php80": "^1.15" + "symfony/polyfill-php80": "^1.15", + "psr/log": "~1.0" }, "conflict": { "symfony/http-kernel": "<4.4", pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy