|
| 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\Notifier\Channel; |
| 13 | + |
| 14 | +use Symfony\Component\Mailer\Envelope; |
| 15 | +use Symfony\Component\Mailer\Messenger\SendEmailMessage; |
| 16 | +use Symfony\Component\Mailer\Transport\TransportInterface; |
| 17 | +use Symfony\Component\Messenger\MessageBusInterface; |
| 18 | +use Symfony\Component\Mime\Email; |
| 19 | +use Symfony\Component\Notifier\Exception\LogicException; |
| 20 | +use Symfony\Component\Notifier\Message\EmailMessage; |
| 21 | +use Symfony\Component\Notifier\Notification\EmailNotificationInterface; |
| 22 | +use Symfony\Component\Notifier\Notification\Notification; |
| 23 | +use Symfony\Component\Notifier\Recipient\Recipient; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author Fabien Potencier <fabien@symfony.com> |
| 27 | + * |
| 28 | + * @experimental in 5.0 |
| 29 | + */ |
| 30 | +class 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_class($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 | +} |
0 commit comments