Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
4 / 6
CRAP
90.32% covered (success)
90.32%
28 / 31
AbstractTransport
0.00% covered (danger)
0.00%
0 / 1
66.67% covered (warning)
66.67%
4 / 6
15.20
90.32% covered (success)
90.32%
28 / 31
 __construct
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
3 / 3
 setMaxPerSecond
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
5 / 5
 send
0.00% covered (danger)
0.00%
0 / 1
5.01
91.67% covered (success)
91.67%
11 / 12
 doSend
n/a
0 / 0
1
n/a
0 / 0
 stringifyAddresses
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 getLogger
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 checkThrottling
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
8 / 8
<?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\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 = LegacyEventDispatcherProxy::decorate($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();
        }
        if (!$envelope->getRecipients()) {
            return null;
        }
        $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);
    }
}