ErrorHandlerMiddleware   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 33
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 10 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\ErrorHandler;
6
7
use Psr\Http\Message\ServerRequestInterface as Request;
8
use Psr\Http\Message\ResponseInterface as Response;
9
10
final class ErrorHandlerMiddleware
11
{
12
    /**
13
     * @var ErrorHandlerInterface
14
     */
15
    private $errorHandler;
16
17
    /**
18
     * @param ErrorHandlerInterface $errorHandler
19
     */
20 2
    public function __construct(ErrorHandlerInterface $errorHandler)
21
    {
22 2
        $this->errorHandler = $errorHandler;
23 2
    }
24
25
    /**
26
     * @param Request  $request
27
     * @param Response $response
28
     * @param callable $next
29
     *
30
     * @return Response
31
     */
32 2
    public function __invoke(Request $request, Response $response, callable $next): Response
33
    {
34
        try {
35 2
            return $next($request, $response);
36 1
        } catch (\Exception $exception) {
37 1
            $errorHandler = $this->errorHandler;
38
39 1
            return $errorHandler($request, $response, $exception);
40
        }
41
    }
42
}
43