Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
Whoops
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
5 / 5
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 render
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
2
 getPrettyPageHandler
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
 getJsonResponseHandler
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getData
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Engelsystem\Exceptions\Handlers;
6
7use Engelsystem\Application;
8use Engelsystem\Helpers\Authenticator;
9use Engelsystem\Http\Request;
10use Throwable;
11use Whoops\Handler\JsonResponseHandler;
12use Whoops\Handler\PrettyPageHandler;
13use Whoops\Run as WhoopsRunner;
14
15class Whoops extends Legacy implements HandlerInterface
16{
17    /**
18     * Whoops constructor.
19     */
20    public function __construct(protected Application $app)
21    {
22    }
23
24    public function render(Request $request, Throwable $e): void
25    {
26        $whoops = $this->app->make(WhoopsRunner::class);
27        $handler = $this->getPrettyPageHandler($e);
28        $whoops->pushHandler($handler);
29        $whoops->writeToOutput(false);
30        $whoops->allowQuit(false);
31
32        if ($request->isXmlHttpRequest()) {
33            $handler = $this->getJsonResponseHandler();
34            $whoops->pushHandler($handler);
35        }
36
37        echo $whoops->handleException($e);
38    }
39
40    protected function getPrettyPageHandler(Throwable $e): PrettyPageHandler
41    {
42        /** @var PrettyPageHandler $handler */
43        $handler = $this->app->make(PrettyPageHandler::class);
44
45        $handler->setPageTitle('Just another ' . get_class($e) . ' to fix :(');
46        $handler->setApplicationPaths([
47            realpath(__DIR__ . '/../..'),
48            realpath(__DIR__ . '/../../../includes/'),
49            realpath(__DIR__ . '/../../../db/'),
50            realpath(__DIR__ . '/../../../tests/'),
51            realpath(__DIR__ . '/../../../public/'),
52        ]);
53
54        $data = $this->getData();
55        $handler->addDataTable('Application', $data);
56
57        return $handler;
58    }
59
60    protected function getJsonResponseHandler(): JsonResponseHandler
61    {
62        $handler = $this->app->make(JsonResponseHandler::class);
63        $handler->setJsonApi(true);
64        $handler->addTraceToOutput(true);
65
66        return $handler;
67    }
68
69    /**
70     * Aggregate application data
71     */
72    protected function getData(): array
73    {
74        $data = [];
75        $user = null;
76
77        if ($this->app->has('authenticator')) {
78            /** @var Authenticator $authenticator */
79            $authenticator = $this->app->get('authenticator');
80            $user = $authenticator->user();
81        }
82
83        $data['user'] = $user;
84        $data['Booted'] = $this->app->isBooted();
85
86        return $data;
87    }
88}