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

Router   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 91.67%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
wmc 4
c 6
b 0
f 0
lcom 1
cbo 2
dl 0
loc 49
ccs 11
cts 12
cp 0.9167
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A connect() 0 11 2
A route() 0 10 2
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