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