AbstractHttpTransport.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Mailer\Transport;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\HttpClient\HttpClient;
  13. use Symfony\Component\Mailer\Exception\HttpTransportException;
  14. use Symfony\Component\Mailer\SentMessage;
  15. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  16. use Symfony\Contracts\HttpClient\HttpClientInterface;
  17. use Symfony\Contracts\HttpClient\ResponseInterface;
  18. /**
  19. * @author Victor Bocharsky <victor@symfonycasts.com>
  20. */
  21. abstract class AbstractHttpTransport extends AbstractTransport
  22. {
  23. protected $host;
  24. protected $port;
  25. protected $client;
  26. public function __construct(HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
  27. {
  28. $this->client = $client;
  29. if (null === $client) {
  30. if (!class_exists(HttpClient::class)) {
  31. throw new \LogicException(sprintf('You cannot use "%s" as the HttpClient component is not installed. Try running "composer require symfony/http-client".', __CLASS__));
  32. }
  33. $this->client = HttpClient::create();
  34. }
  35. parent::__construct($dispatcher, $logger);
  36. }
  37. /**
  38. * @return $this
  39. */
  40. public function setHost(?string $host)
  41. {
  42. $this->host = $host;
  43. return $this;
  44. }
  45. /**
  46. * @return $this
  47. */
  48. public function setPort(?int $port)
  49. {
  50. $this->port = $port;
  51. return $this;
  52. }
  53. abstract protected function doSendHttp(SentMessage $message): ResponseInterface;
  54. protected function doSend(SentMessage $message): void
  55. {
  56. $response = null;
  57. try {
  58. $response = $this->doSendHttp($message);
  59. $message->appendDebug($response->getInfo('debug') ?? '');
  60. } catch (HttpTransportException $e) {
  61. $e->appendDebug($e->getResponse()->getInfo('debug') ?? '');
  62. throw $e;
  63. }
  64. }
  65. }