Passed
Push — main ( 1a3a01...f6a128 )
by Moises
01:22
created

Router::getUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
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
    /**
25
     * Router constructor.
26
     *
27
     * @param string $url
28
     * @param string $method
29
     * @param string $controller
30
     */
31
    public function __construct(string $url, string $method, string $controller)
32
    {
33
        $this->url = $url;
34
        $this->method = $method;
35
        $this->controller = $controller;
36
    }
37
38
    /**
39
     * @return string
40
     */
41
    public function getUrl(): string
42
    {
43
        return $this->url;
44
    }
45
46
    /**
47
     * Run the controller
48
     *
49
     * @return void
50
     */
51
    public function execute(string $namespace, mixed $param): void
52
    {
53
        $controller = $namespace . "\\" . $this->controller;
54
55
        if (!class_exists($controller)) {
56
            throw new ErrorRoute("Class {$controller} not found", 501);
57
        }
58
59
        if (!method_exists($controller, $this->method)) {
60
            throw new ErrorRoute("Method {$this->method} not found", 405);
61
        }
62
63
        call_user_func([new $controller(), $this->method], $param);
64
    }
65
}
66