From 2ebb18177d92e4b6520773ba7cbfd8f3a1601da2 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 10 Feb 2023 11:34:36 +0100 Subject: [PATCH] [Notifier] Introduce FromNotificationInterface --- src/Symfony/Component/Notifier/CHANGELOG.md | 5 +++++ .../Notifier/Message/ChatMessage.php | 2 +- .../Notifier/Message/EmailMessage.php | 13 +++++++++-- .../Message/FromNotificationInterface.php | 22 +++++++++++++++++++ .../Notifier/Message/PushMessage.php | 2 +- .../Component/Notifier/Message/SmsMessage.php | 13 +++++++++-- 6 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 src/Symfony/Component/Notifier/Message/FromNotificationInterface.php diff --git a/src/Symfony/Component/Notifier/CHANGELOG.md b/src/Symfony/Component/Notifier/CHANGELOG.md index 470bc7498a442..99e21a00a5b27 100644 --- a/src/Symfony/Component/Notifier/CHANGELOG.md +++ b/src/Symfony/Component/Notifier/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +6.3 +--- + + * Introduce `FromNotificationInterface` for `MessageInterface` implementations + 6.2 --- diff --git a/src/Symfony/Component/Notifier/Message/ChatMessage.php b/src/Symfony/Component/Notifier/Message/ChatMessage.php index 6a16280d032d7..66fbe6924725f 100644 --- a/src/Symfony/Component/Notifier/Message/ChatMessage.php +++ b/src/Symfony/Component/Notifier/Message/ChatMessage.php @@ -16,7 +16,7 @@ /** * @author Fabien Potencier */ -class ChatMessage implements MessageInterface +class ChatMessage implements MessageInterface, FromNotificationInterface { private ?string $transport = null; private string $subject; diff --git a/src/Symfony/Component/Notifier/Message/EmailMessage.php b/src/Symfony/Component/Notifier/Message/EmailMessage.php index 427885f19320f..62fe4c8f97a44 100644 --- a/src/Symfony/Component/Notifier/Message/EmailMessage.php +++ b/src/Symfony/Component/Notifier/Message/EmailMessage.php @@ -23,10 +23,11 @@ /** * @author Fabien Potencier */ -class EmailMessage implements MessageInterface +class EmailMessage implements MessageInterface, FromNotificationInterface { private RawMessage $message; private ?Envelope $envelope; + private ?Notification $notification = null; public function __construct(RawMessage $message, Envelope $envelope = null) { @@ -59,7 +60,10 @@ public static function fromNotification(Notification $notification, EmailRecipie } } - return new self($email); + $message = new self($email); + $message->notification = $notification; + + return $message; } public function getMessage(): RawMessage @@ -118,4 +122,9 @@ public function getTransport(): ?string { return $this->message instanceof Email ? $this->message->getHeaders()->getHeaderBody('X-Transport') : null; } + + public function getNotification(): ?Notification + { + return $this->notification; + } } diff --git a/src/Symfony/Component/Notifier/Message/FromNotificationInterface.php b/src/Symfony/Component/Notifier/Message/FromNotificationInterface.php new file mode 100644 index 0000000000000..b9ed7543ad50c --- /dev/null +++ b/src/Symfony/Component/Notifier/Message/FromNotificationInterface.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\Message; + +use Symfony\Component\Notifier\Notification\Notification; + +/** + * @author Fabien Potencier + */ +interface FromNotificationInterface +{ + public function getNotification(): ?Notification; +} diff --git a/src/Symfony/Component/Notifier/Message/PushMessage.php b/src/Symfony/Component/Notifier/Message/PushMessage.php index 063e8e9cb6d2f..21f71b58bc266 100644 --- a/src/Symfony/Component/Notifier/Message/PushMessage.php +++ b/src/Symfony/Component/Notifier/Message/PushMessage.php @@ -16,7 +16,7 @@ /** * @author Tomas Norkūnas */ -class PushMessage implements MessageInterface +class PushMessage implements MessageInterface, FromNotificationInterface { private $transport; private $subject; diff --git a/src/Symfony/Component/Notifier/Message/SmsMessage.php b/src/Symfony/Component/Notifier/Message/SmsMessage.php index 7a0ef9ac6d096..a8d774755dfff 100644 --- a/src/Symfony/Component/Notifier/Message/SmsMessage.php +++ b/src/Symfony/Component/Notifier/Message/SmsMessage.php @@ -18,13 +18,14 @@ /** * @author Fabien Potencier */ -class SmsMessage implements MessageInterface +class SmsMessage implements MessageInterface, FromNotificationInterface { private ?string $transport = null; private string $subject; private string $phone; private string $from; private ?MessageOptionsInterface $options; + private ?Notification $notification = null; public function __construct(string $phone, string $subject, string $from = '', MessageOptionsInterface $options = null) { @@ -40,7 +41,10 @@ public function __construct(string $phone, string $subject, string $from = '', M public static function fromNotification(Notification $notification, SmsRecipientInterface $recipient): self { - return new self($recipient->getPhone(), $notification->getSubject()); + $message = new self($recipient->getPhone(), $notification->getSubject()); + $message->notification = $notification; + + return $message; } /** @@ -126,4 +130,9 @@ public function getOptions(): ?MessageOptionsInterface { return $this->options; } + + public function getNotification(): ?Notification + { + return $this->notification; + } }