Passed
Push — master ( b51215...92fe06 )
by Alexander
01:26
created

Router   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 14
c 2
b 0
f 0
dl 0
loc 29
ccs 13
cts 13
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A process() 0 18 4
A __construct() 0 4 1
1
<?php
2
3
namespace Yiisoft\Router\Middleware;
4
5
use Psr\Http\Message\ResponseFactoryInterface;
6
use Psr\Http\Message\ResponseInterface;
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Server\MiddlewareInterface;
9
use Psr\Http\Server\RequestHandlerInterface;
10
use Yiisoft\Router\UrlMatcherInterface;
11
12
final class Router implements MiddlewareInterface
13
{
14
    private UrlMatcherInterface $matcher;
15
    private ResponseFactoryInterface $responseFactory;
16
17 3
    public function __construct(UrlMatcherInterface $matcher, ResponseFactoryInterface $responseFactory)
18
    {
19 3
        $this->matcher = $matcher;
20 3
        $this->responseFactory = $responseFactory;
21
    }
22
23 3
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
24
    {
25 3
        $result = $this->matcher->match($request);
26
27 3
        if ($result->isMethodFailure()) {
28 1
            return $this->responseFactory->createResponse(405)
29 1
                ->withHeader('Allow', implode(', ', $result->methods()));
30
        }
31
32 2
        if (!$result->isSuccess()) {
33 1
            return $handler->handle($request);
34
        }
35
36 1
        foreach ($result->parameters() as $parameter => $value) {
37 1
            $request = $request->withAttribute($parameter, $value);
38
        }
39
40 1
        return $result->process($request, $handler);
41
    }
42
}
43