8000 [Notifier][DX] Dsn::getRequiredOption() · symfony/symfony@02c3640 · GitHub
[go: up one dir, main page]

Skip to content

Commit 02c3640

Browse files
committed
[Notifier][DX] Dsn::getRequiredOption()
1 parent b47b239 commit 02c3640

File tree

5 files changed

+50
-16
lines changed

5 files changed

+50
-16
lines changed

src/Symfony/Component/Notifier/Bridge/OvhCloud/OvhCloudTransportFactory.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,8 @@ public function create(Dsn $dsn): TransportInterface
3434

3535
$applicationKey = $this->getUser($dsn);
3636
$applicationSecret = $this->getPassword($dsn);
37-
$consumerKey = $dsn->getOption('consumer_key');
38-
39-
if (!$consumerKey) {
40-
throw new IncompleteDsnException('Missing consumer_key.', $dsn->getOriginalDsn());
41-
}
42-
43-
$serviceName = $dsn->getOption('service_name');
44-
45-
if (!$serviceName) {
46-
throw new IncompleteDsnException('Missing service_name.', $dsn->getOriginalDsn());
47-
}
48-
37+
$consumerKey = $dsn->getRequiredOption('consumer_key');
38+
$serviceName = $dsn->getRequiredOption('service_name');
4939
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
5040
$port = $dsn->getPort();
5141

src/Symfony/Component/Notifier/Bridge/OvhCloud/Tests/OvhCloudTransportFactoryTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Notifier\Bridge\OvhCloud\OvhCloudTransportFactory;
1616
use Symfony\Component\Notifier\Exception\IncompleteDsnException;
17+
use Symfony\Component\Notifier\Exception\RequiredOptionMissingException;
1718
use Symfony\Component\Notifier\Exception\UnsupportedSchemeException;
1819
use Symfony\Component\Notifier\Transport\Dsn;
1920

@@ -30,21 +31,21 @@ public function testCreateWithDsn()
3031
$this->assertSame('ovhcloud://host.test?consumer_key=consumerKey&service_name=serviceName', (string) $transport);
3132
}
3233

33-
public function testCreateWithMissingOptionConsumerKeyThrowsIncompleteDsnException()
34+
public function testCreateWithMissingOptionConsumerKeyThrowsRequiredOptionMissingException()
3435
{
3536
$factory = $this->createFactory();
3637

37-
$this->expectException(IncompleteDsnException::class);
38+
$this->expectException(RequiredOptionMissingException::class);
3839

3940
$dsnIncomplete = 'ovhcloud://applicationKey:applicationSecret@default?service_name=serviceName';
4041
$factory->create(Dsn::fromString($dsnIncomplete));
4142
}
4243

43-
public function testCreateWithMissingOptionServiceNameThrowsIncompleteDsnException()
44+
public function testCreateWithMissingOptionServiceNameThrowsRequiredOptionMissingException()
4445
{
4546
$factory = $this->createFactory();
4647

47-
$this->expectException(IncompleteDsnException::class);
48+
$this->expectException(RequiredOptionMissingException::class);
4849

4950
$dsnIncomplete = 'ovhcloud://applicationKey:applicationSecret@default?consumeer_key=consumerKey';
5051
$factory->create(Dsn::fromString($dsnIncomplete));

src/Symfony/Component/Notifier/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
5.3.0
5+
-----
6+
7+
* Added `DSN::getRequiredOption` method which throws a new `RequiredOptionMissingException`.
8+
This new exception extends `IncompleteDsnException` to stay BC.
9+
410
5.2.0
511
-----
612

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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\Exception;
13+
14+
/**
15+
* @author Oskar Stark <oskarstark@googlemail 402E .com>
16+
*
17+
* @experimental in 5.3
18+
*/
19+
class RequiredOptionMissingException extends IncompleteDsnException
20+
{
21+
public function __construct(string $option, string $dsn = null, ?\Throwable $previous = null)
22+
{
23+
$message = sprintf('The option "%s" is required but missing.', $option);
24+
25+
parent::__construct($message, $dsn, $previous);
26+
}
27+
}

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

Lines changed: 10 additions & 0 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\RequiredOptionMissingException;
1516

1617
/**
1718
* @author Fabien Potencier <fabien@symfony.com>
@@ -96,6 +97,15 @@ public function getOption(string $key, $default = null)
9697
return $this->options[$key] ?? $default;
9798
}
9899

100+
public function getRequiredOption(string $key)
101+
{
102+
if (!isset($this->options[$key]) || !$this->options[$key]) {
103+
throw new RequiredOptionMissingException($key);
104+
}
105+
106+
return $this->options[$key];
107+
}
108+
99109
public function getPath(): ?string
100110
{
101111
return $this->path;

0 commit comments

Comments
 (0)
0