Navigation Menu

Skip to content

Commit

Permalink
bug #31650 Create an abstract HTTP transport and extend it in all HTT…
Browse files Browse the repository at this point in the history
…P transports (bocharsky-bw)

This PR was merged into the 4.3 branch.

Discussion
----------

Create an abstract HTTP transport and extend it in all HTTP transports

| Q             | A
| ------------- | ---
| Branch?       | 4.3
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | no
| License       | MIT
| Doc PR        | no

Right now when you try to use an HTTP transport e.g. Mailgun w/o HTTP client installed - the error message is:
> Attempted to load class "HttpClient" from namespace "Symfony\Component\HttpClient".
Did you forget a "use" statement for "Http\Client\HttpClient"?

Not clear enough about what to do. After this PR the error message will be:
> You cannot use "Symfony\Component\Mailer\Bridge\Mailgun\Http\MailgunTransport" as the HttpClient component is not installed. Try running "composer require symfony/http-client".

Actually, we already have a similar check for API:
https://github.com/symfony/symfony/blob/2c9a1960a164a857cd551881b580266bc77bdafa/src/Symfony/Component/Mailer/Transport/Http/Api/AbstractApiTransport.php#L37-L44

Commits
-------

3c8d63c Create an abstract HTTP transport and extend it in all HTTP transports
  • Loading branch information
fabpot committed May 28, 2019
2 parents 3a508e3 + 3c8d63c commit 59d5a77
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
Expand Up @@ -13,22 +13,20 @@

use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\Transport\AbstractTransport;
use Symfony\Component\Mailer\Transport\Http\AbstractHttpTransport;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author Kevin Verschaeve
*
* @experimental in 4.3
*/
class SesTransport extends AbstractTransport
class SesTransport extends AbstractHttpTransport
{
private const ENDPOINT = 'https://email.%region%.amazonaws.com';

private $client;
private $accessKey;
private $secretKey;
private $region;
Expand All @@ -38,12 +36,11 @@ class SesTransport extends AbstractTransport
*/
public function __construct(string $accessKey, string $secretKey, string $region = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
{
$this->client = $client ?? HttpClient::create();
$this->accessKey = $accessKey;
$this->secretKey = $secretKey;
$this->region = $region ?: 'eu-west-1';

parent::__construct($dispatcher, $logger);
parent::__construct($client, $dispatcher, $logger);
}

protected function doSend(SentMessage $message): void
Expand Down
Expand Up @@ -13,29 +13,26 @@

use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\Transport\AbstractTransport;
use Symfony\Component\Mailer\Transport\Http\AbstractHttpTransport;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author Kevin Verschaeve
*
* @experimental in 4.3
*/
class MandrillTransport extends AbstractTransport
class MandrillTransport extends AbstractHttpTransport
{
private const ENDPOINT = 'https://mandrillapp.com/api/1.0/messages/send-raw.json';
private $client;
private $key;

public function __construct(string $key, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
{
$this->key = $key;
$this->client = $client ?? HttpClient::create();

parent::__construct($dispatcher, $logger);
parent::__construct($client, $dispatcher, $logger);
}

protected function doSend(SentMessage $message): void
Expand Down
Expand Up @@ -13,10 +13,9 @@

use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mailer\Transport\AbstractTransport;
use Symfony\Component\Mailer\Transport\Http\AbstractHttpTransport;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\Part\Multipart\FormDataPart;
use Symfony\Contracts\HttpClient\HttpClientInterface;
Expand All @@ -26,20 +25,18 @@
*
* @experimental in 4.3
*/
class MailgunTransport extends AbstractTransport
class MailgunTransport extends AbstractHttpTransport
{
private const ENDPOINT = 'https://api.mailgun.net/v3/%domain%/messages.mime';
private $key;
private $domain;
private $client;

public function __construct(string $key, string $domain, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
{
$this->key = $key;
$this->domain = $domain;
$this->client = $client ?? HttpClient::create();

parent::__construct($dispatcher, $logger);
parent::__construct($client, $dispatcher, $logger);
}

protected function doSend(SentMessage $message): void
Expand Down
@@ -0,0 +1,42 @@
<?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\Transport\Http;

use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpClient\HttpClient;
use Symfony\Component\Mailer\Transport\AbstractTransport;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author Victor Bocharsky <victor@symfonycasts.com>
*
* @experimental in 4.3
*/
abstract class AbstractHttpTransport extends AbstractTransport
{
protected $client;

public function __construct(HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
{
$this->client = $client;
if (null === $client) {
if (!class_exists(HttpClient::class)) {
throw new \LogicException(sprintf('You cannot use "%s" as the HttpClient component is not installed. Try running "composer require symfony/http-client".', __CLASS__));
}

$this->client = HttpClient::create();
}

parent::__construct($dispatcher, $logger);
}
}

0 comments on commit 59d5a77

Please sign in to comment.