Test Failed
Branch develop (ab83c3)
by Bohuslav
02:47
created

Router::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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