Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Zur aktuellen Revision | Details | Vergleich mit vorheriger | 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;
13
 
14
use Psr\EventDispatcher\EventDispatcherInterface;
15
use Symfony\Component\Mailer\Event\MessageEvent;
16
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
17
use Symfony\Component\Mailer\Messenger\SendEmailMessage;
18
use Symfony\Component\Mailer\Transport\TransportInterface;
19
use Symfony\Component\Messenger\Exception\HandlerFailedException;
20
use Symfony\Component\Messenger\MessageBusInterface;
21
use Symfony\Component\Mime\RawMessage;
22
 
23
/**
24
 * @author Fabien Potencier <fabien@symfony.com>
25
 */
26
final class Mailer implements MailerInterface
27
{
28
    private TransportInterface $transport;
29
    private ?MessageBusInterface $bus;
30
    private ?EventDispatcherInterface $dispatcher;
31
 
32
    public function __construct(TransportInterface $transport, MessageBusInterface $bus = null, EventDispatcherInterface $dispatcher = null)
33
    {
34
        $this->transport = $transport;
35
        $this->bus = $bus;
36
        $this->dispatcher = $dispatcher;
37
    }
38
 
39
    public function send(RawMessage $message, Envelope $envelope = null): void
40
    {
41
        if (null === $this->bus) {
42
            $this->transport->send($message, $envelope);
43
 
44
            return;
45
        }
46
 
47
        $stamps = [];
48
        if (null !== $this->dispatcher) {
49
            // The dispatched event here has `queued` set to `true`; the goal is NOT to render the message, but to let
50
            // listeners do something before a message is sent to the queue.
51
            // We are using a cloned message as we still want to dispatch the **original** message, not the one modified by listeners.
52
            // That's because the listeners will run again when the email is sent via Messenger by the transport (see `AbstractTransport`).
53
            // Listeners should act depending on the `$queued` argument of the `MessageEvent` instance.
54
            $clonedMessage = clone $message;
55
            $clonedEnvelope = null !== $envelope ? clone $envelope : Envelope::create($clonedMessage);
56
            $event = new MessageEvent($clonedMessage, $clonedEnvelope, (string) $this->transport, true);
57
            $this->dispatcher->dispatch($event);
58
            $stamps = $event->getStamps();
150 lars 59
 
60
            if ($event->isRejected()) {
61
                return;
62
            }
148 lars 63
        }
64
 
65
        try {
66
            $this->bus->dispatch(new SendEmailMessage($message, $envelope), $stamps);
67
        } catch (HandlerFailedException $e) {
68
            foreach ($e->getNestedExceptions() as $nested) {
69
                if ($nested instanceof TransportExceptionInterface) {
70
                    throw $nested;
71
                }
72
            }
73
            throw $e;
74
        }
75
    }
76
}