ErrorHandlerMiddleware   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 21
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 6 2
A __construct() 0 3 1
1
<?php
2
3
namespace Simply\Application\Middleware;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Server\MiddlewareInterface;
8
use Psr\Http\Server\RequestHandlerInterface;
9
10
/**
11
 * Basic error handler middleware that forwards all exceptions to given request handler.
12
 * @author Riikka Kalliomäki <[email protected]>
13
 * @copyright Copyright (c) 2018 Riikka Kalliomäki
14
 * @license http://opensource.org/licenses/mit-license.php MIT License
15
 */
16
class ErrorHandlerMiddleware implements MiddlewareInterface
17
{
18
    /** @var RequestHandlerInterface The request handler used to handle exceptions */
19
    private $errorHandler;
20
21
    /**
22
     * ErrorHandlerMiddleware constructor.
23
     * @param RequestHandlerInterface $handler The request handler used to handle exceptions
24
     */
25 12
    public function __construct(RequestHandlerInterface $handler)
26
    {
27 12
        $this->errorHandler = $handler;
28 12
    }
29
30
    /** {@inheritdoc} */
31 12
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
32
    {
33
        try {
34 12
            return $handler->handle($request);
35 1
        } catch (\Throwable $exception) {
36 1
            return $this->errorHandler->handle($request->withAttribute('exception', $exception));
37
        }
38
    }
39
}
40