Skip to content

[Mailer] Add a name to the transports #32916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public function __construct(EventDispatcherInterface $eventDispatcher, LoggerInt
$this->onDoSend = $onDoSend;
}

public function getName(): string
{
return 'dummy://local';
}

protected function doSend(SentMessage $message): void
{
$onDoSend = $this->onDoSend;
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"symfony/dom-crawler": "<4.3",
"symfony/form": "<4.3",
"symfony/lock": "<4.4",
"symfony/mailer": "<4.4",
"symfony/messenger": "<4.3",
"symfony/property-info": "<3.4",
"symfony/serializer": "<4.2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public function __construct(string $accessKey, string $secretKey, string $region
parent::__construct($client, $dispatcher, $logger);
}

public function getName(): string
{
return sprintf('api://%s@ses?region=%s', $this->accessKey, $this->region);
}

protected function doSendApi(Email $email, SmtpEnvelope $envelope): ResponseInterface
{
$date = gmdate('D, d M Y H:i:s e');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public function __construct(string $accessKey, string $secretKey, string $region
parent::__construct($client, $dispatcher, $logger);
}

public function getName(): string
{
return sprintf('http://%s@ses?region=%s', $this->accessKey, $this->region);
}

protected function doSendHttp(SentMessage $message): ResponseInterface
{
$date = gmdate('D, d M Y H:i:s e');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public function __construct(string $key, HttpClientInterface $client = null, Eve
parent::__construct($client, $dispatcher, $logger);
}

public function getName(): string
{
return sprintf('api://mandrill');
}

protected function doSendApi(Email $email, SmtpEnvelope $envelope): ResponseInterface
{
$response = $this->client->request('POST', self::ENDPOINT, [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public function __construct(string $key, HttpClientInterface $client = null, Eve
parent::__construct($client, $dispatcher, $logger);
}

public function getName(): string
{
return sprintf('http://mandrill');
}

protected function doSendHttp(SentMessage $message): ResponseInterface
{
$envelope = $message->getEnvelope();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public function __construct(string $key, string $domain, string $region = null,
parent::__construct($client, $dispatcher, $logger);
}

public function getName(): string
{
return sprintf('api://%s@mailgun?region=%s', $this->domain, $this->region);
}

protected function doSendApi(Email $email, SmtpEnvelope $envelope): ResponseInterface
{
$body = new FormDataPart($this->getPayload($email, $envelope));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ public function __construct(string $key, string $domain, string $region = null,
parent::__construct($client, $dispatcher, $logger);
}

public function getName(): string
{
return sprintf('http://%s@mailgun?region=%s', $this->domain, $this->region);
}

protected function doSendHttp(SentMessage $message): ResponseInterface
{
$body = new FormDataPart([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public function __construct(string $key, HttpClientInterface $client = null, Eve
parent::__construct($client, $dispatcher, $logger);
}

public function getName(): string
{
return sprintf('api://postmark');
}

protected function doSendApi(Email $email, SmtpEnvelope $envelope): ResponseInterface
{
$response = $this->client->request('POST', self::ENDPOINT, [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public function __construct(string $key, HttpClientInterface $client = null, Eve
parent::__construct($client, $dispatcher, $logger);
}

public function getName(): string
{
return sprintf('api://sendgrid');
}

protected function doSendApi(Email $email, SmtpEnvelope $envelope): ResponseInterface
{
$response = $this->client->request('POST', self::ENDPOINT, [
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Mailer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
4.4.0
-----

* [BC BREAK] `TransportInterface` has a new `getName()` method
* [BC BREAK] Classes `AbstractApiTransport` and `AbstractHttpTransport` moved under `Transport` sub-namespace.
* [BC BREAK] Transports depend on `Symfony\Contracts\EventDispatcher\EventDispatcherInterface`
instead of `Symfony\Component\EventDispatcher\EventDispatcherInterface`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ public function testCreate(Dsn $dsn, TransportInterface $transport): void
$factory = $this->getFactory();

$this->assertEquals($transport, $factory->create($dsn));
if ('smtp' !== $dsn->getScheme()) {
$this->assertStringMatchesFormat($dsn->getScheme().'://%S'.$dsn->getHost().'%S', $transport->getName());
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ public function testSendNoTransports()
new FailoverTransport([]);
}

public function testGetName()
{
$t1 = $this->createMock(TransportInterface::class);
$t1->expects($this->once())->method('getName')->willReturn('t1://local');
$t2 = $this->createMock(TransportInterface::class);
$t2->expects($this->once())->method('getName')->willReturn('t2://local');
$t = new FailoverTransport([$t1, $t2]);
$this->assertEquals('t1://local || t2://local', $t->getName());
}

public function testSendFirstWork()
{
$t1 = $this->createMock(TransportInterface::class);
Expand Down
24 changes: 24 additions & 0 deletions src/Symfony/Component/Mailer/Tests/Transport/NullTransportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mailer\Tests\Transport;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Mailer\Transport\NullTransport;

class NullTransportTest extends TestCase
{
public function testName()
{
$t = new NullTransport();
$this->assertEquals('smtp://null', $t->getName());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ public function testSendNoTransports()
new RoundRobinTransport([]);
}

public function testGetName()
{
$t1 = $this->createMock(TransportInterface::class);
$t1->expects($this->once())->method('getName')->willReturn('t1://local');
$t2 = $this->createMock(TransportInterface::class);
$t2->expects($this->once())->method('getName')->willReturn('t2://local');
$t = new RoundRobinTransport([$t1, $t2]);
$this->assertEquals('t1://local && t2://local', $t->getName());
}

public function testSendAlternate()
{
$t1 = $this->createMock(TransportInterface::class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mailer\Tests\Transport;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Mailer\Transport\SendmailTransport;

class SendmailTransportTest extends TestCase
{
public function testName()
{
$t = new SendmailTransport();
$this->assertEquals('smtp://sendmail', $t->getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Mailer\Tests\Transport\Smtp;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Mailer\Transport\Smtp\SmtpTransport;
use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream;

class SmtpTransportTest extends TestCase
{
public function testName()
{
$t = new SmtpTransport();
$this->assertEquals('smtp://localhost:25', $t->getName());

$t = new SmtpTransport((new SocketStream())->setHost('127.0.0.1')->setPort(2525));
$this->assertEquals('smtp://127.0.0.1:2525', $t->getName());
}
}
5 changes: 5 additions & 0 deletions src/Symfony/Component/Mailer/Tests/TransportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public function send(RawMessage $message, SmtpEnvelope $envelope = null): ?SentM
{
throw new \BadMethodCallException('This method newer should be called.');
}

public function getName(): string
{
return sprintf('dummy://local');
}
}

class DummyTransportFactory implements Transport\TransportFactoryInterface
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Mailer/Transport/AbstractTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public function __construct(EventDispatcherInterface $dispatcher = null, LoggerI
$this->logger = $logger ?: new NullLogger();
}

abstract public function getName(): string;

/**
* Sets the maximum number of messages to send per second (0 to disable).
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Mailer/Transport/FailoverTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ protected function getNextTransport(): ?TransportInterface

return $this->currentTransport;
}

protected function getNameSymbol(): string
{
return '||';
}
}
5 changes: 5 additions & 0 deletions src/Symfony/Component/Mailer/Transport/NullTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,9 @@ final class NullTransport extends AbstractTransport
protected function doSend(SentMessage $message): void
{
}

public function getName(): string
{
return 'smtp://null';
}
}
12 changes: 12 additions & 0 deletions src/Symfony/Component/Mailer/Transport/RoundRobinTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ public function send(RawMessage $message, SmtpEnvelope $envelope = null): ?SentM
throw new TransportException('All transports failed.');
}

public function getName(): string
{
return implode(' '.$this->getNameSymbol().' ', array_map(function (TransportInterface $transport) {
return $transport->getName();
}, $this->transports));
}

/**
* Rotates the transport list around and returns the first instance.
*/
Expand Down Expand Up @@ -90,6 +97,11 @@ protected function isTransportDead(TransportInterface $transport): bool
return $this->deadTransports->contains($transport);
}

protected function getNameSymbol(): string
{
return '&&';
}

private function moveCursor(int $cursor): int
{
return ++$cursor >= \count($this->transports) ? 0 : $cursor;
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Mailer/Transport/SendmailTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public function send(RawMessage $message, SmtpEnvelope $envelope = null): ?SentM
return parent::send($message, $envelope);
}

public function getName(): string
{
return $this->transport->getName();
}

protected function doSend(SentMessage $message): void
{
$this->getLogger()->debug(sprintf('Email transport "%s" starting', __CLASS__));
Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/Mailer/Transport/Smtp/SmtpTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,15 @@ public function send(RawMessage $message, SmtpEnvelope $envelope = null): ?SentM
return $message;
}

public function getName(): string
{
if ($this->stream instanceof SocketStream) {
return sprintf('smtp://%s:%d', $this->stream->getHost(), $this->stream->getPort());
}

return sprintf('smtp://sendmail');
}

/**
* Runs a command against the stream, expecting the given response codes.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Mailer/Transport/TransportInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ interface TransportInterface
* @throws TransportExceptionInterface
*/
public function send(RawMessage $message, SmtpEnvelope $envelope = null): ?SentMessage;

public function getName(): string;
}