MailerHandler.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\Monolog\Handler;
  11. use Monolog\Formatter\FormatterInterface;
  12. use Monolog\Formatter\HtmlFormatter;
  13. use Monolog\Formatter\LineFormatter;
  14. use Monolog\Handler\AbstractProcessingHandler;
  15. use Monolog\Logger;
  16. use Symfony\Component\Mailer\MailerInterface;
  17. use Symfony\Component\Mime\Email;
  18. /**
  19. * @author Alexander Borisov <boshurik@gmail.com>
  20. */
  21. class MailerHandler extends AbstractProcessingHandler
  22. {
  23. private $mailer;
  24. private $messageTemplate;
  25. /**
  26. * @param callable|Email $messageTemplate
  27. * @param string|int $level The minimum logging level at which this handler will be triggered
  28. */
  29. public function __construct(MailerInterface $mailer, $messageTemplate, $level = Logger::DEBUG, bool $bubble = true)
  30. {
  31. parent::__construct($level, $bubble);
  32. $this->mailer = $mailer;
  33. $this->messageTemplate = $messageTemplate;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function handleBatch(array $records): void
  39. {
  40. $messages = [];
  41. foreach ($records as $record) {
  42. if ($record['level'] < $this->level) {
  43. continue;
  44. }
  45. $messages[] = $this->processRecord($record);
  46. }
  47. if (!empty($messages)) {
  48. $this->send((string) $this->getFormatter()->formatBatch($messages), $messages);
  49. }
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. protected function write(array $record): void
  55. {
  56. $this->send((string) $record['formatted'], [$record]);
  57. }
  58. /**
  59. * Send a mail with the given content.
  60. *
  61. * @param string $content formatted email body to be sent
  62. * @param array $records the array of log records that formed this content
  63. */
  64. protected function send(string $content, array $records)
  65. {
  66. $this->mailer->send($this->buildMessage($content, $records));
  67. }
  68. /**
  69. * Gets the formatter for the Message subject.
  70. *
  71. * @param string $format The format of the subject
  72. */
  73. protected function getSubjectFormatter(string $format): FormatterInterface
  74. {
  75. return new LineFormatter($format);
  76. }
  77. /**
  78. * Creates instance of Message to be sent.
  79. *
  80. * @param string $content formatted email body to be sent
  81. * @param array $records Log records that formed the content
  82. */
  83. protected function buildMessage(string $content, array $records): Email
  84. {
  85. $message = null;
  86. if ($this->messageTemplate instanceof Email) {
  87. $message = clone $this->messageTemplate;
  88. } elseif (\is_callable($this->messageTemplate)) {
  89. $message = \call_user_func($this->messageTemplate, $content, $records);
  90. if (!$message instanceof Email) {
  91. throw new \InvalidArgumentException(sprintf('Could not resolve message from a callable. Instance of "%s" is expected.', Email::class));
  92. }
  93. } else {
  94. throw new \InvalidArgumentException('Could not resolve message as instance of Email or a callable returning it.');
  95. }
  96. if ($records) {
  97. $subjectFormatter = $this->getSubjectFormatter($message->getSubject());
  98. $message->subject($subjectFormatter->format($this->getHighestRecord($records)));
  99. }
  100. if ($this->getFormatter() instanceof HtmlFormatter) {
  101. if ($message->getHtmlCharset()) {
  102. $message->html($content, $message->getHtmlCharset());
  103. } else {
  104. $message->html($content);
  105. }
  106. } else {
  107. if ($message->getTextCharset()) {
  108. $message->text($content, $message->getTextCharset());
  109. } else {
  110. $message->text($content);
  111. }
  112. }
  113. return $message;
  114. }
  115. protected function getHighestRecord(array $records): array
  116. {
  117. $highestRecord = null;
  118. foreach ($records as $record) {
  119. if (null === $highestRecord || $highestRecord['level'] < $record['level']) {
  120. $highestRecord = $record;
  121. }
  122. }
  123. return $highestRecord;
  124. }
  125. }