Completed
Push — master ( a52764...802455 )
by n
59:28
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
wmc 4
lcom 1
cbo 4
dl 0
loc 32
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

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\Handler;
5
6
use N1215\Http\Router\RouterInterface;
7
use N1215\Http\Router\RoutingErrorResponderInterface;
8
use N1215\Http\Router\RoutingHandlerInterface;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
12
final class RoutingHandler implements RoutingHandlerInterface
13
{
14
    /** @var RouterInterface */
15
    private $router;
16
17
    /** @var RoutingErrorResponderInterface */
18
    private $errorResponder;
19
20 2
    public function __construct(RouterInterface $router, RoutingErrorResponderInterface $errorResponder)
21
    {
22 2
        $this->router = $router;
23 2
        $this->errorResponder = $errorResponder;
24 2
    }
25
26
    /**
27
     * @inheritDoc
28
     */
29 2
    public function handle(ServerRequestInterface $request): ResponseInterface
30
    {
31 2
        $result = $this->router->match($request);
32
33 2
        if (!$result->isSuccess()) {
34 1
            return $this->errorResponder->respond($request, $result->getError());
35
        }
36
37 1
        foreach ($result->getMatchedParams() as $name => $value) {
38 1
            $request = $request->withAttribute($name, $value);
39
        }
40
41 1
        return $result->getMatchedHandler()->handle($request);
42
    }
43
}
44