Test Failed
Push — master ( b22efa...070fdf )
by Felipe
04:16 queued 02:04
created

ErrorHandlerMiddleware::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of coisa/http.
5
 *
6
 * (c) Felipe Sayão Lobato Abreu <[email protected]>
7
 *
8
 * This source file is subject to the license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
declare(strict_types=1);
13
14
namespace CoiSA\Http\Middleware;
15
16
use Psr\EventDispatcher\EventDispatcherInterface;
0 ignored issues
show
Bug introduced by
The type Psr\EventDispatcher\EventDispatcherInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Psr\Http\Message\ResponseInterface;
18
use Psr\Http\Message\ServerRequestInterface;
19
use Psr\Http\Server\MiddlewareInterface;
20
use Psr\Http\Server\RequestHandlerInterface;
21
22
/**
23
 * Class ErrorHandlerMiddleware
24
 *
25
 * @package CoiSA\Http\Middleware
26
 */
27
final class ErrorHandlerMiddleware implements MiddlewareInterface
28
{
29
    /**
30
     * @var RequestHandlerInterface
31
     */
32
    private $handler;
33
34
    /**
35
     * @var EventDispatcherInterface
36
     */
37
    private $eventDispatcher;
38
39
    /**
40
     * ErrorHandlerMiddleware constructor.
41
     *
42
     * @param RequestHandlerInterface $handler
43
     * @param EventDispatcherInterface $eventDispatcher
44
     */
45
    public function __construct(RequestHandlerInterface $handler, EventDispatcherInterface $eventDispatcher)
46
    {
47
        $this->handler = $handler;
48
        $this->eventDispatcher = $eventDispatcher;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
55
    {
56
        try {
57
            return $handler->handle($request);
58
        } catch (\Throwable $throwable) {
59
            $errorRequest = $request->withAttribute(
60
                self::class,
61
                $throwable
62
            );
63
64
            $this->eventDispatcher->dispatch($throwable);
65
66
            return $this->handler->handle($errorRequest);
67
        }
68
    }
69
}
70