Skip to content

Commit

Permalink
Create an abstract HTTP transport and extend it in all HTTP transports
Browse files Browse the repository at this point in the history
  • Loading branch information
bocharsky-bw committed May 28, 2019
1 parent 08d9d43 commit 3c8d63c
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 3c8d63c

Please sign in to comment.