ErrorMiddleware::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Aidphp\Error;
6
7
use Psr\Http\Server\MiddlewareInterface;
8
use Psr\Http\Server\RequestHandlerInterface;
9
use Psr\Http\Message\ServerRequestInterface;
10
use Psr\Http\Message\ResponseInterface;
11
use Throwable;
12
13
class ErrorMiddleware implements MiddlewareInterface
14
{
15
    protected $errorHandler;
16
17 3
    public function __construct(ErrorHandlerInterface $errorHandler)
18
    {
19 3
        $this->errorHandler = $errorHandler;
20 3
    }
21
22 3
    public function process(ServerRequestInterface $req, RequestHandlerInterface $handler): ResponseInterface
23
    {
24 3
        $level = ob_get_level();
25
26
        try
27
        {
28 3
            $res = $handler->handle($req);
29
        }
30 2
        catch (Throwable $e)
31
        {
32 2
            while (ob_get_level() > $level)
33
            {
34 1
                ob_end_clean();
35
            }
36
37 2
            $res = $this->errorHandler->handleError($e, $req);
38
        }
39
40 3
        return $res;
41
    }
42
}