Skip to content

Commit 73cab0e

Browse files
[Mailer] Move inboxId to __construct
Move inboxId input to __construct as opposed to separate helper class to fit more in-line with use of AbstractApiTransport.
1 parent 84a4fa6 commit 73cab0e

File tree

4 files changed

+28
-26
lines changed

4 files changed

+28
-26
lines changed

src/Symfony/Component/Mailer/Bridge/Mailtrap/Tests/Transport/MailtrapApiSandboxTransportTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@ public static function getTransportData(): array
3737
{
3838
return [
3939
[
40-
(new MailtrapApiSandboxTransport('KEY'))->setInboxId(123456),
40+
new MailtrapApiSandboxTransport('KEY', null, null, null, 123456),
4141
'mailtrap+sandbox://sandbox.api.mailtrap.io/?inboxId=123456',
4242
],
4343
[
44-
(new MailtrapApiSandboxTransport('KEY'))->setInboxId(123456)->setHost('example.com'),
44+
(new MailtrapApiSandboxTransport('KEY', null, null, null, 123456))->setHost('example.com'),
4545
'mailtrap+sandbox://example.com/?inboxId=123456',
4646
],
4747
[
48-
(new MailtrapApiSandboxTransport('KEY'))->setInboxId(123456)->setHost('example.com')->setPort(99),
48+
(new MailtrapApiSandboxTransport('KEY', null, null, null, 123456))->setHost('example.com')->setPort(99),
4949
'mailtrap+sandbox://example.com:99/?inboxId=123456',
5050
],
5151
[
52-
(new MailtrapApiSandboxTransport('KEY'))->setInboxId(123456),
52+
new MailtrapApiSandboxTransport('KEY', null, null, null, 123456),
5353
'mailtrap+sandbox://sandbox.api.mailtrap.io/?inboxId=123456',
5454
],
5555
];
@@ -72,7 +72,7 @@ public function testSend()
7272
]);
7373
});
7474

75-
$transport = (new MailtrapApiSandboxTransport('KEY', $client))->setInboxId(123456);
75+
$transport = new MailtrapApiSandboxTransport('KEY', $client, null, null, 123456);
7676

7777
$mail = new Email();
7878
$mail->subject('Hello!')
@@ -88,7 +88,7 @@ public function testSendThrowsForErrorResponse()
8888
$client = new MockHttpClient(static fn (string $method, string $url, array $options): ResponseInterface => new JsonMockResponse(['errors' => ['i\'m a teapot']], [
8989
'http_code' => 418,
9090
]));
91-
$transport = (new MailtrapApiSandboxTransport('KEY', $client))->setInboxId(123456);
91+
$transport = new MailtrapApiSandboxTransport('KEY', $client, null, null, 123456);
9292
$transport->setPort(8984);
9393

9494
$mail = new Email();
@@ -110,7 +110,7 @@ public function testTagAndMetadataHeaders()
110110
$email->getHeaders()->add(new MetadataHeader('Client-ID', '12345'));
111111
$envelope = new Envelope(new Address('alice@system.com'), [new Address('bob@system.com')]);
112112

113-
$transport = (new MailtrapApiSandboxTransport('ACCESS_KEY'))->setInboxId(123456);
113+
$transport = new MailtrapApiSandboxTransport('ACCESS_KEY', null, null, null, 123456);
114114
$method = new \ReflectionMethod(MailtrapApiSandboxTransport::class, 'getPayload');
115115
$payload = $method->invoke($transport, $email, $envelope);
116116

@@ -129,7 +129,7 @@ public function testMultipleTagsAreNotAllowed()
129129
$email->getHeaders()->add(new TagHeader('tag2'));
130130
$envelope = new Envelope(new Address('alice@system.com'), [new Address('bob@system.com')]);
131131

132-
$transport = (new MailtrapApiSandboxTransport('ACCESS_KEY'))->setInboxId(123456);
132+
$transport = new MailtrapApiSandboxTransport('ACCESS_KEY', null, null, null, 123456);
133133
$method = new \ReflectionMethod(MailtrapApiSandboxTransport::class, 'getPayload');
134134

