Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
33.33% covered (danger)
33.33%
1 / 3
CRAP
88.24% covered (warning)
88.24%
15 / 17
Transports
0.00% covered (danger)
0.00%
0 / 1
33.33% covered (danger)
33.33%
1 / 3
10.16
88.24% covered (warning)
88.24%
15 / 17
 __construct
0.00% covered (danger)
0.00%
0 / 1
4.03
87.50% covered (warning)
87.50%
7 / 8
 send
100.00% covered (success)
100.00%
1 / 1
5
100.00% covered (success)
100.00%
8 / 8
 __toString
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
<?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 Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\Exception\InvalidArgumentException;
use Symfony\Component\Mailer\Exception\LogicException;
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mime\Message;
use Symfony\Component\Mime\RawMessage;
/**
 * @author Fabien Potencier <fabien@symfony.com>
 */
final class Transports implements TransportInterface
{
    private $transports;
    private $default;
    /**
     * @param TransportInterface[] $transports
     */
    public function __construct(iterable $transports)
    {
        $this->transports = [];
        foreach ($transports as $name => $transport) {
            if (null === $this->default) {
                $this->default = $transport;
            }
            $this->transports[$name] = $transport;
        }
        if (!$this->transports) {
            throw new LogicException(sprintf('"%s" must have at least one transport configured.', __CLASS__));
        }
    }
    public function send(RawMessage $message, Envelope $envelope = null): ?SentMessage
    {
        /** @var Message $message */
        if (RawMessage::class === \get_class($message) || !$message->getHeaders()->has('X-Transport')) {
            return $this->default->send($message, $envelope);
        }
        $headers = $message->getHeaders();
        $transport = $headers->get('X-Transport')->getBody();
        $headers->remove('X-Transport');
        if (!isset($this->transports[$transport])) {
            throw new InvalidArgumentException(sprintf('The "%s" transport does not exist (available transports: "%s").', $transport, implode('", "', array_keys($this->transports))));
        }
        return $this->transports[$transport]->send($message, $envelope);
    }
    public function __toString(): string
    {
        return '['.implode(',', array_keys($this->transports)).']';
    }
}