Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
20 / 20 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| EmailLogger | |
100.00% |
20 / 20 |
|
100.00% |
3 / 3 |
7 | |
100.00% |
1 / 1 |
| setDestination | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| makeHeaders | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 | |||
| write | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php declare(strict_types=1); |
| 2 | /* |
| 3 | * This file is part of Aplus Framework Log Library. |
| 4 | * |
| 5 | * (c) Natan Felles <natanfelles@gmail.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 Framework\Log\Loggers; |
| 11 | |
| 12 | use Framework\Log\Log; |
| 13 | use Framework\Log\Logger; |
| 14 | use InvalidArgumentException; |
| 15 | |
| 16 | /** |
| 17 | * Class EmailLogger. |
| 18 | * |
| 19 | * @package log |
| 20 | */ |
| 21 | class EmailLogger extends Logger |
| 22 | { |
| 23 | protected function setDestination(string $destination) : static |
| 24 | { |
| 25 | if ( ! \filter_var($destination, \FILTER_VALIDATE_EMAIL)) { |
| 26 | throw new InvalidArgumentException('Invalid email destination: ' . $destination); |
| 27 | } |
| 28 | $this->destination = $destination; |
| 29 | return $this; |
| 30 | } |
| 31 | |
| 32 | protected function makeHeaders(Log $log) : string |
| 33 | { |
| 34 | $headers = $this->getConfig()['headers'] ?? []; |
| 35 | $names = []; |
| 36 | foreach ($headers as $name => $value) { |
| 37 | $names[] = \strtolower($name); |
| 38 | } |
| 39 | if ( ! \in_array('subject', $names, true)) { |
| 40 | $headers['Subject'] = 'Log ' . $log->level->name . ' ' . $log->id; |
| 41 | } |
| 42 | $headerLines = []; |
| 43 | foreach ($headers as $name => $value) { |
| 44 | $headerLines[] = $name . ': ' . $value; |
| 45 | } |
| 46 | return \implode("\r\n", $headerLines); |
| 47 | } |
| 48 | |
| 49 | protected function write(Log $log) : bool |
| 50 | { |
| 51 | return \error_log( |
| 52 | (string) $log, |
| 53 | 1, |
| 54 | $this->getDestination(), |
| 55 | $this->makeHeaders($log) |
| 56 | ); |
| 57 | } |
| 58 | } |