135135
$this->expectException(TransportException::class);

src/Symfony/Component/Mailer/Bridge/Mailtrap/Tests/Transport/MailtrapTransportFactoryTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ public static function createProvider(): iterable
8080

8181
yield [
8282
new Dsn('mailtrap+sandbox', 'default', self::USER, null, null, ['inboxId' => '123456']),
83-
(new MailtrapApiSandboxTransport(self::USER, new MockHttpClient(), null, $logger))->setInboxId(123456),
83+
new MailtrapApiSandboxTransport(self::USER, new MockHttpClient(), null, $logger, 123456),
8484
];
8585

8686
yield [
8787
new Dsn('mailtrap+sandbox', 'example.com', self::USER, null, 8080, ['inboxId' => '123456']),
88-
(new MailtrapApiSandboxTransport(self::USER, new MockHttpClient(), null, $logger))->setHost('example.com')->setPort(8080)->setInboxId(123456),
88+
(new MailtrapApiSandboxTransport(self::USER, new MockHttpClient(), null, $logger, 123456))->setHost('example.com')->setPort(8080),
8989
];
9090

9191
yield [

src/Symfony/Component/Mailer/Bridge/Mailtrap/Transport/MailtrapApiSandboxTransport.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,39 @@
1111

1212
namespace Symfony\Component\Mailer\Bridge\Mailtrap\Transport;
1313

14+
use Psr\EventDispatcher\EventDispatcherInterface;
15+
use Psr\Log\LoggerInterface;
1416
use Symfony\Component\Mailer\Exception\TransportException;
17+
use Symfony\Contracts\HttpClient\HttpClientInterface;
1518

1619
/**
1720
* @author Kieran Cross
1821
*/
1922
final class MailtrapApiSandboxTransport extends MailtrapApiTransport
2023
{
2124
protected const HOST = 'sandbox.api.mailtrap.io';
22-
private ?int $inboxId = null;
2325

24-
public function __toString(): string
25-
{
26-
return \sprintf('mailtrap+sandbox://%s%s/?inboxId=%u', $this->host ?: static::HOST, $this->port ? ':'.$this->port : '', $this->inboxId);
26+
public function __construct(
27+
#[\SensitiveParameter] private string $token,
28+
?HttpClientInterface $client = null,
29+
?EventDispatcherInterface $dispatcher = null,
30+
?LoggerInterface $logger = null,
31+
private ?int $inboxId = null,
32+
) {
33+
if (null === $inboxId) {
34+
throw new TransportException('Missing required "inboxId" option for Mailtrap sandbox transport.');
35+
}
36+
37+
parent::__construct($token, $client, $dispatcher, $logger);
2738
}
2839

29-
/**
30-
* @return $this
31-
*/
32-
public function setInboxId(?int $inboxId): static
40+
public function __toString(): string
3341
{
34-
$this->inboxId = $inboxId;
35-
36-
return $this;
42+
return \sprintf('mailtrap+sandbox://%s%s/?inboxId=%u', $this->host ?: static::HOST, $this->port ? ':'.$this->port : '', $this->inboxId);
3743
}
3844

3945
protected function getEndpoint(): string
4046
{
41-
if (null === $this->inboxId) {
42-
throw new TransportException('Missing required "inboxId" option for Mailtrap sandbox transport.');
43-
}
44-
4547
return parent::getEndpoint().'/'.$this->inboxId;
4648
}
4749
}

src/Symfony/Component/Mailer/Bridge/Mailtrap/Transport/MailtrapTransportFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function create(Dsn $dsn): TransportInterface
3535
} else {
3636
$inboxId = $dsn->getOption('inboxId');
3737

38-
return (new MailtrapApiSandboxTransport($user, $this->client, $this->dispatcher, $this->logger))->setHost($host)->setPort($port)->setInboxId($inboxId);
38+
return (new MailtrapApiSandboxTransport($user, $this->client, $this->dispatcher, $this->logger, $inboxId))->setHost($host)->setPort($port);
3939
}
4040
}
4141

0 commit comments

Comments
 (0)