Passed
Push — master ( 2aa8dd...218721 )
by n
03:53
created

RoutingHandler::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace N1215\Http\Router;
5
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
9
final class RoutingHandler implements RoutingHandlerInterface
10
{
11
    /** @var RouterInterface  */
12
    private $router;
13
14
    /** @var RoutingErrorResponderInterface  */
15
    private $errorResponder;
16
17 2
    public function __construct(RouterInterface $router, RoutingErrorResponderInterface $errorResponder)
18
    {
19 2
        $this->router = $router;
20 2
        $this->errorResponder = $errorResponder;
21 2
    }
22
23
    /**
24
     * @inheritDoc
25
     */
26 2
    public function handle(ServerRequestInterface $request): ResponseInterface
27
    {
28 2
        $result = $this->router->match($request);
29
30 2
        if (!$result->isSuccess()) {
31 1
            return $this->errorResponder->respond($result->getError());
32
        }
33
34 1
        foreach ($result->getMatchedParams() as $name => $value) {
35 1
            $request = $request->withAttribute($name, $value);
36
        }
37
38 1
        return $result->getMatchedHandler()->handle($request);
39
    }
40
}
41