| 148 |
lars |
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;
|
|
|
13 |
|
|
|
14 |
use Psr\EventDispatcher\EventDispatcherInterface;
|
|
|
15 |
use Psr\Log\LoggerInterface;
|
|
|
16 |
use Symfony\Component\HttpClient\HttpClient;
|
|
|
17 |
use Symfony\Component\Mailer\Exception\HttpTransportException;
|
|
|
18 |
use Symfony\Component\Mailer\SentMessage;
|
|
|
19 |
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|
|
20 |
use Symfony\Contracts\HttpClient\ResponseInterface;
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* @author Victor Bocharsky <victor@symfonycasts.com>
|
|
|
24 |
*/
|
|
|
25 |
abstract class AbstractHttpTransport extends AbstractTransport
|
|
|
26 |
{
|
|
|
27 |
protected $host;
|
|
|
28 |
protected $port;
|
|
|
29 |
protected $client;
|
|
|
30 |
|
|
|
31 |
public function __construct(HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
|
|
|
32 |
{
|
|
|
33 |
$this->client = $client;
|
|
|
34 |
if (null === $client) {
|
|
|
35 |
if (!class_exists(HttpClient::class)) {
|
|
|
36 |
throw new \LogicException(sprintf('You cannot use "%s" as the HttpClient component is not installed. Try running "composer require symfony/http-client".', __CLASS__));
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
$this->client = HttpClient::create();
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
parent::__construct($dispatcher, $logger);
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* @return $this
|
|
|
47 |
*/
|
|
|
48 |
public function setHost(?string $host): static
|
|
|
49 |
{
|
|
|
50 |
$this->host = $host;
|
|
|
51 |
|
|
|
52 |
return $this;
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
/**
|
|
|
56 |
* @return $this
|
|
|
57 |
*/
|
|
|
58 |
public function setPort(?int $port): static
|
|
|
59 |
{
|
|
|
60 |
$this->port = $port;
|
|
|
61 |
|
|
|
62 |
return $this;
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
abstract protected function doSendHttp(SentMessage $message): ResponseInterface;
|
|
|
66 |
|
|
|
67 |
protected function doSend(SentMessage $message): void
|
|
|
68 |
{
|
|
|
69 |
$response = null;
|
|
|
70 |
try {
|
|
|
71 |
$response = $this->doSendHttp($message);
|
|
|
72 |
$message->appendDebug($response->getInfo('debug') ?? '');
|
|
|
73 |
} catch (HttpTransportException $e) {
|
|
|
74 |
$e->appendDebug($e->getResponse()->getInfo('debug') ?? '');
|
|
|
75 |
|
|
|
76 |
throw $e;
|
|
|
77 |
}
|
|
|
78 |
}
|
|
|
79 |
}
|