Router::dispatch()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 2
crap 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