vendor/symfony/notifier/Transport/AbstractTransport.php line 37

Open in your IDE?
  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\Notifier\Transport;
  11. use Symfony\Component\HttpClient\HttpClient;
  12. use Symfony\Component\Notifier\Event\FailedMessageEvent;
  13. use Symfony\Component\Notifier\Event\MessageEvent;
  14. use Symfony\Component\Notifier\Event\SentMessageEvent;
  15. use Symfony\Component\Notifier\Exception\LogicException;
  16. use Symfony\Component\Notifier\Message\MessageInterface;
  17. use Symfony\Component\Notifier\Message\SentMessage;
  18. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  19. use Symfony\Contracts\HttpClient\HttpClientInterface;
  20. /**
  21.  * @author Fabien Potencier <fabien@symfony.com>
  22.  */
  23. abstract class AbstractTransport implements TransportInterface
  24. {
  25.     protected const HOST 'localhost';
  26.     private ?EventDispatcherInterface $dispatcher;
  27.     protected $client;
  28.     protected $host;
  29.     protected $port;
  30.     public function __construct(HttpClientInterface $client nullEventDispatcherInterface $dispatcher null)
  31.     {
  32.         $this->client $client;
  33.         if (null === $client) {
  34.             if (!class_exists(HttpClient::class)) {
  35.                 throw new LogicException(sprintf('You cannot use "%s" as the HttpClient component is not installed. Try running "composer require symfony/http-client".'__CLASS__));
  36.             }
  37.             $this->client HttpClient::create();
  38.         }
  39.         $this->dispatcher $dispatcher;
  40.     }
  41.     /**
  42.      * @return $this
  43.      */
  44.     public function setHost(?string $host): static
  45.     {
  46.         $this->host $host;
  47.         return $this;
  48.     }
  49.     /**
  50.      * @return $this
  51.      */
  52.     public function setPort(?int $port): static
  53.     {
  54.         $this->port $port;
  55.         return $this;
  56.     }
  57.     public function send(MessageInterface $message): SentMessage
  58.     {
  59.         if (null === $this->dispatcher) {
  60.             return $this->doSend($message);
  61.         }
  62.         $this->dispatcher->dispatch(new MessageEvent($message));
  63.         try {
  64.             $sentMessage $this->doSend($message);
  65.         } catch (\Throwable $error) {
  66.             $this->dispatcher->dispatch(new FailedMessageEvent($message$error));
  67.             throw $error;
  68.         }
  69.         $this->dispatcher->dispatch(new SentMessageEvent($sentMessage));
  70.         return $sentMessage;
  71.     }
  72.     abstract protected function doSend(MessageInterface $message): SentMessage;
  73.     protected function getEndpoint(): string
  74.     {
  75.         return ($this->host ?: $this->getDefaultHost()).($this->port ':'.$this->port '');
  76.     }
  77.     protected function getDefaultHost(): string
  78.     {
  79.         return static::HOST;
  80.     }
  81. }