Passed
Push — master ( c0f6da...b424db )
by Jelmer
04:57
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\Router;
4
5
use jschreuder\Middle\HttpMiddlewareInterface;
6
use jschreuder\Middle\DelegateInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
10
class RoutingMiddleware implements HttpMiddlewareInterface
11
{
12
    /** @var  RouterInterface */
13
    private $router;
14
15
    /** @var  callable */
16
    private $fallbackController;
17
18 3
    public function __construct(
19
        RouterInterface $router,
20
        callable $fallbackController
21
    )
22
    {
23 3
        $this->router = $router;
24 3
        $this->fallbackController = $fallbackController;
25 3
    }
26
27 2
    public function process(ServerRequestInterface $request, DelegateInterface $delegate) : ResponseInterface
28
    {
29 2
        $routeMatch = $this->router->parseRequest($request);
30
31 2
        if ($routeMatch->isMatch()) {
32
            // Register Controller to the request object
33 1
            $request = $request->withAttribute('controller', $routeMatch->getController());
34
35
            // Add all routing attributes to request
36 1
            foreach ($routeMatch->getAttributes() as $key => $value) {
37 1
                $request = $request->withAttribute($key, $value);
38
            }
39
        } else {
40 1
            $request = $request->withAttribute('controller', $this->fallbackController);
41
        }
42
43 2
        return $delegate->next($request);
44
    }
45
}
46