Passed
Push — master ( b3f945...fcf496 )
by Jelmer
04:58
created

RoutingMiddleware   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
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 36
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A process() 0 18 3
1
<?php declare(strict_types = 1);
2
3
namespace jschreuder\Middle\ServerMiddleware;
4
5
use Interop\Http\ServerMiddleware\DelegateInterface;
6
use Interop\Http\ServerMiddleware\MiddlewareInterface;
7
use jschreuder\Middle\Controller\ControllerInterface;
8
use jschreuder\Middle\Router\RouterInterface;
9
use Psr\Http\Message\ResponseInterface;
10
use Psr\Http\Message\ServerRequestInterface;
11
12
final class RoutingMiddleware implements MiddlewareInterface
13
{
14
    /** @var  RouterInterface */
15
    private $router;
16
17
    /** @var  ControllerInterface */
18
    private $fallbackController;
19
20 3
    public function __construct(
21
        RouterInterface $router,
22
        ControllerInterface $fallbackController
23
    )
24
    {
25 3
        $this->router = $router;
26 3
        $this->fallbackController = $fallbackController;
27 3
    }
28
29 2
    public function process(ServerRequestInterface $request, DelegateInterface $delegate): ResponseInterface
30
    {
31 2
        $routeMatch = $this->router->parseRequest($request);
32
33 2
        if ($routeMatch->isMatch()) {
34
            // Register Controller to the request object
35 1
            $request = $request->withAttribute('controller', $routeMatch->getController());
36
37
            // Add all routing attributes to request
38 1
            foreach ($routeMatch->getAttributes() as $key => $value) {
39 1
                $request = $request->withAttribute($key, $value);
40
            }
41
        } else {
42 1
            $request = $request->withAttribute('controller', $this->fallbackController);
43
        }
44
45 2
        return $delegate->process($request);
46
    }
47
}
48