Skip to content

Commit b6a2d24

Browse files
committed
[Notifier] Remove default transport property in Transports class
1 parent b3b368b commit b6a2d24

File tree

3 files changed

+133
-12
lines changed

3 files changed

+133
-12
lines changed

src/Symfony/Component/Notifier/Channel/ChatChannel.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Component\Notifier\Channel;
1313

14-
use Symfony\Component\Notifier\Exception\LogicException;
1514
use Symfony\Component\Notifier\Message\ChatMessage;
1615
use Symfony\Component\Notifier\Notification\ChatNotificationInterface;
1716
use Symfony\Component\Notifier\Notification\Notification;
@@ -26,10 +25,6 @@ class ChatChannel extends AbstractChannel
2625
{
2726
public function notify(Notification $notification, Recipient $recipient, string $transportName = null): void
2827
{
29-
if (null === $transportName) {
30-
throw new LogicException('A Chat notification must have a transport defined.');
31-
}
32-
3328
$message = null;
3429
if ($notification instanceof ChatNotificationInterface) {
3530
$message = $notification->asChatMessage($recipient, $transportName);
@@ -39,7 +34,9 @@ public function notify(Notification $notification, Recipient $recipient, string
3934
$message = ChatMessage::fromNotification($notification);
4035
}
4136

42-
$message->transport($transportName);
37+
if (null !== $transportName) {
38+
$message->transport($transportName);
39+
}
4340

4441
if (null === $this->bus) {
4542
$this->transport->send($message);
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Notifier\Tests\Transport;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
16+
use Symfony\Component\Notifier\Exception\LogicException;
17+
use Symfony\Component\Notifier\Message\ChatMessage;
18+
use Symfony\Component\Notifier\Transport\TransportInterface;
19+
use Symfony\Component\Notifier\Transport\Transports;
20+
21+
class TransportsTest extends TestCase
22+
{
23+
public function test_send_to_transport_defined_by_message(): void
24+
{
25+
$transports = new Transports([
26+
'one' => $one = $this->createMock(TransportInterface::class),
27+
]);
28+
29+
$message = new ChatMessage('subject');
30+
31+
$one->method('supports')->with($message)->willReturn(true);
32+
33+
$one->expects($this->once())->method('send');
34+
35+
$transports->send($message);
36+
}
37+
38+
public function test_send_to_all_supported_transports_if_message_does_not_define_a_transport(): void
39+
{
40+
$transports = new Transports([
41+
'one' => $one = $this->createMock(TransportInterface::class),
42+
'two' => $two = $this->createMock(TransportInterface::class),
43+
'three' => $three = $this->createMock(TransportInterface::class),
44+
'four' => $four = $this->createMock(TransportInterface::class),
45+
]);
46+
47+
$message = new ChatMessage('subject');
48+
49+
$one->method('supports')->with($message)->willReturn(false);
50+
$two->method('supports')->with($message)->willReturn(true);
51+
$three->method('supports')->with($message)->willReturn(false);
52+
$four->method('supports')->with($message)->willReturn(true);
53+
54+
$one->expects($this->never())->method('send');
55+
$two->expects($this->once())->method('send');
56+
$three->expects($this->never())->method('send');
57+
$four->expects($this->once())->method('send');
58+
59+
$transports->send($message);
60+
}
61+
62+
public function test_throw_exception_if_no_supported_transport_was_found(): void
63+
{
64+
$transports = new Transports([
65+
'one' => $one = $this->createMock(TransportInterface::class),
66+
]);
67+
68+
$message = new ChatMessage('subject');
69+
70+
$one->method('supports')->with($message)->willReturn(false);
71+
72+
$this->expectException(LogicException::class);
73+
$this->expectExceptionMessage('None of the available transports support the given message (available transports: "one"');
74+
75+
$transports->send($message);
76+
}
77+
78+
public function test_throw_exception_if_transport_defined_by_message_is_not_supported(): void
79+
{
80+
$transports = new Transports([
81+
'one' => $one = $this->createMock(TransportInterface::class),
82+
'two' => $two = $this->createMock(TransportInterface::class),
83+
]);
84+
85+
$message = new ChatMessage('subject');
86+
$message->transport('one');
87+
88+
$one->method('supports')->with($message)->willReturn(false);
89+
$two->method('supports')->with($message)->willReturn(true);
90+
91+
$this->expectException(LogicException::class);
92+
$this->expectExceptionMessage('The "one" transport does not support the given message.');
93+
94+
$transports->send($message);
95+
}
96+
97+
public function test_throw_exception_if_transport_defined_by_message_does_not_exist()
98+
{
99+
$transports = new Transports([
100+
'one' => $one = $this->createMock(TransportInterface::class),
101+
]);
102+
103+
$message = new ChatMessage('subject');
104+
$message->transport('two');
105+
106+
$one->method('supports')->with($message)->willReturn(false);
107+
108+
$this->expectException(InvalidArgumentException::class);
109+
$this->expectExceptionMessage('The "two" transport does not exist (available transports: "one").');
110+
111+
$transports->send($message);
112+
}
113+
}

src/Symfony/Component/Notifier/Transport/Transports.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Notifier\Transport;
1313

1414
use Symfony\Component\Notifier\Exception\InvalidArgumentException;
15+
use Symfony\Component\Notifier\Exception\LogicException;
1516
use Symfony\Component\Notifier\Message\MessageInterface;
1617

1718
/**
@@ -22,7 +23,6 @@
2223
final class Transports implements TransportInterface
2324
{
2425
private $transports;
25-
private $default;
2626

2727
/**
2828
* @param TransportInterface[] $transports
@@ -31,9 +31,6 @@ public function __construct(iterable $transports)
3131
{
3232
$this->transports = [];
3333
foreach ($transports as $name => $transport) {
34-
if (null === $this->default) {
35-
$this->default = $transport;
36-
}
3734
$this->transports[$name] = $transport;
3835
}
3936
}
@@ -57,13 +54,27 @@ public function supports(MessageInterface $message): bool
5754
public function send(MessageInterface $message): void
5855
{
5956
if (!$transport = $message->getTransport()) {
60-
$this->default->send($message);
57+
$supportedTransportsCount = 0;
58+
foreach ($this->transports as $transport) {
59+
if ($transport->supports($message)) {
60+
$transport->send($message);
61+
++$supportedTransportsCount;
62+
}
63+
}
64+
65+
if (0 === $supportedTransportsCount) {
66+
throw new LogicException(sprintf('None of the available transports support the given message (available transports: "%s").', implode('", "', array_keys($this->transports))));
67+
}
6168

6269
return;
6370
}
6471

6572
if (!isset($this->transports[$transport])) {
66-
throw new InvalidArgumentException(sprintf('The "%s" transport does not exist.', $transport));
73+
throw new InvalidArgumentException(sprintf('The "%s" transport does not exist (available transports: "%s").', $transport, implode('", "', array_keys($this->transports))));
74+
}
75+
76+
if (!$this->transports[$transport]->supports($message)) {
77+
throw new LogicException(sprintf('The "%s" transport does not support the given message.', $transport));
6778
}
6879

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

0 commit comments

Comments
 (0)