Router   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 2
dl 0
loc 50
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A dispatch() 0 9 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Kambo\Router;
5
6
// \Psr\Http\Message
7
use Psr\Http\Message\ServerRequestInterface as ServerRequest;
8
9
//  \Kambo\Router
10
use Kambo\Router\Dispatcher;
11
use Kambo\Router\Matcher;
12
13
/**
14
 * Match provided request object with all defined routes in route collection.
15
 * If some of routes match a data in provided request. Route is dispatched
16
 * with additionall parameters. If nothing is matched execution is passed to
17
 * specific function in dispatcher
18
 *
19
 * @package Kambo\Router
20
 * @author  Bohuslav Simek <[email protected]>
21
 * @license MIT
22
 */
23
class Router
24
{
25
    /**
26
     * Instance of route collection
27
     *
28
     * @var \Kambo\Router\Matcher
29
     */
30
    private $matcher;
31
32
    /**
33
     * Instance of Dispatcher which will dispatch the request
34
     *
35
     * @var \Kambo\Router\Dispatcher
36
     */
37
    private $dispatcher;
38
39
    /**
40
     * Defualt constructor
41
     *
42
     * @param \Kambo\Router\Dispatcher $dispatcher
43
     * @param \Kambo\Router\Matcher    $matcher
44
     *
45
     */
46 20
    public function __construct(
47
        Dispatcher $dispatcher,
48
        Matcher $matcher
49
    ) {
50 20
        $this->dispatcher = $dispatcher;
51 20
        $this->matcher    = $matcher;
52 20
    }
53
54
    /**
55
     * Match request with provided routes.
56
     *
57
     * @param ServerRequest $request    instance of PSR 7 compatible request object
58
     * @param array         $parameters Additional parameters which will be passed into
59
     *                                  the dispatcher.
60
     *
61
     * @return mixed
62
     */
63 20
    public function dispatch(ServerRequest $request, array $parameters = [])
64
    {
65 20
        $matchedRoute = $this->matcher->matchRequest($request);
66 20
        if ($matchedRoute !== false) {
67 16
            return $this->dispatcher->dispatchRoute($matchedRoute, $parameters);
68
        }
69
70 4
        return $this->dispatcher->dispatchNotFound();
71
    }
72
}
73