Router   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 7
eloc 18
c 3
b 1
f 0
dl 0
loc 71
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A setParam() 0 3 1
A getUrl() 0 7 2
A execute() 0 13 3
1
<?php
2
3
namespace DevPontes\Route;
4
5
use DevPontes\Route\Exception\ErrorRoute;
6
7
/**
8
 * Description of Router
9
 *
10
 * @author Moises Pontes
11
 * @package DevPontes\Route
12
 */
13
class Router
14
{
15
    /** @var string */
16
    private $url;
17
18
    /** @var string */
19
    private $method;
20
21
    /** @var string */
22
    private $controller;
23
24
    /** @var string */
25
    private $param;
26
27
    /**
28
     * Router constructor.
29
     *
30
     * @param string $url
31
     * @param string $method
32
     * @param string $controller
33
     */
34
    public function __construct(string $url, string $method, string $controller)
35
    {
36
        $this->url = $url;
37
        $this->method = $method;
38
        $this->controller = $controller;
39
    }
40
41
    /**
42
     * @param string|null $param
43
     * @return void
44
     */
45
    public function setParam(?string $param): void
46
    {
47
        $this->param = $param;
48
    }
49
50
    /**
51
     * Returns the complete URL (string) or in parts (array)
52
     *
53
     * @param boolean $part
54
     * @return string|array
55
     */
56
    public function getUrl(bool $part = false): string | array
57
    {
58
        if ($part) {
59
            return explode('/', $this->url);
60
        }
61
62
        return $this->url;
63
    }
64
65
    /**
66
     * Run the controller
67
     *
68
     * @param string $namespace
69
     * @return void
70
     */
71
    public function execute(string $namespace): void
72
    {
73
        $controller = $namespace . "\\" . $this->controller;
74
75
        if (!class_exists($controller)) {
76
            throw new ErrorRoute("Controller {$controller} not found", 404);
77
        }
78
79
        if (!method_exists($controller, $this->method)) {
80
            throw new ErrorRoute("Method {$this->method} not found in {$controller}", 405);
81
        }
82
83
        call_user_func([new $controller(), $this->method], $this->param);
84
    }
85
}
86