From 437cad7c2b425226661074341d610a4d824334f4 Mon Sep 17 00:00:00 2001 From: Oskar Stark Date: Mon, 14 Dec 2020 14:28:22 +0100 Subject: [PATCH] [Notifier] Introduce LengthException --- .../Bridge/Discord/DiscordTransport.php | 4 ++-- .../Discord/Tests/DiscordTransportTest.php | 4 ++-- .../Bridge/Slack/Block/SlackHeaderBlock.php | 6 +++--- .../Tests/Block/SlackHeaderBlockTest.php | 6 +++--- .../Notifier/Exception/LengthException.php | 21 +++++++++++++++++++ 5 files changed, 31 insertions(+), 10 deletions(-) create mode 100644 src/Symfony/Component/Notifier/Exception/LengthException.php diff --git a/src/Symfony/Component/Notifier/Bridge/Discord/DiscordTransport.php b/src/Symfony/Component/Notifier/Bridge/Discord/DiscordTransport.php index 70b638db19c4..b8b865b81208 100644 --- a/src/Symfony/Component/Notifier/Bridge/Discord/DiscordTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Discord/DiscordTransport.php @@ -11,7 +11,7 @@ namespace Symfony\Component\Notifier\Bridge\Discord; -use Symfony\Component\Notifier\Exception\LogicException; +use Symfony\Component\Notifier\Exception\LengthException; use Symfony\Component\Notifier\Exception\TransportException; use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException; use Symfony\Component\Notifier\Message\ChatMessage; @@ -67,7 +67,7 @@ protected function doSend(MessageInterface $message): SentMessage $content = $message->getSubject(); if (\strlen($content) > 2000) { - throw new LogicException('The subject length of a Discord message must not exceed 2000 characters.'); + throw new LengthException('The subject length of a Discord message must not exceed 2000 characters.'); } $endpoint = sprintf('https://%s/api/webhooks/%s/%s', $this->getEndpoint(), $this->webhookId, $this->token); diff --git a/src/Symfony/Component/Notifier/Bridge/Discord/Tests/DiscordTransportTest.php b/src/Symfony/Component/Notifier/Bridge/Discord/Tests/DiscordTransportTest.php index 5f034db10bd8..3a64c1bd7687 100644 --- a/src/Symfony/Component/Notifier/Bridge/Discord/Tests/DiscordTransportTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Discord/Tests/DiscordTransportTest.php @@ -14,7 +14,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\HttpClient\MockHttpClient; use Symfony\Component\Notifier\Bridge\Discord\DiscordTransport; -use Symfony\Component\Notifier\Exception\LogicException; +use Symfony\Component\Notifier\Exception\LengthException; use Symfony\Component\Notifier\Exception\TransportException; use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException; use Symfony\Component\Notifier\Message\ChatMessage; @@ -55,7 +55,7 @@ public function testSendChatMessageWithMoreThan2000CharsThrowsLogicException() { $transport = new DiscordTransport('testToken', 'testChannel', $this->createMock(HttpClientInterface::class)); - $this->expectException(LogicException::class); + $this->expectException(LengthException::class); $this->expectExceptionMessage('The subject length of a Discord message must not exceed 2000 characters.'); $transport->send(new ChatMessage(str_repeat('d', 2001))); diff --git a/src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackHeaderBlock.php b/src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackHeaderBlock.php index c958962f1b6a..5a4d4189ca5b 100644 --- a/src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackHeaderBlock.php +++ b/src/Symfony/Component/Notifier/Bridge/Slack/Block/SlackHeaderBlock.php @@ -11,7 +11,7 @@ namespace Symfony\Component\Notifier\Bridge\Slack\Block; -use Symfony\Component\Notifier\Exception\LogicException; +use Symfony\Component\Notifier\Exception\LengthException; /** * @author Tomas Norkūnas @@ -26,7 +26,7 @@ final class SlackHeaderBlock extends AbstractSlackBlock public function __construct(string $text) { if (\strlen($text) > self::TEXT_LIMIT) { - throw new LogicException(sprintf('Maximum length for the text is %d characters.', self::TEXT_LIMIT)); + throw new LengthException(sprintf('Maximum length for the text is %d characters.', self::TEXT_LIMIT)); } $this->options = [ @@ -41,7 +41,7 @@ public function __construct(string $text) public function id(string $id): self { if (\strlen($id) > self::ID_LIMIT) { - throw new LogicException(sprintf('Maximum length for the block id is %d characters.', self::ID_LIMIT)); + throw new LengthException(sprintf('Maximum length for the block id is %d characters.', self::ID_LIMIT)); } $this->options['block_id'] = $id; diff --git a/src/Symfony/Component/Notifier/Bridge/Slack/Tests/Block/SlackHeaderBlockTest.php b/src/Symfony/Component/Notifier/Bridge/Slack/Tests/Block/SlackHeaderBlockTest.php index c899b1822c09..1174fe971249 100644 --- a/src/Symfony/Component/Notifier/Bridge/Slack/Tests/Block/SlackHeaderBlockTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Slack/Tests/Block/SlackHeaderBlockTest.php @@ -13,7 +13,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Notifier\Bridge\Slack\Block\SlackHeaderBlock; -use Symfony\Component\Notifier\Exception\LogicException; +use Symfony\Component\Notifier\Exception\LengthException; final class SlackHeaderBlockTest extends TestCase { @@ -34,7 +34,7 @@ public function testCanBeInstantiated(): void public function testThrowsWhenTextExceedsCharacterLimit(): void { - $this->expectException(LogicException::class); + $this->expectException(LengthException::class); $this->expectExceptionMessage('Maximum length for the text is 150 characters.'); new SlackHeaderBlock(str_repeat('h', 151)); @@ -42,7 +42,7 @@ public function testThrowsWhenTextExceedsCharacterLimit(): void public function testThrowsWhenBlockIdExceedsCharacterLimit(): void { - $this->expectException(LogicException::class); + $this->expectException(LengthException::class); $this->expectExceptionMessage('Maximum length for the block id is 255 characters.'); $header = new SlackHeaderBlock('header'); diff --git a/src/Symfony/Component/Notifier/Exception/LengthException.php b/src/Symfony/Component/Notifier/Exception/LengthException.php new file mode 100644 index 000000000000..fec27f39d8b6 --- /dev/null +++ b/src/Symfony/Component/Notifier/Exception/LengthException.php @@ -0,0 +1,21 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Notifier\Exception; + +/** + * @author Oskar Stark + * + * @experimental in 5.3 + */ +class LengthException extends LogicException +{ +}