Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 27
EmailChannel
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 3
272
0.00% covered (danger)
0.00%
0 / 27
 __construct
0.00% covered (danger)
0.00%
0 / 1
30
0.00% covered (danger)
0.00%
0 / 6
 notify
0.00% covered (danger)
0.00%
0 / 1
110
0.00% covered (danger)
0.00%
0 / 20
 supports
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
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
12namespace Symfony\Component\Notifier\Channel;
13
14use Symfony\Component\Mailer\Envelope;
15use Symfony\Component\Mailer\Messenger\SendEmailMessage;
16use Symfony\Component\Mailer\Transport\TransportInterface;
17use Symfony\Component\Messenger\MessageBusInterface;
18use Symfony\Component\Mime\Email;
19use Symfony\Component\Notifier\Exception\LogicException;
20use Symfony\Component\Notifier\Message\EmailMessage;
21use Symfony\Component\Notifier\Notification\EmailNotificationInterface;
22use Symfony\Component\Notifier\Notification\Notification;
23use Symfony\Component\Notifier\Recipient\Recipient;
24
25/**
26 * @author Fabien Potencier <fabien@symfony.com>
27 *
28 * @experimental in 5.1
29 */
30class EmailChannel implements ChannelInterface
31{
32    private $transport;
33    private $bus;
34    private $from;
35    private $envelope;
36
37    public function __construct(TransportInterface $transport = null, MessageBusInterface $bus = null, string $from = null, Envelope $envelope = null)
38    {
39        if (null === $transport && null === $bus) {
40            throw new LogicException(sprintf('"%s" needs a Transport or a Bus but both cannot be "null".', static::class));
41        }
42
43        $this->transport = $transport;
44        $this->bus = $bus;
45        $this->from = $from ?: ($envelope ? $envelope->getSender() : null);
46        $this->envelope = $envelope;
47    }
48
49    public function notify(Notification $notification, Recipient $recipient, string $transportName = null): void
50    {
51        $message = null;
52        if ($notification instanceof EmailNotificationInterface) {
53            $message = $notification->asEmailMessage($recipient, $transportName);
54        }
55
56        $message = $message ?: EmailMessage::fromNotification($notification, $recipient, $transportName);
57        $email = $message->getMessage();
58        if ($email instanceof Email) {
59            if (!$email->getFrom()) {
60                if (null === $this->from) {
61                    throw new LogicException(sprintf('To send the "%s" notification by email, you should either configure a global "from" or set it in the "asEmailMessage()" method.', get_debug_type($notification)));
62                }
63
64                $email->from($this->from);
65            }
66
67            if (!$email->getTo()) {
68                $email->to($recipient->getEmail());
69            }
70        }
71
72        if (null !== $this->envelope) {
73            $message->envelope($this->envelope);
74        }
75
76        if (null !== $transportName) {
77            $message->transport($transportName);
78        }
79
80        if (null === $this->bus) {
81            $this->transport->send($message->getMessage(), $message->getEnvelope());
82        } else {
83            $this->bus->dispatch(new SendEmailMessage($message->getMessage(), $message->getEnvelope()));
84        }
85    }
86
87    public function supports(Notification $notification, Recipient $recipient): bool
88    {
89        return '' !== $recipient->getEmail();
90    }
91}