Completed
Push — master ( 3bf488...347cdc )
by Dominik
03:33
created

ErrorHandlerMiddleware   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 33
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
    public function __construct(ErrorHandlerInterface $errorHandler)
21
    {
22
        $this->errorHandler = $errorHandler;
23
    }
24
25
    /**
26
     * @param Request  $request
27
     * @param Response $response
28
     * @param callable $next
29
     *
30
     * @return Response
31
     */
32
    public function __invoke(Request $request, Response $response, callable $next): Response
33
    {
34
        try {
35
            return $next($request, $response);
36
        } catch (\Exception $exception) {
37
            $errorHandler = $this->errorHandler;
38
39
            return $errorHandler($request, $response, $exception);
40
        }
41
    }
42
}
43