Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Symfony\Component\Mailer\Transport;
13
 
14
use Symfony\Component\Mailer\Envelope;
15
use Symfony\Component\Mailer\Exception\InvalidArgumentException;
16
use Symfony\Component\Mailer\Exception\LogicException;
17
use Symfony\Component\Mailer\SentMessage;
18
use Symfony\Component\Mime\Message;
19
use Symfony\Component\Mime\RawMessage;
20
 
21
/**
22
 * @author Fabien Potencier <fabien@symfony.com>
23
 */
24
final class Transports implements TransportInterface
25
{
26
    /**
27
     * @var array<string, TransportInterface>
28
     */
29
    private array $transports = [];
30
    private TransportInterface $default;
31
 
32
    /**
33
     * @param iterable<string, TransportInterface> $transports
34
     */
35
    public function __construct(iterable $transports)
36
    {
37
        foreach ($transports as $name => $transport) {
38
            $this->default ??= $transport;
39
            $this->transports[$name] = $transport;
40
        }
41
 
42
        if (!$this->transports) {
43
            throw new LogicException(sprintf('"%s" must have at least one transport configured.', __CLASS__));
44
        }
45
    }
46
 
47
    public function send(RawMessage $message, Envelope $envelope = null): ?SentMessage
48
    {
49
        /** @var Message $message */
50
        if (RawMessage::class === $message::class || !$message->getHeaders()->has('X-Transport')) {
51
            return $this->default->send($message, $envelope);
52
        }
53
 
54
        $headers = $message->getHeaders();
55
        $transport = $headers->get('X-Transport')->getBody();
56
        $headers->remove('X-Transport');
57
 
58
        if (!isset($this->transports[$transport])) {
59
            throw new InvalidArgumentException(sprintf('The "%s" transport does not exist (available transports: "%s").', $transport, implode('", "', array_keys($this->transports))));
60
        }
61
 
62
        try {
63
            return $this->transports[$transport]->send($message, $envelope);
64
        } catch (\Throwable $e) {
65
            $headers->addTextHeader('X-Transport', $transport);
66
 
67
            throw $e;
68
        }
69
    }
70
 
71
    public function __toString(): string
72
    {
73
        return '['.implode(',', array_keys($this->transports)).']';
74
    }
75
}