Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 59d5a77

Browse filesBrowse files
committed
bug #31650 Create an abstract HTTP transport and extend it in all HTTP 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
2 parents 3a508e3 + 3c8d63c commit 59d5a77
Copy full SHA for 59d5a77

File tree

Expand file treeCollapse file tree

4 files changed

+51
-18
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+51
-18
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Mailer/Bridge/Amazon/Http/SesTransport.php
+3-6Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Mailer/Bridge/Mailchimp/Http/MandrillTransport.php
+3-6Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Mailer/Bridge/Mailgun/Http/MailgunTransport.php
+3-6Lines 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
+42Lines 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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.