Repository: symfony/symfony
File: src/Symfony/Component/Mailer/Transport/RoundRobinTransport.php (PHP)

Code before:
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mime\RawMessage;

/**
 * Uses several Transports using a round robin algorithm.
 *
 * @author Fabien Potencier <fabien@symfony.com>
 */
class RoundRobinTransport implements TransportInterface
{
    private LoggerInterface $logger;
    /**
     * @var \SplObjectStorage<TransportInterface, float>
     */
    private \SplObjectStorage $deadTransports;
    private int $cursor = -1;

    /**
     * @param TransportInterface[] $transports
     */
    public function __construct(
        private array $transports,
        private int $retryPeriod = 60,
        ?LoggerInterface $logger = null,
    ) {
        if (!$transports) {
            throw new TransportException(\sprintf('"%s" must have at least one transport configured.', static::class));
        }

        $this->logger = $logger ?? new NullLogger();
        $this->deadTransports = new \SplObjectStorage();
    }

    public function send(RawMessage $message, ?Envelope $envelope = null): ?SentMessage
    {
        $exception = null;

        while ($transport = $this->getNextTransport()) {
            try {
                return $transport->send(clone $message, $envelope);
            } catch (TransportExceptionInterface $e) {
                $exception ??= new TransportException('All transports failed.');
                $exception->appendDebug(\sprintf("Transport \"%s\": %s\n", $transport, $e->getDebug()));
                $this->logger->error(\sprintf("Transport \"%s\" failed.", $transport), ['exception' => $e]);
                $this->deadTransports[$transport] = microtime(true);
            }
        }

        throw $exception ?? new TransportException('No transports found.');

Review comment (suggestion):
```suggestion
        private LoggerInterface $logger = new NullLogger(),
```

Code after:
use Symfony\Component\Mailer\Exception\TransportException;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mime\RawMessage;

/**
 * Uses several Transports using a round robin algorithm.
 *
 * @author Fabien Potencier <fabien@symfony.com>
 */
class RoundRobinTransport implements TransportInterface
{
    /**
     * @var \SplObjectStorage<TransportInterface, float>
     */
    private \SplObjectStorage $deadTransports;
    private int $cursor = -1;

    /**
     * @param TransportInterface[] $transports
     */
    public function __construct(
        private array $transports,
        private int $retryPeriod = 60,
        private LoggerInterface $logger = new NullLogger(),
    ) {
        if (!$transports) {
            throw new TransportException(\sprintf('"%s" must have at least one transport configured.', static::class));
        }

        $this->deadTransports = new \SplObjectStorage();
    }

    public function send(RawMessage $message, ?Envelope $envelope = null): ?SentMessage
    {
        $exception = null;

        while ($transport = $this->getNextTransport()) {
            try {
                return $transport->send(clone $message, $envelope);
            } catch (TransportExceptionInterface $e) {
                $exception ??= new TransportException('All transports failed.');
                $exception->appendDebug(\sprintf("Transport \"%s\": %s\n", $transport, $e->getDebug()));
                $this->logger->error(\sprintf("Transport \"%s\" failed.", $transport), ['exception' => $e]);
                $this->deadTransports[$transport] = microtime(true);
            }
        }

        throw $exception ?? new TransportException('No transports found.');
    }