Skip to content

Commit 50f870a

Browse files
committed
feature #42433 [Notifier] Add more explicit error if a SMSChannel doesn't have a Recipient (ismail1432)
This PR was merged into the 5.4 branch. Discussion ---------- [Notifier] Add more explicit error if a SMSChannel doesn't have a Recipient | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | yes | License | MIT Improve error message if a `SmSChannel` doesn't have a Recipient. ATM The snippet bellow output the message: `The "sms" channel is not supported.` because the `SmsChannel::supports` return `false` if you pass an instance of `NoRecipient`. This PR improve the error message with : "The "sms" channel needs a Recipient." ```php <?php $notifier = new Notifier(['sms' => new SmsChannel(new NullTransport())]); $notifier->send(new Notification("Hello World!", ["sms/twilio"])); Commits ------- 45d577f9b8 add more explicit error
2 parents b2f9572 + 68af828 commit 50f870a

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

Notifier.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Notifier\Channel\ChannelInterface;
1616
use Symfony\Component\Notifier\Channel\ChannelPolicy;
1717
use Symfony\Component\Notifier\Channel\ChannelPolicyInterface;
18+
use Symfony\Component\Notifier\Channel\SmsChannel;
1819
use Symfony\Component\Notifier\Exception\LogicException;
1920
use Symfony\Component\Notifier\Notification\Notification;
2021
use Symfony\Component\Notifier\Recipient\NoRecipient;
@@ -89,6 +90,10 @@ private function getChannels(Notification $notification, RecipientInterface $rec
8990
throw new LogicException(sprintf('The "%s" channel does not exist.', $channelName));
9091
}
9192

93+
if ($channel instanceof SmsChannel && $recipient instanceof NoRecipient) {
94+
throw new LogicException(sprintf('The "%s" channel needs a Recipient.', $channelName));
95+
}
96+
9297
if (!$channel->supports($notification, $recipient)) {
9398
throw new LogicException(sprintf('The "%s" channel is not supported.', $channelName));
9499
}

Tests/NotifierTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Symfony\Component\Notifier\Tests;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Symfony\Component\Notifier\Channel\SmsChannel;
7+
use Symfony\Component\Notifier\Exception\LogicException;
8+
use Symfony\Component\Notifier\Notification\Notification;
9+
use Symfony\Component\Notifier\Notifier;
10+
use Symfony\Component\Notifier\Transport\NullTransport;
11+
12+
/**
13+
* @author Smaïne Milianni <smaine.milianni@gmail.com>
14+
*/
15+
final class NotifierTest extends TestCase
16+
{
17+
public function testItThrowAnExplicitErrorIfAnSmsChannelDoesNotHaveRecipient()
18+
{
19+
$this->expectException(LogicException::class);
20+
$this->expectExceptionMessage('The "sms" channel needs a Recipient.');
21+
22+
$notifier = new Notifier(['sms' => new SmsChannel(new NullTransport())]);
23+
$notifier->send(new Notification('Hello World!', ['sms/twilio']));
24+
}
25+
}

0 commit comments

Comments
 (0)