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

Router::setParam()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 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