diff --git a/src/Symfony/Component/Notifier/Bridge/Discord/DiscordTransportFactory.php b/src/Symfony/Component/Notifier/Bridge/Discord/DiscordTransportFactory.php index 776e1d7bc7db7..9cdefdd4b3c8f 100644 --- a/src/Symfony/Component/Notifier/Bridge/Discord/DiscordTransportFactory.php +++ b/src/Symfony/Component/Notifier/Bridge/Discord/DiscordTransportFactory.php @@ -11,7 +11,6 @@ namespace Symfony\Component\Notifier\Bridge\Discord; -use Symfony\Component\Notifier\Exception\IncompleteDsnException; use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; use Symfony\Component\Notifier\Transport\AbstractTransportFactory; use Symfony\Component\Notifier\Transport\Dsn; @@ -34,12 +33,7 @@ public function create(Dsn $dsn): TransportInterface } $token = $this->getUser($dsn); - $webhookId = $dsn->getOption('webhook_id'); - - if (!$webhookId) { - throw new IncompleteDsnException('Missing webhook_id.', $dsn->getOriginalDsn()); - } - + $webhookId = $dsn->getRequiredOption('webhook_id'); $host = 'default' === $dsn->getHost() ? null : $dsn->getHost(); $port = $dsn->getPort(); diff --git a/src/Symfony/Component/Notifier/Bridge/Discord/Tests/DiscordTransportFactoryTest.php b/src/Symfony/Component/Notifier/Bridge/Discord/Tests/DiscordTransportFactoryTest.php index 40c8f4d4aabd9..cb020e32c9925 100644 --- a/src/Symfony/Component/Notifier/Bridge/Discord/Tests/DiscordTransportFactoryTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Discord/Tests/DiscordTransportFactoryTest.php @@ -14,6 +14,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Notifier\Bridge\Discord\DiscordTransportFactory; use Symfony\Component\Notifier\Exception\IncompleteDsnException; +use Symfony\Component\Notifier\Exception\MissingRequiredOptionException; use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; use Symfony\Component\Notifier\Transport\Dsn; @@ -28,11 +29,11 @@ public function testCreateWithDsn() $this->assertSame('discord://host.test?webhook_id=testWebhookId', (string) $transport); } - public function testCreateWithMissingOptionWebhookIdThrowsIncompleteDsnException() + public function testCreateWithMissingOptionWebhookIdThrowsMissingRequiredOptionException() { $factory = $this->createFactory(); - $this->expectException(IncompleteDsnException::class); + $this->expectException(MissingRequiredOptionException::class); $factory->create(Dsn::fromString('discord://token@host')); } diff --git a/src/Symfony/Component/Notifier/Bridge/Esendex/EsendexTransportFactory.php b/src/Symfony/Component/Notifier/Bridge/Esendex/EsendexTransportFactory.php index 62b8b6a559134..57d3a6237e9f9 100644 --- a/src/Symfony/Component/Notifier/Bridge/Esendex/EsendexTransportFactory.php +++ b/src/Symfony/Component/Notifier/Bridge/Esendex/EsendexTransportFactory.php @@ -11,7 +11,6 @@ namespace Symfony\Component\Notifier\Bridge\Esendex; -use Symfony\Component\Notifier\Exception\IncompleteDsnException; use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; use Symfony\Component\Notifier\Transport\AbstractTransportFactory; use Symfony\Component\Notifier\Transport\Dsn; @@ -32,18 +31,8 @@ public function create(Dsn $dsn): TransportInterface $email = $this->getUser($dsn); $password = $this->getPassword($dsn); - $accountReference = $dsn->getOption('accountreference'); - - if (!$accountReference) { - throw new IncompleteDsnException('Missing accountreference.', $dsn->getOriginalDsn()); - } - - $from = $dsn->getOption('from'); - - if (!$from) { - throw new IncompleteDsnException('Missing from.', $dsn->getOriginalDsn()); - } - + $accountReference = $dsn->getRequiredOption('accountreference'); + $from = $dsn->getRequiredOption('from'); $host = 'default' === $dsn->getHost() ? null : $dsn->getHost(); $port = $dsn->getPort(); diff --git a/src/Symfony/Component/Notifier/Bridge/Esendex/Tests/EsendexTransportFactoryTest.php b/src/Symfony/Component/Notifier/Bridge/Esendex/Tests/EsendexTransportFactoryTest.php index 0fe29f74e4656..75b907166a135 100644 --- a/src/Symfony/Component/Notifier/Bridge/Esendex/Tests/EsendexTransportFactoryTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Esendex/Tests/EsendexTransportFactoryTest.php @@ -14,6 +14,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Notifier\Bridge\Esendex\EsendexTransportFactory; use Symfony\Component\Notifier\Exception\IncompleteDsnException; +use Symfony\Component\Notifier\Exception\MissingRequiredOptionException; use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; use Symfony\Component\Notifier\Transport\Dsn; @@ -46,20 +47,20 @@ public function testCreateWithMissingPasswordThrowsIncompleteDsnException() $factory->create(Dsn::fromString('esendex://email:@host?accountreference=testAccountreference&from=FROM')); } - public function testCreateWithMissingOptionAccountreferenceThrowsIncompleteDsnException() + public function testCreateWithMissingOptionAccountreferenceThrowsMissingRequiredOptionException() { $factory = $this->createFactory(); - $this->expectException(IncompleteDsnException::class); + $this->expectException(MissingRequiredOptionException::class); $factory->create(Dsn::fromString('esendex://email:password@host?from=FROM')); } - public function testCreateWithMissingOptionFromThrowsIncompleteDsnException() + public function testCreateWithMissingOptionFromThrowsMissingRequiredOptionException() { $factory = $this->createFactory(); - $this->expectException(IncompleteDsnException::class); + $this->expectException(MissingRequiredOptionException::class); $factory->create(Dsn::fromString('esendex://email:password@host?accountreference=ACCOUNTREFERENCE')); } diff --git a/src/Symfony/Component/Notifier/Bridge/FreeMobile/FreeMobileTransportFactory.php b/src/Symfony/Component/Notifier/Bridge/FreeMobile/FreeMobileTransportFactory.php index 94a53235466da..eda2c888df5cb 100644 --- a/src/Symfony/Component/Notifier/Bridge/FreeMobile/FreeMobileTransportFactory.php +++ b/src/Symfony/Component/Notifier/Bridge/FreeMobile/FreeMobileTransportFactory.php @@ -11,7 +11,6 @@ namespace Symfony\Component\Notifier\Bridge\FreeMobile; -use Symfony\Component\Notifier\Exception\IncompleteDsnException; use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; use Symfony\Component\Notifier\Transport\AbstractTransportFactory; use Symfony\Component\Notifier\Transport\Dsn; @@ -35,11 +34,7 @@ public function create(Dsn $dsn): TransportInterface $login = $this->getUser($dsn); $password = $this->getPassword($dsn); - $phone = $dsn->getOption('phone'); - - if (!$phone) { - throw new IncompleteDsnException('Missing phone.', $dsn->getOriginalDsn()); - } + $phone = $dsn->getRequiredOption('phone'); $host = 'default' === $dsn->getHost() ? null : $dsn->getHost(); $port = $dsn->getPort(); diff --git a/src/Symfony/Component/Notifier/Bridge/FreeMobile/Tests/FreeMobileTransportFactoryTest.php b/src/Symfony/Component/Notifier/Bridge/FreeMobile/Tests/FreeMobileTransportFactoryTest.php index bfc9921591d0e..fa554180160b2 100644 --- a/src/Symfony/Component/Notifier/Bridge/FreeMobile/Tests/FreeMobileTransportFactoryTest.php +++ b/src/Symfony/Component/Notifier/Bridge/FreeMobile/Tests/FreeMobileTransportFactoryTest.php @@ -39,7 +39,7 @@ public function supportsProvider(): iterable yield [false, 'somethingElse://login:pass@default?phone=0611223344']; } - public function incompleteDsnProvider(): iterable + public function missingRequiredOptionProvider(): iterable { yield 'missing option: phone' => ['freemobile://login:pass@default']; } diff --git a/src/Symfony/Component/Notifier/Bridge/Infobip/InfobipTransportFactory.php b/src/Symfony/Component/Notifier/Bridge/Infobip/InfobipTransportFactory.php index 29755598a3b12..b4ed0f9fe4c55 100644 --- a/src/Symfony/Component/Notifier/Bridge/Infobip/InfobipTransportFactory.php +++ b/src/Symfony/Component/Notifier/Bridge/Infobip/InfobipTransportFactory.php @@ -11,7 +11,6 @@ namespace Symfony\Component\Notifier\Bridge\Infobip; -use Symfony\Component\Notifier\Exception\IncompleteDsnException; use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; use Symfony\Component\Notifier\Transport\AbstractTransportFactory; use Symfony\Component\Notifier\Transport\Dsn; @@ -35,14 +34,10 @@ public function create(Dsn $dsn): TransportInterface } $authToken = $this->getUser($dsn); - $from = $dsn->getOption('from'); + $from = $dsn->getRequiredOption('from'); $host = $dsn->getHost(); $port = $dsn->getPort(); - if (!$from) { - throw new IncompleteDsnException('Missing from.', $dsn->getOriginalDsn()); - } - return (new InfobipTransport($authToken, $from, $this->client, $this->dispatcher))->setHost($host)->setPort($port); } diff --git a/src/Symfony/Component/Notifier/Bridge/Infobip/Tests/InfobipTransportFactoryTest.php b/src/Symfony/Component/Notifier/Bridge/Infobip/Tests/InfobipTransportFactoryTest.php index a10c3214f4510..cd7f11aa465cb 100644 --- a/src/Symfony/Component/Notifier/Bridge/Infobip/Tests/InfobipTransportFactoryTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Infobip/Tests/InfobipTransportFactoryTest.php @@ -13,7 +13,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Notifier\Bridge\Infobip\InfobipTransportFactory; -use Symfony\Component\Notifier\Exception\IncompleteDsnException; +use Symfony\Component\Notifier\Exception\MissingRequiredOptionException; use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; use Symfony\Component\Notifier\Transport\Dsn; @@ -28,11 +28,11 @@ public function testCreateWithDsn() $this->assertSame('infobip://host.test?from=0611223344', (string) $transport); } - public function testCreateWithNoFromThrowsIncompleteDsnException() + public function testCreateWithNoFromThrowsMissingRequiredOptionException() { $factory = $this->createFactory(); - $this->expectException(IncompleteDsnException::class); + $this->expectException(MissingRequiredOptionException::class); $factory->create(Dsn::fromString('infobip://authtoken@default')); } diff --git a/src/Symfony/Component/Notifier/Bridge/Iqsms/IqsmsTransportFactory.php b/src/Symfony/Component/Notifier/Bridge/Iqsms/IqsmsTransportFactory.php index 6c607a6a73b42..480416e07b875 100644 --- a/src/Symfony/Component/Notifier/Bridge/Iqsms/IqsmsTransportFactory.php +++ b/src/Symfony/Component/Notifier/Bridge/Iqsms/IqsmsTransportFactory.php @@ -11,7 +11,6 @@ namespace Symfony\Component\Notifier\Bridge\Iqsms; -use Symfony\Component\Notifier\Exception\IncompleteDsnException; use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; use Symfony\Component\Notifier\Transport\AbstractTransportFactory; use Symfony\Component\Notifier\Transport\Dsn; @@ -35,12 +34,7 @@ public function create(Dsn $dsn): TransportInterface $login = $this->getUser($dsn); $password = $this->getPassword($dsn); - $from = $dsn->getOption('from'); - - if (!$from) { - throw new IncompleteDsnException('Missing from.', $dsn->getOriginalDsn()); - } - + $from = $dsn->getRequiredOption('from'); $host = 'default' === $dsn->getHost() ? null : $dsn->getHost(); $port = $dsn->getPort(); diff --git a/src/Symfony/Component/Notifier/Bridge/Mattermost/MattermostTransportFactory.php b/src/Symfony/Component/Notifier/Bridge/Mattermost/MattermostTransportFactory.php index 472f63e0eaa9c..aa68de27dbebd 100644 --- a/src/Symfony/Component/Notifier/Bridge/Mattermost/MattermostTransportFactory.php +++ b/src/Symfony/Component/Notifier/Bridge/Mattermost/MattermostTransportFactory.php @@ -11,7 +11,6 @@ namespace Symfony\Component\Notifier\Bridge\Mattermost; -use Symfony\Component\Notifier\Exception\IncompleteDsnException; use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; use Symfony\Component\Notifier\Transport\AbstractTransportFactory; use Symfony\Component\Notifier\Transport\Dsn; @@ -32,12 +31,7 @@ public function create(Dsn $dsn): TransportInterface $path = $dsn->getPath(); $token = $this->getUser($dsn); - $channel = $dsn->getOption('channel'); - - if (!$channel) { - throw new IncompleteDsnException('Missing channel.', $dsn->getOriginalDsn()); - } - + $channel = $dsn->getRequiredOption('channel'); $host = $dsn->getHost(); $port = $dsn->getPort(); diff --git a/src/Symfony/Component/Notifier/Bridge/Mattermost/Tests/MattermostTransportFactoryTest.php b/src/Symfony/Component/Notifier/Bridge/Mattermost/Tests/MattermostTransportFactoryTest.php index 6de18a24391c7..8474f636b2f82 100644 --- a/src/Symfony/Component/Notifier/Bridge/Mattermost/Tests/MattermostTransportFactoryTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Mattermost/Tests/MattermostTransportFactoryTest.php @@ -59,7 +59,11 @@ public function supportsProvider(): iterable public function incompleteDsnProvider(): iterable { - yield 'missing option: token' => ['mattermost://host.test?channel=testChannel']; + yield 'missing token' => ['mattermost://host.test?channel=testChannel']; + } + + public function missingRequiredOptionProvider(): iterable + { yield 'missing option: channel' => ['mattermost://token@host']; } diff --git a/src/Symfony/Component/Notifier/Bridge/Mobyt/MobytTransportFactory.php b/src/Symfony/Component/Notifier/Bridge/Mobyt/MobytTransportFactory.php index 955fefd63d64e..6bbc4ccb81537 100644 --- a/src/Symfony/Component/Notifier/Bridge/Mobyt/MobytTransportFactory.php +++ b/src/Symfony/Component/Notifier/Bridge/Mobyt/MobytTransportFactory.php @@ -11,7 +11,6 @@ namespace Symfony\Component\Notifier\Bridge\Mobyt; -use Symfony\Component\Notifier\Exception\IncompleteDsnException; use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; use Symfony\Component\Notifier\Transport\AbstractTransportFactory; use Symfony\Component\Notifier\Transport\Dsn; @@ -35,12 +34,7 @@ public function create(Dsn $dsn): TransportInterface $accountSid = $this->getUser($dsn); $authToken = $this->getPassword($dsn); - $from = $dsn->getOption('from'); - - if (!$from) { - throw new IncompleteDsnException('Missing from.', $dsn->getOriginalDsn()); - } - + $from = $dsn->getRequiredOption('from'); $typeQuality = $dsn->getOption('type_quality', MobytOptions::MESSAGE_TYPE_QUALITY_LOW); $host = 'default' === $dsn->getHost() ? null : $dsn->getHost(); $port = $dsn->getPort(); diff --git a/src/Symfony/Component/Notifier/Bridge/Nexmo/NexmoTransportFactory.php b/src/Symfony/Component/Notifier/Bridge/Nexmo/NexmoTransportFactory.php index 3095550d392f0..6c7287b398658 100644 --- a/src/Symfony/Component/Notifier/Bridge/Nexmo/NexmoTransportFactory.php +++ b/src/Symfony/Component/Notifier/Bridge/Nexmo/NexmoTransportFactory.php @@ -11,7 +11,6 @@ namespace Symfony\Component\Notifier\Bridge\Nexmo; -use Symfony\Component\Notifier\Exception\IncompleteDsnException; use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; use Symfony\Component\Notifier\Transport\AbstractTransportFactory; use Symfony\Component\Notifier\Transport\Dsn; @@ -35,12 +34,7 @@ public function create(Dsn $dsn): TransportInterface $apiKey = $this->getUser($dsn); $apiSecret = $this->getPassword($dsn); - $from = $dsn->getOption('from'); - - if (!$from) { - throw new IncompleteDsnException('Missing from.', $dsn->getOriginalDsn()); - } - + $from = $dsn->getRequiredOption('from'); $host = 'default' === $dsn->getHost() ? null : $dsn->getHost(); $port = $dsn->getPort(); diff --git a/src/Symfony/Component/Notifier/Bridge/Nexmo/Tests/NexmoTransportFactoryTest.php b/src/Symfony/Component/Notifier/Bridge/Nexmo/Tests/NexmoTransportFactoryTest.php index 7daeb275367ae..a8753cd5affcd 100644 --- a/src/Symfony/Component/Notifier/Bridge/Nexmo/Tests/NexmoTransportFactoryTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Nexmo/Tests/NexmoTransportFactoryTest.php @@ -39,7 +39,7 @@ public function supportsProvider(): iterable yield [false, 'somethingElse://apiKey:apiSecret@default?from=0611223344']; } - public function incompleteDsnProvider(): iterable + public function missingRequiredOptionProvider(): iterable { yield 'missing option: from' => ['nexmo://apiKey:apiSecret@default']; } diff --git a/src/Symfony/Component/Notifier/Bridge/OvhCloud/OvhCloudTransportFactory.php b/src/Symfony/Component/Notifier/Bridge/OvhCloud/OvhCloudTransportFactory.php index 6fa9e1626e336..eacf9efcad14e 100644 --- a/src/Symfony/Component/Notifier/Bridge/OvhCloud/OvhCloudTransportFactory.php +++ b/src/Symfony/Component/Notifier/Bridge/OvhCloud/OvhCloudTransportFactory.php @@ -11,7 +11,6 @@ namespace Symfony\Component\Notifier\Bridge\OvhCloud; -use Symfony\Component\Notifier\Exception\IncompleteDsnException; use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; use Symfony\Component\Notifier\Transport\AbstractTransportFactory; use Symfony\Component\Notifier\Transport\Dsn; @@ -32,18 +31,8 @@ public function create(Dsn $dsn): TransportInterface $applicationKey = $this->getUser($dsn); $applicationSecret = $this->getPassword($dsn); - $consumerKey = $dsn->getOption('consumer_key'); - - if (!$consumerKey) { - throw new IncompleteDsnException('Missing consumer_key.', $dsn->getOriginalDsn()); - } - - $serviceName = $dsn->getOption('service_name'); - - if (!$serviceName) { - throw new IncompleteDsnException('Missing service_name.', $dsn->getOriginalDsn()); - } - + $consumerKey = $dsn->getRequiredOption('consumer_key'); + $serviceName = $dsn->getRequiredOption('service_name'); $host = 'default' === $dsn->getHost() ? null : $dsn->getHost(); $port = $dsn->getPort(); diff --git a/src/Symfony/Component/Notifier/Bridge/OvhCloud/Tests/OvhCloudTransportFactoryTest.php b/src/Symfony/Component/Notifier/Bridge/OvhCloud/Tests/OvhCloudTransportFactoryTest.php index b775defdd83b7..918758f5deb22 100644 --- a/src/Symfony/Component/Notifier/Bridge/OvhCloud/Tests/OvhCloudTransportFactoryTest.php +++ b/src/Symfony/Component/Notifier/Bridge/OvhCloud/Tests/OvhCloudTransportFactoryTest.php @@ -39,7 +39,7 @@ public function supportsProvider(): iterable yield [false, 'somethingElse://key:secret@default?consumer_key=consumerKey&service_name=serviceName']; } - public function incompleteDsnProvider(): iterable + public function missingRequiredOptionProvider(): iterable { yield 'missing option: consumer_key' => ['ovhcloud://key:secret@default?service_name=serviceName']; yield 'missing option: service_name' => ['ovhcloud://key:secret@default?consumer_key=consumerKey']; diff --git a/src/Symfony/Component/Notifier/Bridge/RocketChat/Tests/RocketChatTransportFactoryTest.php b/src/Symfony/Component/Notifier/Bridge/RocketChat/Tests/RocketChatTransportFactoryTest.php index 81fee6e9958a4..2c83d04ff48a2 100644 --- a/src/Symfony/Component/Notifier/Bridge/RocketChat/Tests/RocketChatTransportFactoryTest.php +++ b/src/Symfony/Component/Notifier/Bridge/RocketChat/Tests/RocketChatTransportFactoryTest.php @@ -44,7 +44,7 @@ public function supportsProvider(): iterable public function incompleteDsnProvider(): iterable { - yield 'missing option: token' => ['rocketchat://host.test?channel=testChannel']; + yield 'missing token' => ['rocketchat://host.test?channel=testChannel']; } public function unsupportedSchemeProvider(): iterable diff --git a/src/Symfony/Component/Notifier/Bridge/Sendinblue/SendinblueTransportFactory.php b/src/Symfony/Component/Notifier/Bridge/Sendinblue/SendinblueTransportFactory.php index e36b3f2e3e9cd..bd38a43762bdb 100644 --- a/src/Symfony/Component/Notifier/Bridge/Sendinblue/SendinblueTransportFactory.php +++ b/src/Symfony/Component/Notifier/Bridge/Sendinblue/SendinblueTransportFactory.php @@ -11,7 +11,6 @@ namespace Symfony\Component\Notifier\Bridge\Sendinblue; -use Symfony\Component\Notifier\Exception\IncompleteDsnException; use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; use Symfony\Component\Notifier\Transport\AbstractTransportFactory; use Symfony\Component\Notifier\Transport\Dsn; @@ -34,12 +33,7 @@ public function create(Dsn $dsn): TransportInterface } $apiKey = $this->getUser($dsn); - $sender = $dsn->getOption('sender'); - - if (!$sender) { - throw new IncompleteDsnException('Missing sender.', $dsn->getOriginalDsn()); - } - + $sender = $dsn->getRequiredOption('sender'); $host = 'default' === $dsn->getHost() ? null : $dsn->getHost(); $port = $dsn->getPort(); diff --git a/src/Symfony/Component/Notifier/Bridge/Sendinblue/Tests/SendinblueTransportFactoryTest.php b/src/Symfony/Component/Notifier/Bridge/Sendinblue/Tests/SendinblueTransportFactoryTest.php index a2a1c605ef5ab..6627e3e36c39f 100644 --- a/src/Symfony/Component/Notifier/Bridge/Sendinblue/Tests/SendinblueTransportFactoryTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Sendinblue/Tests/SendinblueTransportFactoryTest.php @@ -14,6 +14,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Notifier\Bridge\Sendinblue\SendinblueTransportFactory; use Symfony\Component\Notifier\Exception\IncompleteDsnException; +use Symfony\Component\Notifier\Exception\MissingRequiredOptionException; use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; use Symfony\Component\Notifier\Transport\Dsn; @@ -28,11 +29,11 @@ public function testCreateWithDsn() $this->assertSame('sendinblue://host.test?sender=0611223344', (string) $transport); } - public function testCreateWithMissingOptionSenderThrowsIncompleteDsnException() + public function testCreateWithMissingOptionSenderThrowsMissingRequiredOptionException() { $factory = $this->createFactory(); - $this->expectException(IncompleteDsnException::class); + $this->expectException(MissingRequiredOptionException::class); $factory->create(Dsn::fromString('sendinblue://apiKey@host.test')); } diff --git a/src/Symfony/Component/Notifier/Bridge/Sinch/SinchTransportFactory.php b/src/Symfony/Component/Notifier/Bridge/Sinch/SinchTransportFactory.php index 1ef55d6ec9dd0..30ac8a929942a 100644 --- a/src/Symfony/Component/Notifier/Bridge/Sinch/SinchTransportFactory.php +++ b/src/Symfony/Component/Notifier/Bridge/Sinch/SinchTransportFactory.php @@ -11,7 +11,6 @@ namespace Symfony\Component\Notifier\Bridge\Sinch; -use Symfony\Component\Notifier\Exception\IncompleteDsnException; use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; use Symfony\Component\Notifier\Transport\AbstractTransportFactory; use Symfony\Component\Notifier\Transport\Dsn; @@ -32,12 +31,7 @@ public function create(Dsn $dsn): TransportInterface $accountSid = $this->getUser($dsn); $authToken = $this->getPassword($dsn); - $from = $dsn->getOption('from'); - - if (!$from) { - throw new IncompleteDsnException('Missing from.', $dsn->getOriginalDsn()); - } - + $from = $dsn->getRequiredOption('from'); $host = 'default' === $dsn->getHost() ? null : $dsn->getHost(); $port = $dsn->getPort(); diff --git a/src/Symfony/Component/Notifier/Bridge/Sinch/Tests/SinchTransportFactoryTest.php b/src/Symfony/Component/Notifier/Bridge/Sinch/Tests/SinchTransportFactoryTest.php index 367342e8a636f..1ed1dd779c587 100644 --- a/src/Symfony/Component/Notifier/Bridge/Sinch/Tests/SinchTransportFactoryTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Sinch/Tests/SinchTransportFactoryTest.php @@ -39,7 +39,7 @@ public function supportsProvider(): iterable yield [false, 'somethingElse://accountSid:authToken@default?from=0611223344']; } - public function incompleteDsnProvider(): iterable + public function missingRequiredOptionProvider(): iterable { yield 'missing option: from' => ['sinch://accountSid:authToken@default']; } diff --git a/src/Symfony/Component/Notifier/Bridge/Smsapi/SmsapiTransportFactory.php b/src/Symfony/Component/Notifier/Bridge/Smsapi/SmsapiTransportFactory.php index 6f3e42d97928a..5080e057d518f 100644 --- a/src/Symfony/Component/Notifier/Bridge/Smsapi/SmsapiTransportFactory.php +++ b/src/Symfony/Component/Notifier/Bridge/Smsapi/SmsapiTransportFactory.php @@ -11,7 +11,6 @@ namespace Symfony\Component\Notifier\Bridge\Smsapi; -use Symfony\Component\Notifier\Exception\IncompleteDsnException; use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; use Symfony\Component\Notifier\Transport\AbstractTransportFactory; use Symfony\Component\Notifier\Transport\Dsn; @@ -36,12 +35,7 @@ public function create(Dsn $dsn): TransportInterface } $authToken = $this->getUser($dsn); - $from = $dsn->getOption('from'); - - if (!$from) { - throw new IncompleteDsnException('Missing from.', $dsn->getOriginalDsn()); - } - + $from = $dsn->getRequiredOption('from'); $host = 'default' === $dsn->getHost() ? null : $dsn->getHost(); $port = $dsn->getPort(); diff --git a/src/Symfony/Component/Notifier/Bridge/Smsapi/Tests/SmsapiTransportFactoryTest.php b/src/Symfony/Component/Notifier/Bridge/Smsapi/Tests/SmsapiTransportFactoryTest.php index 86c8ee63a551d..f5a4a66dd4fe9 100644 --- a/src/Symfony/Component/Notifier/Bridge/Smsapi/Tests/SmsapiTransportFactoryTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Smsapi/Tests/SmsapiTransportFactoryTest.php @@ -14,6 +14,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Notifier\Bridge\Smsapi\SmsapiTransportFactory; use Symfony\Component\Notifier\Exception\IncompleteDsnException; +use Symfony\Component\Notifier\Exception\MissingRequiredOptionException; use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; use Symfony\Component\Notifier\Transport\Dsn; @@ -28,11 +29,11 @@ public function testCreateWithDsn() $this->assertSame('smsapi://host.test?from=testFrom', (string) $transport); } - public function testCreateWithMissingOptionFromThrowsIncompleteDsnException() + public function testCreateWithMissingOptionFromThrowsMissingRequiredOptionException() { $factory = $this->createFactory(); - $this->expectException(IncompleteDsnException::class); + $this->expectException(MissingRequiredOptionException::class); $factory->create(Dsn::fromString('smsapi://token@host')); } diff --git a/src/Symfony/Component/Notifier/Bridge/Twilio/Tests/TwilioTransportFactoryTest.php b/src/Symfony/Component/Notifier/Bridge/Twilio/Tests/TwilioTransportFactoryTest.php index b7b3bab9f2814..f5cf9a29ade25 100644 --- a/src/Symfony/Component/Notifier/Bridge/Twilio/Tests/TwilioTransportFactoryTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Twilio/Tests/TwilioTransportFactoryTest.php @@ -39,7 +39,7 @@ public function supportsProvider(): iterable yield [false, 'somethingElse://accountSid:authToken@default?from=0611223344']; } - public function incompleteDsnProvider(): iterable + public function missingRequiredOptionProvider(): iterable { yield 'missing option: from' => ['twilio://accountSid:authToken@default']; } diff --git a/src/Symfony/Component/Notifier/Bridge/Twilio/TwilioTransportFactory.php b/src/Symfony/Component/Notifier/Bridge/Twilio/TwilioTransportFactory.php index bda4b5c5eea71..2009d63dc4ea5 100644 --- a/src/Symfony/Component/Notifier/Bridge/Twilio/TwilioTransportFactory.php +++ b/src/Symfony/Component/Notifier/Bridge/Twilio/TwilioTransportFactory.php @@ -11,7 +11,6 @@ namespace Symfony\Component\Notifier\Bridge\Twilio; -use Symfony\Component\Notifier\Exception\IncompleteDsnException; use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; use Symfony\Component\Notifier\Transport\AbstractTransportFactory; use Symfony\Component\Notifier\Transport\Dsn; @@ -35,12 +34,7 @@ public function create(Dsn $dsn): TransportInterface $accountSid = $this->getUser($dsn); $authToken = $this->getPassword($dsn); - $from = $dsn->getOption('from'); - - if (!$from) { - throw new IncompleteDsnException('Missing from.', $dsn->getOriginalDsn()); - } - + $from = $dsn->getRequiredOption('from'); $host = 'default' === $dsn->getHost() ? null : $dsn->getHost(); $port = $dsn->getPort(); diff --git a/src/Symfony/Component/Notifier/Bridge/Zulip/Tests/ZulipTransportFactoryTest.php b/src/Symfony/Component/Notifier/Bridge/Zulip/Tests/ZulipTransportFactoryTest.php index d60c7ad80f77e..b09c769a11624 100644 --- a/src/Symfony/Component/Notifier/Bridge/Zulip/Tests/ZulipTransportFactoryTest.php +++ b/src/Symfony/Component/Notifier/Bridge/Zulip/Tests/ZulipTransportFactoryTest.php @@ -14,6 +14,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Notifier\Bridge\Zulip\ZulipTransportFactory; use Symfony\Component\Notifier\Exception\IncompleteDsnException; +use Symfony\Component\Notifier\Exception\MissingRequiredOptionException; use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; use Symfony\Component\Notifier\Transport\Dsn; @@ -28,11 +29,11 @@ public function testCreateWithDsn() $this->assertSame('zulip://host.test?channel=testChannel', (string) $transport); } - public function testCreateWithMissingOptionChannelThrowsIncompleteDsnException() + public function testCreateWithMissingOptionChannelThrowsMissingRequiredOptionException() { $factory = $this->createFactory(); - $this->expectException(IncompleteDsnException::class); + $this->expectException(MissingRequiredOptionException::class); $factory->create(Dsn::fromString('zulip://email:token@host')); } diff --git a/src/Symfony/Component/Notifier/Bridge/Zulip/ZulipTransportFactory.php b/src/Symfony/Component/Notifier/Bridge/Zulip/ZulipTransportFactory.php index 3914202529243..8d54924c06f62 100644 --- a/src/Symfony/Component/Notifier/Bridge/Zulip/ZulipTransportFactory.php +++ b/src/Symfony/Component/Notifier/Bridge/Zulip/ZulipTransportFactory.php @@ -11,7 +11,6 @@ namespace Symfony\Component\Notifier\Bridge\Zulip; -use Symfony\Component\Notifier\Exception\IncompleteDsnException; use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; use Symfony\Component\Notifier\Transport\AbstractTransportFactory; use Symfony\Component\Notifier\Transport\Dsn; @@ -35,12 +34,7 @@ public function create(Dsn $dsn): TransportInterface $email = $this->getUser($dsn); $token = $this->getPassword($dsn); - $channel = $dsn->getOption('channel'); - - if (!$channel) { - throw new IncompleteDsnException('Missing channel.', $dsn->getOriginalDsn()); - } - + $channel = $dsn->getRequiredOption('channel'); $host = $dsn->getHost(); $port = $dsn->getPort(); diff --git a/src/Symfony/Component/Notifier/CHANGELOG.md b/src/Symfony/Component/Notifier/CHANGELOG.md index f6106137d9478..b236eca51e408 100644 --- a/src/Symfony/Component/Notifier/CHANGELOG.md +++ b/src/Symfony/Component/Notifier/CHANGELOG.md @@ -6,6 +6,7 @@ CHANGELOG * The component is not marked as `@experimental` anymore * [BC BREAK] Changed the return type of `AbstractTransportFactory::getEndpoint()` from `?string` to `string` + * Added `DSN::getRequiredOption` method which throws a new `MissingRequiredOptionException`. 5.2.0 ----- diff --git a/src/Symfony/Component/Notifier/Exception/MissingRequiredOptionException.php b/src/Symfony/Component/Notifier/Exception/MissingRequiredOptionException.php new file mode 100644 index 0000000000000..afcacf03fdd0c --- /dev/null +++ b/src/Symfony/Component/Notifier/Exception/MissingRequiredOptionException.php @@ -0,0 +1,27 @@ + + * + * 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 MissingRequiredOptionException extends IncompleteDsnException +{ + public function __construct(string $option, string $dsn = null, ?\Throwable $previous = null) + { + $message = sprintf('The option "%s" is required but missing.', $option); + + parent::__construct($message, $dsn, $previous); + } +} diff --git a/src/Symfony/Component/Notifier/Tests/Transport/DsnTest.php b/src/Symfony/Component/Notifier/Tests/Transport/DsnTest.php index fc7072dee68ec..abd12a5134e84 100644 --- a/src/Symfony/Component/Notifier/Tests/Transport/DsnTest.php +++ b/src/Symfony/Component/Notifier/Tests/Transport/DsnTest.php @@ -13,6 +13,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Notifier\Exception\InvalidArgumentException; +use Symfony\Component\Notifier\Exception\MissingRequiredOptionException; use Symfony\Component\Notifier\Transport\Dsn; final class DsnTest extends TestCase @@ -120,4 +121,54 @@ public function testGetOption() $this->assertSame('default', $dsn->getOption('nullable', 'default')); $this->assertSame('default', $dsn->getOption('not_existent_property', 'default')); } + + public function testGetRequiredOptionGetsOptionIfSet() + { + $options = ['with_value' => 'some value']; + $dsn = new Dsn('scheme', 'localhost', 'u$er', 'pa$s', '8000', $options, '/channel'); + + $this->assertSame('some value', $dsn->getRequiredOption('with_value')); + } + + public function testGetRequiredOptionGetsOptionIfValueIsZero() + { + $options = ['timeout' => 0]; + $dsn = new Dsn('scheme', 'localhost', 'u$er', 'pa$s', '8000', $options, '/channel'); + + $this->assertSame(0, $dsn->getRequiredOption('timeout')); + } + + /** + * @dataProvider getRequiredOptionThrowsMissingRequiredOptionExceptionProvider + */ + public function testGetRequiredOptionThrowsMissingRequiredOptionException(string $expectedExceptionMessage, array $options, string $option) + { + $dsn = new Dsn('scheme', 'localhost', 'u$er', 'pa$s', '8000', $options, '/channel'); + + $this->expectException(MissingRequiredOptionException::class); + $this->expectExceptionMessage($expectedExceptionMessage); + + $dsn->getRequiredOption($option); + } + + public function getRequiredOptionThrowsMissingRequiredOptionExceptionProvider(): iterable + { + yield [ + 'The option "foo_bar" is required but missing.', + ['with_value' => 'some value'], + 'foo_bar', + ]; + + yield [ + 'The option "with_empty_string" is required but missing.', + ['with_empty_string' => ''], + 'with_empty_string', + ]; + + yield [ + 'The option "with_null" is required but missing.', + ['with_null' => null], + 'with_null', + ]; + } } diff --git a/src/Symfony/Component/Notifier/Tests/TransportFactoryTestCase.php b/src/Symfony/Component/Notifier/Tests/TransportFactoryTestCase.php index be3da0ff34b61..c539f5d652816 100644 --- a/src/Symfony/Component/Notifier/Tests/TransportFactoryTestCase.php +++ b/src/Symfony/Component/Notifier/Tests/TransportFactoryTestCase.php @@ -14,6 +14,7 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Notifier\Exception\IncompleteDsnException; use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; +use Symfony\Component\Notifier\Exception\MissingRequiredOptionException; use Symfony\Component\Notifier\Transport\Dsn; use Symfony\Component\Notifier\Transport\TransportFactoryInterface; @@ -52,6 +53,14 @@ public function incompleteDsnProvider(): iterable return []; } + /** + * @return iterable + */ + public function missingRequiredOptionProvider(): iterable + { + return []; + } + /** * @dataProvider supportsProvider */ @@ -106,4 +115,21 @@ public function testIncompleteDsnException(string $dsn, string $message = null) $factory->create($dsn); } + + /** + * @dataProvider missingRequiredOptionProvider + */ + public function testMissingRequiredOptionException(string $dsn, string $message = null): void + { + $factory = $this->createFactory(); + + $dsn = Dsn::fromString($dsn); + + $this->expectException(MissingRequiredOptionException::class); + if (null !== $message) { + $this->expectExceptionMessage($message); + } + + $factory->create($dsn); + } } diff --git a/src/Symfony/Component/Notifier/Transport/Dsn.php b/src/Symfony/Component/Notifier/Transport/Dsn.php index f2385a5584534..0e2e2abd67579 100644 --- a/src/Symfony/Component/Notifier/Transport/Dsn.php +++ b/src/Symfony/Component/Notifier/Transport/Dsn.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Notifier\Transport; use Symfony\Component\Notifier\Exception\InvalidArgumentException; +use Symfony\Component\Notifier\Exception\MissingRequiredOptionException; /** * @author Fabien Potencier @@ -94,6 +95,15 @@ public function getOption(string $key, $default = null) return $this->options[$key] ?? $default; } + public function getRequiredOption(string $key) + { + if (!\array_key_exists($key, $this->options) || '' === trim($this->options[$key])) { + throw new MissingRequiredOptionException($key); + } + + return $this->options[$key]; + } + public function getPath(): ?string { return $this->path; diff --git a/src/Symfony/Component/Notifier/Transport/TransportFactoryInterface.php b/src/Symfony/Component/Notifier/Transport/TransportFactoryInterface.php index ff62e189c6304..094e159c72df9 100644 --- a/src/Symfony/Component/Notifier/Transport/TransportFactoryInterface.php +++ b/src/Symfony/Component/Notifier/Transport/TransportFactoryInterface.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Notifier\Transport; use Symfony\Component\Notifier\Exception\IncompleteDsnException; +use Symfony\Component\Notifier\Exception\MissingRequiredOptionException; use Symfony\Component\Notifier\Exception\UnsupportedSchemeException; /** @@ -22,6 +23,7 @@ interface TransportFactoryInterface /** * @throws UnsupportedSchemeException * @throws IncompleteDsnException + * @throws MissingRequiredOptionException */ public function create(Dsn $dsn): TransportInterface;