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

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