RegisterExceptionHandler::__invoke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 2
rs 10
1
<?php declare(strict_types = 1);
2
3
/**
4
 * This file is part of the Simplex package.
5
 *
6
 * (c) Freddie Frantzen <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Simplex\HttpMiddleware;
12
13
use Psr\Http\Message\ResponseInterface as Response;
14
use Psr\Http\Message\ServerRequestInterface;
15
use Whoops\Handler\PrettyPageHandler;
16
use Whoops\Run;
17
18
final class RegisterExceptionHandler
19
{
20
    /** @var bool */
21
    private $debugEnabled;
22
23
    /** @var string */
24
    private $editor;
25
26
    public function __construct(bool $debugEnabled, string $editor)
27
    {
28
        $this->debugEnabled = $debugEnabled;
29
        $this->editor = $editor;
30
    }
31
32
    public function __invoke(ServerRequestInterface $request, Response $response, callable $next)
33
    {
34
        $this->registerExceptionHandler();
35
36
        $response = $next($request, $response);
37
38
        return $response;
39
    }
40
41
    private function registerExceptionHandler(): void
42
    {
43
        if (!$this->debugEnabled) {
44
            return;
45
        }
46
47
        $whoops = new Run();
48
49
        $handler = new PrettyPageHandler();
50
        $handler->setEditor($this->editor);
51
52
        $whoops->pushHandler($handler);
53
        $whoops->register();
54
    }
55
}
56