Skip to content

Commit ba66ee5

Browse files
jschaedlfabpot
authored andcommitted
[Notifier] Remove default transport property in Transports class
1 parent a840f51 commit ba66ee5

File tree

3 files changed

+124
-13
lines changed

3 files changed

+124
-13
lines changed

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);

Tests/Transport/TransportsTest.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
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 testSendToTransportDefinedByMessage(): 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 testSendToFirstSupportedTransportIfMessageDoesNotDefineATransport(): void
39+
{
40+
$transports = new Transports([
41+
'one' => $one = $this->createMock(TransportInterface::class),
42+
'two' => $two = $this->createMock(TransportInterface::class),
43+
]);
44+
45+
$message = new ChatMessage('subject');
46+
47+
$one->method('supports')->with($message)->willReturn(false);
48+
$two->method('supports')->with($message)->willReturn(true);
49+
50+
$one->expects($this->never())->method('send');
51+
$two->expects($this->once())->method('send');
52+
53+
$transports->send($message);
54+
}
55+
56+
public function testThrowExceptionIfNoSupportedTransportWasFound(): void
57+
{
58+
$transports = new Transports([
59+
'one' => $one = $this->createMock(TransportInterface::class),
60+
]);
61+
62+
$message = new ChatMessage('subject');
63+
64+
$one->method('supports')->with($message)->willReturn(false);
65+
66+
$this->expectException(LogicException::class);
67+
$this->expectExceptionMessage('None of the available transports support the given message (available transports: "one"');
68+
69+
$transports->send($message);
70+
}
71+
72+
public function testThrowExceptionIfTransportDefinedByMessageIsNotSupported(): void
73+
{
74+
$transports = new Transports([
75+
'one' => $one = $this->createMock(TransportInterface::class),
76+
'two' => $two = $this->createMock(TransportInterface::class),
77+
]);
78+
79+
$message = new ChatMessage('subject');
80+
$message->transport('one');
81+
82+
$one->method('supports')->with($message)->willReturn(false);
83+
$two->method('supports')->with($message)->willReturn(true);
84+
85+
$this->expectException(LogicException::class);
86+
$this->expectExceptionMessage('The "one" transport does not support the given message.');
87+
88+
$transports->send($message);
89+
}
90+
91+
public function testThrowExceptionIfTransportDefinedByMessageDoesNotExist()
92+
{
93+
$transports = new Transports([
94+
'one' => $one = $this->createMock(TransportInterface::class),
95+
]);
96+
97+
$message = new ChatMessage('subject');
98+
$message->transport('two');
99+
100+
$one->method('supports')->with($message)->willReturn(false);
101+
102+
$this->expectException(InvalidArgumentException::class);
103+
$this->expectExceptionMessage('The "two" transport does not exist (available transports: "one").');
104+
105+
$transports->send($message);
106+
}
107+
}

Transport/Transports.php

Lines changed: 14 additions & 7 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,23 @@ 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+
foreach ($this->transports as $transport) {
58+
if ($transport->supports($message)) {
59+
$transport->send($message);
60+
61+
return;
62+
}
63+
}
6164

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

6568
if (!isset($this->transports[$transport])) {
66-
throw new InvalidArgumentException(sprintf('The "%s" transport does not exist.', $transport));
69+
throw new InvalidArgumentException(sprintf('The "%s" transport does not exist (available transports: "%s").', $transport, implode('", "', array_keys($this->transports))));
70+
}
71+
72+
if (!$this->transports[$transport]->supports($message)) {
73+
throw new LogicException(sprintf('The "%s" transport does not support the given message.', $transport));
6774
}
6875

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

0 commit comments

Comments
 (0)