ErrorHandlerMiddleware::process()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
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