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:
Fabien Potencier 2020-10-08 08:41:23 +02:00
commit 0137609eaf
7 changed files with 52 additions and 10 deletions

View File

@ -77,7 +77,7 @@ final class ChatMessage implements MessageInterface
/**
* @return $this
*/
public function transport(?string $transport): MessageInterface
public function transport(?string $transport): self
{
$this->transport = $transport;

View File

@ -102,7 +102,7 @@ final class EmailMessage implements MessageInterface
/**
* @return $this
*/
public function transport(string $transport): MessageInterface
public function transport(string $transport): self
{
if (!$this->message instanceof Email) {
throw new LogicException('Cannot set a Transport on a RawMessage instance.');

View File

@ -25,6 +25,4 @@ interface MessageInterface
public function getOptions(): ?MessageOptionsInterface;
public function getTransport(): ?string;
public function transport(string $transport): self;
}

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

View File

@ -83,7 +83,7 @@ final class SmsMessage implements MessageInterface
/**
* @return $this
*/
public function transport(string $transport): MessageInterface
public function transport(string $transport): self
{
$this->transport = $transport;

View File

@ -15,6 +15,7 @@ use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
use Symfony\Component\Notifier\Event\MessageEvent;
use Symfony\Component\Notifier\Message\MessageInterface;
use Symfony\Component\Notifier\Message\NullMessage;
use Symfony\Component\Notifier\Message\SentMessage;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
@ -34,9 +35,7 @@ class NullTransport implements TransportInterface
public function send(MessageInterface $message): SentMessage
{
if (null === $message->getTransport()) {
$message->transport((string) $this);
}
$message = new NullMessage($message);
if (null !== $this->dispatcher) {
$this->dispatcher->dispatch(new MessageEvent($message));

View File

@ -57,8 +57,6 @@ final class Transports implements TransportInterface
if (!$transport = $message->getTransport()) {
foreach ($this->transports as $transportName => $transport) {
if ($transport->supports($message)) {
$message->transport($transportName);
return $transport->send($message);
}
}