|
| 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\Bridge\FakeChat; |
| 13 | + |
| 14 | +use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
| 15 | +use Symfony\Component\Mailer\Exception\TransportExceptionInterface; |
| 16 | +use Symfony\Component\Mailer\MailerInterface; |
| 17 | +use Symfony\Component\Mime\Email; |
| 18 | +use Symfony\Component\Notifier\Exception\UnsupportedMessageTypeException; |
| 19 | +use Symfony\Component\Notifier\Message\ChatMessage; |
| 20 | +use Symfony\Component\Notifier\Message\MessageInterface; |
| 21 | +use Symfony\Component\Notifier\Message\SentMessage; |
| 22 | +use Symfony\Component\Notifier\Transport\AbstractTransport; |
| 23 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 24 | + |
| 25 | +/** |
| 26 | + * @author Oskar Stark <oskarstark@googlemail.com> |
| 27 | + */ |
| 28 | +final class FakeChatEmailTransport extends AbstractTransport |
| 29 | +{ |
| 30 | + private $mailer; |
| 31 | + private $to; |
| 32 | + private $from; |
| 33 | + |
| 34 | + public function __construct(MailerInterface $mailer, string $to, string $from, HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null) |
| 35 | + { |
| 36 | + $this->mailer = $mailer; |
| 37 | + $this->to = $to; |
| 38 | + $this->from = $from; |
| 39 | + |
| 40 | + parent::__construct($client, $dispatcher); |
| 41 | + } |
| 42 | + |
| 43 | + public function __toString(): string |
| 44 | + { |
| 45 | + return sprintf('fakechat+email://%s?to=%s&from=%s', $this->getEndpoint(), $this->to, $this->from); |
| 46 | + } |
| 47 | + |
| 48 | + public function supports(MessageInterface $message): bool |
| 49 | + { |
| 50 | + return $message instanceof ChatMessage; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @param MessageInterface|ChatMessage $message |
| 55 | + * |
| 56 | + * @throws TransportExceptionInterface |
| 57 | + */ |
| 58 | + protected function doSend(MessageInterface $message): SentMessage |
| 59 | + { |
| 60 | + if (!$this->supports($message)) { |
| 61 | + throw new UnsupportedMessageTypeException(__CLASS__, ChatMessage::class, $message); |
| 62 | + } |
| 63 | + |
| 64 | + $email = (new Email()) |
| 65 | + ->from($this->from) |
| 66 | + ->to($this->to) |
| 67 | + ->subject(sprintf('New Chat message for recipient: %s', $message->getRecipientId())) |
| 68 | + ->html($message->getSubject()) |
| 69 | + ->text($message->getSubject()); |
| 70 | + |
| 71 | + $this->mailer->send($email); |
| 72 | + |
| 73 | + return new SentMessage($message, (string) $this); |
| 74 | + } |
| 75 | +} |
0 commit comments