Passed
Push — main ( f6a128...5b5bbf )
by Moises
06:25
created

Router   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 16
c 1
b 0
f 0
dl 0
loc 64
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A execute() 0 13 3
A setParam() 0 3 1
A getUrl() 0 3 1
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 mixed */
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 mixed $param
43
     * @return void
44
     */
45
    public function setParam(mixed $param): void
46
    {
47
        $this->param = $param;
48
    }
49
50
    /**
51
     * @return string
52
     */
53
    public function getUrl(): string
54
    {
55
        return $this->url;
56
    }
57
58
    /**
59
     * Run the controller
60
     *
61
     * @param string $namespace
62
     * @return void
63
     */
64
    public function execute(string $namespace): void
65
    {
66
        $controller = $namespace . "\\" . $this->controller;
67
68
        if (!class_exists($controller)) {
69
            throw new ErrorRoute("Class {$controller} not found", 501);
70
        }
71
72
        if (!method_exists($controller, $this->method)) {
73
            throw new ErrorRoute("Method {$this->method} not found", 405);
74
        }
75
76
        call_user_func([new $controller(), $this->method], $this->param);
77
    }
78
}
79