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

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