WhoopsMiddleware   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 13
c 4
b 0
f 0
dl 0
loc 22
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A process() 0 11 2
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