Completed
Pull Request — master (#2)
by René
04:42
created

Router::route()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 10
ccs 4
cts 5
cp 0.8
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
crap 2.032
1
<?php
2
declare(strict_types = 1);
3
4
namespace Zortje\MVC\Routing;
5
6
use Zortje\MVC\Routing\Exception\RouteAlreadyConnectedException;
7
use Zortje\MVC\Routing\Exception\RouteNonexistentException;
8
9
/**
10
 * Class Router
11
 *
12
 * @package Zortje\MVC\Routing
13
 */
14
class Router
15
{
16
17
    /**
18
     * @var array Routes
19
     */
20
    protected $routes = [];
21
22
    /**
23
     * Connects a new route in the router
24
     *
25
     * @param string $route      Route
26
     * @param string $controller Controller
27
     * @param string $action     Action
28
     *
29
     * @throws RouteAlreadyConnectedException When route is already connected
30
     */
31 2
    public function connect(string $route, string $controller, string $action)
32
    {
33 2
        if (isset($this->routes[$route]) === true) {
34 1
            throw new RouteAlreadyConnectedException([$route]);
35
        }
36
37 2
        $this->routes[$route] = [
38 2
            'controller' => $controller,
39 2
            'action'     => $action
40
        ];
41 2
    }
42
43
    /**
44
     * Route to get controller and action
45
     *
46
     * @param string $route Route
47
     *
48
     * @return array Controller and action
49
     *
50
     * @throws RouteNonexistentException When route is not connected
51
     */
52 1
    public function route(string $route): array
53
    {
54 1
        if (isset($this->routes[$route]) === false) {
55
            throw new RouteNonexistentException([$route]);
56
        }
57
58 1
        $result = $this->routes[$route];
59
60 1
        return $result;
61
    }
62
}
63