| Code Coverage | ||||||||||
| Lines | Functions and Methods | Classes and Traits | ||||||||
| Total |  | 100.00% | 27 / 27 |  | 100.00% | 4 / 4 | CRAP |  | 100.00% | 1 / 1 | 
| Legacy |  | 100.00% | 27 / 27 |  | 100.00% | 4 / 4 | 12 |  | 100.00% | 1 / 1 | 
| render |  | 100.00% | 3 / 3 |  | 100.00% | 1 / 1 | 2 | |||
| report |  | 100.00% | 21 / 21 |  | 100.00% | 1 / 1 | 6 | |||
| setLogger |  | 100.00% | 1 / 1 |  | 100.00% | 1 / 1 | 1 | |||
| stripBasePath |  | 100.00% | 2 / 2 |  | 100.00% | 1 / 1 | 1 | |||
| isCli | n/a | 0 / 0 | n/a | 0 / 0 | 2 | |||||
| 1 | <?php | 
| 2 | |
| 3 | declare(strict_types=1); | 
| 4 | |
| 5 | namespace Engelsystem\Exceptions\Handlers; | 
| 6 | |
| 7 | use Engelsystem\Http\Request; | 
| 8 | use ErrorException; | 
| 9 | use Psr\Log\LoggerInterface; | 
| 10 | use Psr\Log\LogLevel; | 
| 11 | use Throwable; | 
| 12 | |
| 13 | class Legacy implements HandlerInterface | 
| 14 | { | 
| 15 | protected ?LoggerInterface $log = null; | 
| 16 | |
| 17 | public function render(Request $request, Throwable $e): void | 
| 18 | { | 
| 19 | if ($this->isCli()) { | 
| 20 | return; | 
| 21 | } | 
| 22 | |
| 23 | echo 'An <del>un</del>expected error occurred. A team of untrained monkeys has been dispatched to fix it.'; | 
| 24 | } | 
| 25 | |
| 26 | public function report(Throwable $e): void | 
| 27 | { | 
| 28 | $previous = $e->getPrevious(); | 
| 29 | error_log(sprintf( | 
| 30 | '%s: Code: %s, Message: %s, File: %s:%u, Previous: %s, Trace: %s', | 
| 31 | get_class($e), | 
| 32 | $e->getCode(), | 
| 33 | $e->getMessage(), | 
| 34 | $this->stripBasePath($e->getFile()), | 
| 35 | $e->getLine(), | 
| 36 | $previous ? $previous->getMessage() : 'None', | 
| 37 | json_encode($e->getTrace()) | 
| 38 | )); | 
| 39 | |
| 40 | if (is_null($this->log)) { | 
| 41 | return; | 
| 42 | } | 
| 43 | |
| 44 | $errorLevels = E_ERROR | E_RECOVERABLE_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR; | 
| 45 | $logAsError = !$e instanceof ErrorException || $e->getSeverity() & $errorLevels; | 
| 46 | try { | 
| 47 | $this->log->log( | 
| 48 | $logAsError ? LogLevel::CRITICAL : LogLevel::WARNING, | 
| 49 | '', | 
| 50 | ['exception' => $e], | 
| 51 | ); | 
| 52 | } catch (Throwable) { | 
| 53 | } | 
| 54 | } | 
| 55 | |
| 56 | public function setLogger(LoggerInterface $logger): void | 
| 57 | { | 
| 58 | $this->log = $logger; | 
| 59 | } | 
| 60 | |
| 61 | protected function stripBasePath(string $path): string | 
| 62 | { | 
| 63 | $basePath = realpath(__DIR__ . '/../../..') . '/'; | 
| 64 | return str_replace($basePath, '', $path); | 
| 65 | } | 
| 66 | |
| 67 | /** | 
| 68 | * Test if is called from cli | 
| 69 | * @codeCoverageIgnore | 
| 70 | */ | 
| 71 | protected function isCli(): bool | 
| 72 | { | 
| 73 | return PHP_SAPI == 'cli' || PHP_SAPI == 'phpdbg'; | 
| 74 | } | 
| 75 | } |