minor #38474 [Notifier] Introduce NullMessage and remove transport setter in MessageInterface (jschaedl)
This PR was merged into the 5.x branch.
Discussion
----------
[Notifier] Introduce NullMessage and remove transport setter in MessageInterface
| Q | A
| ------------- | ---
| Branch? | 5.x
| Bug fix? | no
| New feature? | no <!-- please update src/**/CHANGELOG.md files -->
| Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files -->
| Tickets | - <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead -->
| License | MIT
| Doc PR | - <!-- required for new features -->
Follow-up PR of https://github.com/symfony/symfony/pull/36479
Commits
-------
5701e89960
Introduce NullMessage and remove transport setter in MessageInterface
This commit is contained in:
commit
0137609eaf
@ -77,7 +77,7 @@ final class ChatMessage implements MessageInterface
|
|||||||
/**
|
/**
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function transport(?string $transport): MessageInterface
|
public function transport(?string $transport): self
|
||||||
{
|
{
|
||||||
$this->transport = $transport;
|
$this->transport = $transport;
|
||||||
|
|
||||||
|
@ -102,7 +102,7 @@ final class EmailMessage implements MessageInterface
|
|||||||
/**
|
/**
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function transport(string $transport): MessageInterface
|
public function transport(string $transport): self
|
||||||
{
|
{
|
||||||
if (!$this->message instanceof Email) {
|
if (!$this->message instanceof Email) {
|
||||||
throw new LogicException('Cannot set a Transport on a RawMessage instance.');
|
throw new LogicException('Cannot set a Transport on a RawMessage instance.');
|
||||||
|
@ -25,6 +25,4 @@ interface MessageInterface
|
|||||||
public function getOptions(): ?MessageOptionsInterface;
|
public function getOptions(): ?MessageOptionsInterface;
|
||||||
|
|
||||||
public function getTransport(): ?string;
|
public function getTransport(): ?string;
|
||||||
|
|
||||||
public function transport(string $transport): self;
|
|
||||||
}
|
}
|
||||||
|
47
src/Symfony/Component/Notifier/Message/NullMessage.php
Normal file
47
src/Symfony/Component/Notifier/Message/NullMessage.php
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\Notifier\Message;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Jan Schädlich <jan.schaedlich@sensiolabs.de>
|
||||||
|
*
|
||||||
|
* @experimental in 5.2
|
||||||
|
*/
|
||||||
|
final class NullMessage implements MessageInterface
|
||||||
|
{
|
||||||
|
private $decoratedMessage;
|
||||||
|
|
||||||
|
public function __construct(MessageInterface $message)
|
||||||
|
{
|
||||||
|
$this->decoratedMessage = $message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getRecipientId(): ?string
|
||||||
|
{
|
||||||
|
return $this->decoratedMessage->getRecipientId();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getSubject(): string
|
||||||
|
{
|
||||||
|
return $this->decoratedMessage->getSubject();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getOptions(): ?MessageOptionsInterface
|
||||||
|
{
|
||||||
|
return $this->decoratedMessage->getOptions();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getTransport(): ?string
|
||||||
|
{
|
||||||
|
return $this->decoratedMessage->getTransport() ?? 'null';
|
||||||
|
}
|
||||||
|
}
|
@ -83,7 +83,7 @@ final class SmsMessage implements MessageInterface
|
|||||||
/**
|
/**
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function transport(string $transport): MessageInterface
|
public function transport(string $transport): self
|
||||||
{
|
{
|
||||||
$this->transport = $transport;
|
$this->transport = $transport;
|
||||||
|
|
||||||
|
@ -15,6 +15,7 @@ use Symfony\Component\EventDispatcher\Event;
|
|||||||
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
|
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
|
||||||
use Symfony\Component\Notifier\Event\MessageEvent;
|
use Symfony\Component\Notifier\Event\MessageEvent;
|
||||||
use Symfony\Component\Notifier\Message\MessageInterface;
|
use Symfony\Component\Notifier\Message\MessageInterface;
|
||||||
|
use Symfony\Component\Notifier\Message\NullMessage;
|
||||||
use Symfony\Component\Notifier\Message\SentMessage;
|
use Symfony\Component\Notifier\Message\SentMessage;
|
||||||
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
|
||||||
|
|
||||||
@ -34,9 +35,7 @@ class NullTransport implements TransportInterface
|
|||||||
|
|
||||||
public function send(MessageInterface $message): SentMessage
|
public function send(MessageInterface $message): SentMessage
|
||||||
{
|
{
|
||||||
if (null === $message->getTransport()) {
|
$message = new NullMessage($message);
|
||||||
$message->transport((string) $this);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (null !== $this->dispatcher) {
|
if (null !== $this->dispatcher) {
|
||||||
$this->dispatcher->dispatch(new MessageEvent($message));
|
$this->dispatcher->dispatch(new MessageEvent($message));
|
||||||
|
@ -57,8 +57,6 @@ final class Transports implements TransportInterface
|
|||||||
if (!$transport = $message->getTransport()) {
|
if (!$transport = $message->getTransport()) {
|
||||||
foreach ($this->transports as $transportName => $transport) {
|
foreach ($this->transports as $transportName => $transport) {
|
||||||
if ($transport->supports($message)) {
|
if ($transport->supports($message)) {
|
||||||
$message->transport($transportName);
|
|
||||||
|
|
||||||
return $transport->send($message);
|
return $transport->send($message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user