123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\Mailer\Transport;
- use Psr\Log\LoggerInterface;
- use Psr\Log\NullLogger;
- use Symfony\Component\EventDispatcher\Event;
- use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
- use Symfony\Component\Mailer\Envelope;
- use Symfony\Component\Mailer\Event\MessageEvent;
- use Symfony\Component\Mailer\SentMessage;
- use Symfony\Component\Mime\Address;
- use Symfony\Component\Mime\RawMessage;
- use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
- /**
- * @author Fabien Potencier <fabien@symfony.com>
- */
- abstract class AbstractTransport implements TransportInterface
- {
- private $dispatcher;
- private $logger;
- private $rate = 0;
- private $lastSent = 0;
- public function __construct(EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
- {
- $this->dispatcher = class_exists(Event::class) ? LegacyEventDispatcherProxy::decorate($dispatcher) : $dispatcher;
- $this->logger = $logger ?: new NullLogger();
- }
- /**
- * Sets the maximum number of messages to send per second (0 to disable).
- */
- public function setMaxPerSecond(float $rate): self
- {
- if (0 >= $rate) {
- $rate = 0;
- }
- $this->rate = $rate;
- $this->lastSent = 0;
- return $this;
- }
- public function send(RawMessage $message, Envelope $envelope = null): ?SentMessage
- {
- $message = clone $message;
- $envelope = null !== $envelope ? clone $envelope : Envelope::create($message);
- if (null !== $this->dispatcher) {
- $event = new MessageEvent($message, $envelope, (string) $this);
- $this->dispatcher->dispatch($event);
- $envelope = $event->getEnvelope();
- }
- $message = new SentMessage($message, $envelope);
- $this->doSend($message);
- $this->checkThrottling();
- return $message;
- }
- abstract protected function doSend(SentMessage $message): void;
- /**
- * @param Address[] $addresses
- *
- * @return string[]
- */
- protected function stringifyAddresses(array $addresses): array
- {
- return array_map(function (Address $a) {
- return $a->toString();
- }, $addresses);
- }
- protected function getLogger(): LoggerInterface
- {
- return $this->logger;
- }
- private function checkThrottling()
- {
- if (0 == $this->rate) {
- return;
- }
- $sleep = (1 / $this->rate) - (microtime(true) - $this->lastSent);
- if (0 < $sleep) {
- $this->logger->debug(sprintf('Email transport "%s" sleeps for %.2f seconds', __CLASS__, $sleep));
- usleep($sleep * 1000000);
- }
- $this->lastSent = microtime(true);
- }
- }
|