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

RoutingHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 32
c 0
b 0
f 0
wmc 4
lcom 1
cbo 4
ccs 11
cts 11
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A handle() 0 14 3
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