Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
Handler
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
9 / 9
16
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 register
n/a
0 / 0
n/a
0 / 0
2
 errorHandler
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 exceptionHandler
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
4
 terminateApplicationImmediately
n/a
0 / 0
n/a
0 / 0
1
 getEnvironment
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setEnvironment
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getHandler
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 setHandler
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRequest
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setRequest
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Engelsystem\Exceptions;
6
7use Engelsystem\Environment;
8use Engelsystem\Exceptions\Handlers\HandlerInterface;
9use Engelsystem\Exceptions\Handlers\Legacy;
10use Engelsystem\Http\Request;
11use ErrorException;
12use Throwable;
13
14class Handler
15{
16    /** @var HandlerInterface[] */
17    protected array $handler = [];
18
19    protected ?Request $request = null;
20
21    /**
22     * Handler constructor.
23     *
24     * @param Environment $environment prod|dev
25     */
26    public function __construct(protected Environment $environment = Environment::PRODUCTION)
27    {
28    }
29
30    /**
31     * Activate the error handler
32     * @codeCoverageIgnore
33     */
34    public function register(): void
35    {
36        if (defined('PHPUNIT_COMPOSER_INSTALL')) {
37            return;
38        }
39
40        set_error_handler([$this, 'errorHandler']);
41        set_exception_handler([$this, 'exceptionHandler']);
42    }
43
44    public function errorHandler(int $number, string $message, string $file, int $line): void
45    {
46        $exception = new ErrorException($message, 0, $number, $file, $line);
47        $this->exceptionHandler($exception);
48    }
49
50    public function exceptionHandler(Throwable $e, bool $return = false): string
51    {
52        if (!$this->request instanceof Request) {
53            $this->request = new Request();
54        }
55
56        $handler = $this->handler[$this->environment->value] ?? new Legacy();
57        $handler->report($e);
58        ob_start();
59        $handler->render($this->request, $e);
60
61        if ($return) {
62            $output = ob_get_contents();
63            ob_end_clean();
64            return $output;
65        }
66
67        if (!headers_sent()) {
68            // @codeCoverageIgnoreStart
69            http_response_code(500);
70            // @codeCoverageIgnoreEnd
71        }
72
73        ob_end_flush();
74
75        $this->terminateApplicationImmediately();
76
77        return '';
78    }
79
80    /**
81     * Exit the application
82     *
83     * @codeCoverageIgnore
84     */
85    protected function terminateApplicationImmediately(string $message = ''): void
86    {
87        echo $message;
88        die(1);
89    }
90
91    public function getEnvironment(): Environment
92    {
93        return $this->environment;
94    }
95
96    public function setEnvironment(Environment $environment): void
97    {
98        $this->environment = $environment;
99    }
100
101    /**
102     * @return HandlerInterface|HandlerInterface[]
103     */
104    public function getHandler(Environment $environment = null): HandlerInterface|array
105    {
106        if (!is_null($environment)) {
107            return $this->handler[$environment->value];
108        }
109
110        return $this->handler;
111    }
112
113    public function setHandler(Environment $environment, HandlerInterface $handler): void
114    {
115        $this->handler[$environment->value] = $handler;
116    }
117
118    public function getRequest(): Request
119    {
120        return $this->request;
121    }
122
123    public function setRequest(Request $request): void
124    {
125        $this->request = $request;
126    }
127}