[e3d4e0a] | 1 | <?php declare(strict_types=1);
|
---|
| 2 |
|
---|
| 3 | /*
|
---|
| 4 | * This file is part of the Monolog package.
|
---|
| 5 | *
|
---|
| 6 | * (c) Jordi Boggiano <j.boggiano@seld.be>
|
---|
| 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 Monolog\Handler;
|
---|
| 13 |
|
---|
| 14 | use Closure;
|
---|
| 15 | use Monolog\Level;
|
---|
| 16 | use Monolog\LogRecord;
|
---|
| 17 | use Monolog\Utils;
|
---|
| 18 | use Monolog\Formatter\FormatterInterface;
|
---|
| 19 | use Monolog\Formatter\LineFormatter;
|
---|
| 20 | use Symfony\Component\Mailer\MailerInterface;
|
---|
| 21 | use Symfony\Component\Mailer\Transport\TransportInterface;
|
---|
| 22 | use Symfony\Component\Mime\Email;
|
---|
| 23 |
|
---|
| 24 | /**
|
---|
| 25 | * SymfonyMailerHandler uses Symfony's Mailer component to send the emails
|
---|
| 26 | *
|
---|
| 27 | * @author Jordi Boggiano <j.boggiano@seld.be>
|
---|
| 28 | */
|
---|
| 29 | class SymfonyMailerHandler extends MailHandler
|
---|
| 30 | {
|
---|
| 31 | protected MailerInterface|TransportInterface $mailer;
|
---|
| 32 | /** @var Email|Closure(string, LogRecord[]): Email */
|
---|
| 33 | private Email|Closure $emailTemplate;
|
---|
| 34 |
|
---|
| 35 | /**
|
---|
| 36 | * @phpstan-param Email|Closure(string, LogRecord[]): Email $email
|
---|
| 37 | *
|
---|
| 38 | * @param MailerInterface|TransportInterface $mailer The mailer to use
|
---|
| 39 | * @param Closure|Email $email An email template, the subject/body will be replaced
|
---|
| 40 | */
|
---|
| 41 | public function __construct($mailer, Email|Closure $email, int|string|Level $level = Level::Error, bool $bubble = true)
|
---|
| 42 | {
|
---|
| 43 | parent::__construct($level, $bubble);
|
---|
| 44 |
|
---|
| 45 | $this->mailer = $mailer;
|
---|
| 46 | $this->emailTemplate = $email;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | /**
|
---|
| 50 | * {@inheritDoc}
|
---|
| 51 | */
|
---|
| 52 | protected function send(string $content, array $records): void
|
---|
| 53 | {
|
---|
| 54 | $this->mailer->send($this->buildMessage($content, $records));
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | /**
|
---|
| 58 | * Gets the formatter for the Swift_Message subject.
|
---|
| 59 | *
|
---|
| 60 | * @param string|null $format The format of the subject
|
---|
| 61 | */
|
---|
| 62 | protected function getSubjectFormatter(?string $format): FormatterInterface
|
---|
| 63 | {
|
---|
| 64 | return new LineFormatter($format);
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | /**
|
---|
| 68 | * Creates instance of Email to be sent
|
---|
| 69 | *
|
---|
| 70 | * @param string $content formatted email body to be sent
|
---|
| 71 | * @param LogRecord[] $records Log records that formed the content
|
---|
| 72 | */
|
---|
| 73 | protected function buildMessage(string $content, array $records): Email
|
---|
| 74 | {
|
---|
| 75 | $message = null;
|
---|
| 76 | if ($this->emailTemplate instanceof Email) {
|
---|
| 77 | $message = clone $this->emailTemplate;
|
---|
| 78 | } elseif (\is_callable($this->emailTemplate)) {
|
---|
| 79 | $message = ($this->emailTemplate)($content, $records);
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | if (!$message instanceof Email) {
|
---|
| 83 | $record = reset($records);
|
---|
| 84 |
|
---|
| 85 | throw new \InvalidArgumentException('Could not resolve message as instance of Email or a callable returning it' . ($record instanceof LogRecord ? Utils::getRecordMessageForException($record) : ''));
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | if (\count($records) > 0) {
|
---|
| 89 | $subjectFormatter = $this->getSubjectFormatter($message->getSubject());
|
---|
| 90 | $message->subject($subjectFormatter->format($this->getHighestRecord($records)));
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | if ($this->isHtmlBody($content)) {
|
---|
| 94 | if (null !== ($charset = $message->getHtmlCharset())) {
|
---|
| 95 | $message->html($content, $charset);
|
---|
| 96 | } else {
|
---|
| 97 | $message->html($content);
|
---|
| 98 | }
|
---|
| 99 | } else {
|
---|
| 100 | if (null !== ($charset = $message->getTextCharset())) {
|
---|
| 101 | $message->text($content, $charset);
|
---|
| 102 | } else {
|
---|
| 103 | $message->text($content);
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | return $message->date(new \DateTimeImmutable());
|
---|
| 108 | }
|
---|
| 109 | }
|
---|