Skip to content

[Notifier] Remove default transport property in Transports class #35834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/Symfony/Component/Notifier/Channel/ChatChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Component\Notifier\Channel;

use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Notification\ChatNotificationInterface;
use Symfony\Component\Notifier\Notification\Notification;
Expand All @@ -26,10 +25,6 @@ class ChatChannel extends AbstractChannel
{
public function notify(Notification $notification, Recipient $recipient, string $transportName = null): void
{
if (null === $transportName) {
throw new LogicException('A Chat notification must have a transport defined.');
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a reason for this restriction. All the other channels don't have it either.


$message = null;
if ($notification instanceof ChatNotificationInterface) {
$message = $notification->asChatMessage($recipient, $transportName);
Expand All @@ -39,7 +34,9 @@ public function notify(Notification $notification, Recipient $recipient, string
$message = ChatMessage::fromNotification($notification);
}

$message->transport($transportName);
if (null !== $transportName) {
$message->transport($transportName);
}

if (null === $this->bus) {
$this->transport->send($message);
Expand Down
107 changes: 107 additions & 0 deletions src/Symfony/Component/Notifier/Tests/Transport/TransportsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?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\Tests\Transport;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Message\ChatMessage;
use Symfony\Component\Notifier\Transport\TransportInterface;
use Symfony\Component\Notifier\Transport\Transports;

class TransportsTest extends TestCase
{
public function testSendToTransportDefinedByMessage(): void
{
$transports = new Transports([
'one' => $one = $this->createMock(TransportInterface::class),
]);

$message = new ChatMessage('subject');

$one->method('supports')->with($message)->willReturn(true);

$one->expects($this->once())->method('send');

$transports->send($message);
}

public function testSendToFirstSupportedTransportIfMessageDoesNotDefineATransport(): void
{
$transports = new Transports([
'one' => $one = $this->createMock(TransportInterface::class),
'two' => $two = $this->createMock(TransportInterface::class),
]);

$message = new ChatMessage('subject');

$one->method('supports')->with($message)->willReturn(false);
$two->method('supports')->with($message)->willReturn(true);

$one->expects($this->never())->method('send');
$two->expects($this->once())->method('send');

$transports->send($message);
}

public function testThrowExceptionIfNoSupportedTransportWasFound(): void
{
$transports = new Transports([
'one' => $one = $this->createMock(TransportInterface::class),
]);

$message = new ChatMessage('subject');

$one->method('supports')->with($message)->willReturn(false);

$this->expectException(LogicException::class);
$this->expectExceptionMessage('None of the available transports support the given message (available transports: "one"');

$transports->send($message);
}

public function testThrowExceptionIfTransportDefinedByMessageIsNotSupported(): void
{
$transports = new Transports([
'one' => $one = $this->createMock(TransportInterface::class),
'two' => $two = $this->createMock(TransportInterface::class),
]);

$message = new ChatMessage('subject');
$message->transport('one');

$one->method('supports')->with($message)->willReturn(false);
$two->method('supports')->with($message)->willReturn(true);

$this->expectException(LogicException::class);
$this->expectExceptionMessage('The "one" transport does not support the given message.');

$transports->send($message);
}

public function testThrowExceptionIfTransportDefinedByMessageDoesNotExist()
{
$transports = new Transports([
'one' => $one = $this->createMock(TransportInterface::class),
]);

$message = new ChatMessage('subject');
$message->transport('two');

$one->method('supports')->with($message)->willReturn(false);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The "two" transport does not exist (available transports: "one").');

$transports->send($message);
}
}
21 changes: 14 additions & 7 deletions src/Symfony/Component/Notifier/Transport/Transports.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Notifier\Transport;

use Symfony\Component\Notifier\Exception\InvalidArgumentException;
use Symfony\Component\Notifier\Exception\LogicException;
use Symfony\Component\Notifier\Message\MessageInterface;

/**
Expand All @@ -22,7 +23,6 @@
final class Transports implements TransportInterface
{
private $transports;
private $default;

/**
* @param TransportInterface[] $transports
Expand All @@ -31,9 +31,6 @@ public function __construct(iterable $transports)
{
$this->transports = [];
foreach ($transports as $name => $transport) {
if (null === $this->default) {
$this->default = $transport;
}
$this->transports[$name] = $transport;
}
}
Expand All @@ -57,13 +54,23 @@ public function supports(MessageInterface $message): bool
public function send(MessageInterface $message): void
{
if (!$transport = $message->getTransport()) {
$this->default->send($message);
foreach ($this->transports as $transport) {
if ($transport->supports($message)) {
$transport->send($message);

return;
}
}

return;
throw new LogicException(sprintf('None of the available transports support the given message (available transports: "%s").', implode('", "', array_keys($this->transports))));
}

if (!isset($this->transports[$transport])) {
throw new InvalidArgumentException(sprintf('The "%s" transport does not exist.', $transport));
throw new InvalidArgumentException(sprintf('The "%s" transport does not exist (available transports: "%s").', $transport, implode('", "', array_keys($this->transports))));
}

if (!$this->transports[$transport]->supports($message)) {
throw new LogicException(sprintf('The "%s" transport does not support the given message.', $transport));
}

$this->transports[$transport]->send($message);
Expand Down