vendor/symfony/mailer/Transport/AbstractTransport.php line 59

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\Mailer\Transport;
  11. use Psr\EventDispatcher\EventDispatcherInterface;
  12. use Psr\Log\LoggerInterface;
  13. use Psr\Log\NullLogger;
  14. use Symfony\Component\EventDispatcher\Event;
  15. use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
  16. use Symfony\Component\Mailer\Envelope;
  17. use Symfony\Component\Mailer\Event\MessageEvent;
  18. use Symfony\Component\Mailer\SentMessage;
  19. use Symfony\Component\Mime\Address;
  20. use Symfony\Component\Mime\RawMessage;
  21. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface as SymfonyEventDispatcherInterface;
  22. /**
  23.  * @author Fabien Potencier <fabien@symfony.com>
  24.  */
  25. abstract class AbstractTransport implements TransportInterface
  26. {
  27.     private $dispatcher;
  28.     private $logger;
  29.     private $rate 0;
  30.     private $lastSent 0;
  31.     public function __construct(?EventDispatcherInterface $dispatcher null, ?LoggerInterface $logger null)
  32.     {
  33.         $this->dispatcher class_exists(Event::class) && $dispatcher instanceof SymfonyEventDispatcherInterface LegacyEventDispatcherProxy::decorate($dispatcher) : $dispatcher;
  34.         $this->logger $logger ?? new NullLogger();
  35.     }
  36.     /**
  37.      * Sets the maximum number of messages to send per second (0 to disable).
  38.      *
  39.      * @return $this
  40.      */
  41.     public function setMaxPerSecond(float $rate): self
  42.     {
  43.         if (>= $rate) {
  44.             $rate 0;
  45.         }
  46.         $this->rate $rate;
  47.         $this->lastSent 0;
  48.         return $this;
  49.     }
  50.     public function send(RawMessage $message, ?Envelope $envelope null): ?SentMessage
  51.     {
  52.         $message = clone $message;
  53.         $envelope null !== $envelope ? clone $envelope Envelope::create($message);
  54.         if (null !== $this->dispatcher) {
  55.             $event = new MessageEvent($message$envelope, (string) $this);
  56.             $this->dispatcher->dispatch($event);
  57.             $envelope $event->getEnvelope();
  58.             $message $event->getMessage();
  59.         }
  60.         $message = new SentMessage($message$envelope);
  61.         $this->doSend($message);
  62.         $this->checkThrottling();
  63.         return $message;
  64.     }
  65.     abstract protected function doSend(SentMessage $message): void;
  66.     /**
  67.      * @param Address[] $addresses
  68.      *
  69.      * @return string[]
  70.      */
  71.     protected function stringifyAddresses(array $addresses): array
  72.     {
  73.         return array_map(function (Address $a) {
  74.             return $a->toString();
  75.         }, $addresses);
  76.     }
  77.     protected function getLogger(): LoggerInterface
  78.     {
  79.         return $this->logger;
  80.     }
  81.     private function checkThrottling()
  82.     {
  83.         if (== $this->rate) {
  84.             return;
  85.         }
  86.         $sleep = ($this->rate) - (microtime(true) - $this->lastSent);
  87.         if ($sleep) {
  88.             $this->logger->debug(sprintf('Email transport "%s" sleeps for %.2f seconds'__CLASS__$sleep));
  89.             usleep((int) ($sleep 1000000));
  90.         }
  91.         $this->lastSent microtime(true);
  92.     }
  93. }