Skip to content

Commit 3486244

Browse files
committed
[Mailer] Change the DSN semantics
1 parent 8da8f82 commit 3486244

12 files changed

+51
-40
lines changed

Exception/UnsupportedSchemeException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
*/
1919
class UnsupportedSchemeException extends LogicException
2020
{
21-
public function __construct(Dsn $dsn, array $supported)
21+
public function __construct(Dsn $dsn, string $name, array $supported)
2222
{
23-
parent::__construct(sprintf('The "%s" scheme is not supported for mailer "%s". Supported schemes are: "%s".', $dsn->getScheme(), $dsn->getHost(), implode('", "', $supported)));
23+
parent::__construct(sprintf('The "%s" scheme is not supported. Supported schemes for mailer "%s" are: "%s".', $dsn->getScheme(), $name, implode('", "', $supported)));
2424
}
2525
}

Test/TransportFactoryTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function testCreate(Dsn $dsn, TransportInterface $transport): void
6969
$factory = $this->getFactory();
7070

7171
$this->assertEquals($transport, $factory->create($dsn));
72-
if ('smtp' !== $dsn->getScheme() && 'smtps' !== $dsn->getScheme()) {
72+
if (false !== strpos('smtp', $dsn->getScheme())) {
7373
$this->assertStringMatchesFormat($dsn->getScheme().'://%S'.$dsn->getHost().'%S', (string) $transport);
7474
}
7575
}

Tests/Transport/NullTransportFactoryTest.php

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,16 @@ public function getFactory(): TransportFactoryInterface
2727
public function supportsProvider(): iterable
2828
{
2929
yield [
30-
new Dsn('smtp', 'null'),
30+
new Dsn('null', ''),
3131
true,
3232
];
33-
34-
yield [
35-
new Dsn('smtp', 'example.com'),
36-
false,
37-
];
3833
}
3934

4035
public function createProvider(): iterable
4136
{
4237
yield [
43-
new Dsn('smtp', 'null'),
38+
new Dsn('null', 'null'),
4439
new NullTransport($this->getDispatcher(), $this->getLogger()),
4540
];
4641
}
47-
48-
public function unsupportedSchemeProvider(): iterable
49-
{
50-
yield [
51-
new Dsn('foo', 'null'),
52-
'The "foo" scheme is not supported for mailer "null". Supported schemes are: "smtp".',
53-
];
54-
}
5542
}

Tests/Transport/NullTransportTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ class NullTransportTest extends TestCase
1919
public function testToString()
2020
{
2121
$t = new NullTransport();
22-
$this->assertEquals('smtp://null', (string) $t);
22+
$this->assertEquals('null://', (string) $t);
2323
}
2424
}

Tests/Transport/SendmailTransportFactoryTest.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,24 @@ public function getFactory(): TransportFactoryInterface
2727
public function supportsProvider(): iterable
2828
{
2929
yield [
30-
new Dsn('smtp', 'sendmail'),
30+
new Dsn('sendmail+smtp', 'default'),
3131
true,
3232
];
33-
34-
yield [
35-
new Dsn('smtp', 'example.com'),
36-
false,
37-
];
3833
}
3934

4035
public function createProvider(): iterable
4136
{
4237
yield [
43-
new Dsn('smtp', 'sendmail'),
38+
new Dsn('sendmail+smtp', 'default'),
4439
new SendmailTransport(null, $this->getDispatcher(), $this->getLogger()),
4540
];
4641
}
4742

4843
public function unsupportedSchemeProvider(): iterable
4944
{
5045
yield [
51-
new Dsn('http', 'sendmail'),
52-
'The "http" scheme is not supported for mailer "sendmail". Supported schemes are: "smtp".',
46+
new Dsn('sendmail+http', 'default'),
47+
'The "sendmail+http" scheme is not supported. Supported schemes for mailer "sendmail" are: "sendmail", "sendmail+smtp".',
5348
];
5449
}
5550
}

Tests/Transport/Smtp/EsmtpTransportFactoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public function createProvider(): iterable
6464
$transport = new EsmtpTransport('example.com', 465, true, $eventDispatcher, $logger);
6565

6666
yield [
67-
new Dsn('smtp', 'example.com', '', '', 465),
67+
new Dsn('smtps', 'example.com', '', '', 465),
6868
$transport,
6969
];
7070
}

Transport/AbstractHttpTransport.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
*/
2525
abstract class AbstractHttpTransport extends AbstractTransport
2626
{
27+
protected $host;
28+
protected $port;
2729
protected $client;
2830

2931
public function __construct(HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
@@ -40,6 +42,26 @@ public function __construct(HttpClientInterface $client = null, EventDispatcherI
4042
parent::__construct($dispatcher, $logger);
4143
}
4244

45+
/**
46+
* @return $this
47+
*/
48+
public function setHost(?string $host)
49+
{
50+
$this->host = $host;
51+
52+
return $this;
53+
}
54+
55+
/**
56+
* @return $this
57+
*/
58+
public function setPort(?int $port)
59+
{
60+
$this->port = $port;
61+
62+
return $this;
63+
}
64+
4365
abstract protected function doSendHttp(SentMessage $message): ResponseInterface;
4466

4567
protected function doSend(SentMessage $message): void

Transport/AbstractTransportFactory.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ public function __construct(EventDispatcherInterface $dispatcher = null, HttpCli
3232
$this->logger = $logger;
3333
}
3434

35+
public function supports(Dsn $dsn): bool
36+
{
37+
return \in_array($dsn->getScheme(), $this->getSupportedSchemes());
38+
}
39+
40+
abstract protected function getSupportedSchemes(): array;
41+
3542
protected function getUser(Dsn $dsn): string
3643
{
3744
$user = $dsn->getUser();

Transport/NullTransport.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ protected function doSend(SentMessage $message): void
2626

2727
public function __toString(): string
2828
{
29-
return 'smtp://null';
29+
return 'null://';
3030
}
3131
}

Transport/NullTransportFactory.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ final class NullTransportFactory extends AbstractTransportFactory
2020
{
2121
public function create(Dsn $dsn): TransportInterface
2222
{
23-
if ('smtp' === $dsn->getScheme()) {
23+
if ('null' === $dsn->getScheme()) {
2424
return new NullTransport($this->dispatcher, $this->logger);
2525
}
2626

27-
throw new UnsupportedSchemeException($dsn, ['smtp']);
27+
throw new UnsupportedSchemeException($dsn, 'null', $this->getSupportedSchemes());
2828
}
2929

30-
public function supports(Dsn $dsn): bool
30+
protected function getSupportedSchemes(): array
3131
{
32-
return 'null' === $dsn->getHost();
32+
return ['null'];
3333
}
3434
}

0 commit comments

Comments
 (0)