vendor/monolog/monolog/src/Monolog/Handler/MailHandler.php line 60

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * This file is part of the Monolog package.
  4.  *
  5.  * (c) Jordi Boggiano <j.boggiano@seld.be>
  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 Monolog\Handler;
  11. use Monolog\Formatter\FormatterInterface;
  12. use Monolog\Formatter\HtmlFormatter;
  13. /**
  14.  * Base class for all mail handlers
  15.  *
  16.  * @author Gyula Sallai
  17.  *
  18.  * @phpstan-import-type Record from \Monolog\Logger
  19.  */
  20. abstract class MailHandler extends AbstractProcessingHandler
  21. {
  22.     /**
  23.      * {@inheritDoc}
  24.      */
  25.     public function handleBatch(array $records): void
  26.     {
  27.         $messages = [];
  28.         foreach ($records as $record) {
  29.             if ($record['level'] < $this->level) {
  30.                 continue;
  31.             }
  32.             /** @var Record $message */
  33.             $message $this->processRecord($record);
  34.             $messages[] = $message;
  35.         }
  36.         if (!empty($messages)) {
  37.             $this->send((string) $this->getFormatter()->formatBatch($messages), $messages);
  38.         }
  39.     }
  40.     /**
  41.      * Send a mail with the given content
  42.      *
  43.      * @param string $content formatted email body to be sent
  44.      * @param array  $records the array of log records that formed this content
  45.      *
  46.      * @phpstan-param Record[] $records
  47.      */
  48.     abstract protected function send(string $content, array $records): void;
  49.     /**
  50.      * {@inheritDoc}
  51.      */
  52.     protected function write(array $record): void
  53.     {
  54.         $this->send((string) $record['formatted'], [$record]);
  55.     }
  56.     /**
  57.      * @phpstan-param non-empty-array<Record> $records
  58.      * @phpstan-return Record
  59.      */
  60.     protected function getHighestRecord(array $records): array
  61.     {
  62.         $highestRecord null;
  63.         foreach ($records as $record) {
  64.             if ($highestRecord === null || $highestRecord['level'] < $record['level']) {
  65.                 $highestRecord $record;
  66.             }
  67.         }
  68.         return $highestRecord;
  69.     }
  70.     protected function isHtmlBody(string $body): bool
  71.     {
  72.         return ($body[0] ?? null) === '<';
  73.     }
  74.     /**
  75.      * Gets the default formatter.
  76.      *
  77.      * @return FormatterInterface
  78.      */
  79.     protected function getDefaultFormatter(): FormatterInterface
  80.     {
  81.         return new HtmlFormatter();
  82.     }
  83. }