WhoopsMiddleware::process()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
eloc 8
c 3
b 0
f 0
nc 2
nop 2
dl 0
loc 11
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chinstrap\Core\Http\Middleware;
6
7
use Chinstrap\Core\Contracts\Exceptions\Handler;
8
use Psr\Http\Message\ResponseInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Server\MiddlewareInterface;
11
use Psr\Http\Server\RequestHandlerInterface;
12
use Whoops\Handler\CallbackHandler;
13
use Whoops\Handler\PrettyPageHandler;
14
use Whoops\RunInterface;
15
16
final class WhoopsMiddleware implements MiddlewareInterface
17
{
18
    private RunInterface $whoops;
19
    private Handler $errorHandler;
20
21
    public function __construct(RunInterface $whoops, Handler $errorHandler)
22
    {
23
        $this->whoops = $whoops;
24
        $this->errorHandler = $errorHandler;
25
    }
26
27
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
28
    {
29
        $env = $request->getServerParams();
30
        assert($this->whoops instanceof \Whoops\Run);
31
        if ($env['APP_ENV'] === 'production') {
32
            $this->whoops->prependHandler(new CallbackHandler($this->errorHandler));
33
        } else {
34
            $this->whoops->prependHandler(new PrettyPageHandler());
35
        }
36
        $this->whoops->register();
37
        return $handler->handle($request);
38
    }
39
}
40