8000 Create an abstract HTTP transport and extend it in all HTTP transports · symfony/symfony@3c8d63c · GitHub
[go: up one dir, main page]

Skip to content

Commit 3c8d63c

Browse files
committed
Create an abstract HTTP transport and extend it in all HTTP transports
1 parent 08d9d43 commit 3c8d63c

File tree

4 files changed

+51
-18
lines changed

4 files changed

+51
-18
lines changed

src/Symfony/Component/Mailer/Bridge/Amazon/Http/SesTransport.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,20 @@
1313

1414
use Psr\Log\LoggerInterface;
1515
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16-
use Symfony\Component\HttpClient\HttpClient;
1716
use Symfony\Component\Mailer\Exception\TransportException;
1817
use Symfony\Component\Mailer\SentMessage;
19-
use Symfony\Component\Mailer\Transport\AbstractTransport;
18+
use Symfony\Component\Mailer\Transport\Http\AbstractHttpTransport;
2019
use Symfony\Contracts\HttpClient\HttpClientInterface;
2120

2221
/**
2322
* @author Kevin Verschaeve
2423
*
2524
* @experimental in 4.3
2625
*/
27-
class SesTransport extends AbstractTransport
26+
class SesTransport extends AbstractHttpTransport
2827
{
2928
private const ENDPOINT = 'https://email.%region%.amazonaws.com';
3029

31-
private $client;
3230
private $accessKey;
3331
private $secretKey;
3432
private $region;
@@ -38,12 +36,11 @@ class SesTransport extends AbstractTransport
3836
*/
3937
public function __construct(string $accessKey, string $secretKey, string $region = null, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
4038
{
41-
$this->client = $client ?? HttpClient::create();
4239
$this->accessKey = $accessKey;
4340
$this->secretKey = $secretKey;
4441
$this->region = $region ?: 'eu-west-1';
4542

46-
parent::__construct($dispatcher, $logger);
43+
parent::__construct($client, $dispatcher, $logger);
4744
}
4845

4946
protected function doSend(SentMessage $message): void

src/Symfony/Component/Mailer/Bridge/Mailchimp/Http/MandrillTransport.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,26 @@
1313

1414
use Psr\Log\LoggerInterface;
1515
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16-
use Symfony\Component\HttpClient\HttpClient;
1716
use Symfony\Component\Mailer\Exception\TransportException;
1817
use Symfony\Component\Mailer\SentMessage;
19-
use Symfony\Component\Mailer\Transport\AbstractTransport;
18+
use Symfony\Component\Mailer\Transport\Http\AbstractHttpTransport;
2019
use Symfony\Contracts\HttpClient\HttpClientInterface;
2120

2221
/**
2322
* @author Kevin Verschaeve
2423
*
2524
* @experimental in 4.3
2625
*/
27-
class MandrillTransport extends AbstractTransport
26+
class MandrillTransport extends AbstractHttpTransport
2827
{
2928
private const ENDPOINT = 'https://mandrillapp.com/api/1.0/messages/send-raw.json';
30-
private $client;
3129
private $key;
3230

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

38-
parent::__construct($dispatcher, $logger);
35+
parent::__construct($client, $dispatcher, $logger);
3936
}
4037

4138
protected function doSend(SentMessage $message): void

src/Symfony/Component/Mailer/Bridge/Mailgun/Http/MailgunTransport.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313

1414
use Psr\Log\LoggerInterface;
1515
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16-
use Symfony\Component\HttpClient\HttpClient;
1716
use Symfony\Component\Mailer\Exception\TransportException;
1817
use Symfony\Component\Mailer\SentMessage;
19-
use Symfony\Component\Mailer\Transport\AbstractTransport;
18+
use Symfony\Component\Mailer\Transport\Http\AbstractHttpTransport;
2019
use Symfony\Component\Mime\Part\DataPart;
2120
use Symfony\Component\Mime\Part\Multipart\FormDataPart;
2221
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -26,20 +25,18 @@
2625
*
2726
* @experimental in 4.3
2827
*/
29-
class MailgunTransport extends AbstractTransport
28+
class MailgunTransport extends AbstractHttpTransport
3029
{
3130
private const ENDPOINT = 'https://api.mailgun.net/v3/%domain%/messages.mime';
3231
private $key;
3332
private $domain;
34-
private $client;
3533

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

42-
parent::__construct($dispatcher, $logger);
39+
parent::__construct($client, $dispatcher, $logger);
4340
}
4441

4542
protected function doSend(SentMessage $message): void
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Mailer\Transport\Http;
13+
14+
use Psr\Log\LoggerInterface;
15+
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
16+
use Symfony\Component\HttpClient\HttpClient;
17+
use Symfony\Component\Mailer\Transport\AbstractTransport;
18+
use Symfony\Contracts\HttpClient\HttpClientInterface;
19+
20+
/**
21+
* @author Victor Bocharsky <victor@symfonycasts.com>
22+
*
23+
* @experimental in 4.3
24+
*/
25+
abstract class AbstractHttpTransport extends AbstractTransport
26+
{
27+
protected $client;
28+
29+
public function __construct(HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
30+
{
31+
$this->client = $client;
32+
if (null === $client) {
33+
if (!class_exists(HttpClient::class)) {
34+
throw new \LogicException(sprintf('You cannot use "%s" as the HttpClient component is not installed. Try running "composer require symfony/http-client".', __CLASS__));
35+
}
36+
37+
$this->client = HttpClient::create();
38+
}
39+
40+
parent::__construct($dispatcher, $logger);
41+
}
42+
}

0 commit comments

Comments
 (0)
0