media-cloud-sync
/
includes
/
sdk
/
google
/
monolog
/
monolog
/
src
/
Monolog
/
Handler
/
MailHandler.php
MailHandler.php in Media Cloud Sync 1.4.2, at includes/sdk/google/monolog/monolog/src/Monolog/Handler/MailHandler.php
| 1 | <?php |
| 2 | |
| 3 | declare (strict_types=1); |
| 4 | /* |
| 5 | * This file is part of the Monolog package. |
| 6 | * |
| 7 | * (c) Jordi Boggiano <[email protected]> |
| 8 | * |
| 9 | * For the full copyright and license information, please view the LICENSE |
| 10 | * file that was distributed with this source code. |
| 11 | */ |
| 12 | namespace Dudlewebs\WPMCS\GCP\Monolog\Handler; |
| 13 | |
| 14 | use Dudlewebs\WPMCS\GCP\Monolog\Formatter\FormatterInterface; |
| 15 | use Dudlewebs\WPMCS\GCP\Monolog\Formatter\HtmlFormatter; |
| 16 | use Dudlewebs\WPMCS\GCP\Monolog\LogRecord; |
| 17 | /** |
| 18 | * Base class for all mail handlers |
| 19 | * |
| 20 | * @author Gyula Sallai |
| 21 | */ |
| 22 | abstract class MailHandler extends AbstractProcessingHandler |
| 23 | { |
| 24 | /** |
| 25 | * @inheritDoc |
| 26 | */ |
| 27 | public function handleBatch(array $records) : void |
| 28 | { |
| 29 | $messages = []; |
| 30 | foreach ($records as $record) { |
| 31 | if ($record->level->isLowerThan($this->level)) { |
| 32 | continue; |
| 33 | } |
| 34 | $message = $this->processRecord($record); |
| 35 | $messages[] = $message; |
| 36 | } |
| 37 | if (\count($messages) > 0) { |
| 38 | $this->send((string) $this->getFormatter()->formatBatch($messages), $messages); |
| 39 | } |
| 40 | } |
| 41 | /** |
| 42 | * Send a mail with the given content |
| 43 | * |
| 44 | * @param string $content formatted email body to be sent |
| 45 | * @param array $records the array of log records that formed this content |
| 46 | * |
| 47 | * @phpstan-param non-empty-array<LogRecord> $records |
| 48 | */ |
| 49 | protected abstract function send(string $content, array $records) : void; |
| 50 | /** |
| 51 | * @inheritDoc |
| 52 | */ |
| 53 | protected function write(LogRecord $record) : void |
| 54 | { |
| 55 | $this->send((string) $record->formatted, [$record]); |
| 56 | } |
| 57 | /** |
| 58 | * @phpstan-param non-empty-array<LogRecord> $records |
| 59 | */ |
| 60 | protected function getHighestRecord(array $records) : LogRecord |
| 61 | { |
| 62 | $highestRecord = null; |
| 63 | foreach ($records as $record) { |
| 64 | if ($highestRecord === null || $record->level->isHigherThan($highestRecord->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 | protected function getDefaultFormatter() : FormatterInterface |
| 78 | { |
| 79 | return new HtmlFormatter(); |
| 80 | } |
| 81 | } |
| 82 |