Completed
Push — master ( c0720e...b4e066 )
by Edson
01:14
created

Route   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 22
dl 0
loc 159
rs 10
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A default() 0 3 1
A post() 0 3 1
A any() 0 4 1
A route() 0 7 3
A patch() 0 3 1
A handle() 0 15 2
A delete() 0 3 1
A options() 0 3 1
A __destruct() 0 10 3
A put() 0 3 1
A __construct() 0 3 1
A match() 0 4 2
A dispatch() 0 7 3
A get() 0 3 1
1
<?php
2
3
namespace Router;
4
5
/**
6
 * Class Route
7
 * @package Router
8
 */
9
class Route
10
{
11
    /**
12
     * @var BaseRouter
13
     */
14
    private $router;
15
16
    private $status = false;
17
18
    /**
19
     * @var
20
     */
21
    private static $route;
22
23
    /**
24
     * @var null
25
     */
26
    private static $match = null;
27
28
    private static $defaultCallback = null;
29
30
    /**
31
     * Route constructor.
32
     */
33
    private function __construct()
34
    {
35
        $this->router = new BaseRouter();
36
    }
37
38
    public function __destruct()
39
    {
40
        if (!$this->status) {
41
            $callback = self::$defaultCallback;
42
            if (is_callable($callback)) {
43
                $callback();
44
            }
45
        }
46
47
        exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
48
    }
49
50
    /**
51
     * @return Route
52
     */
53
    private static function route(): Route
54
    {
55
        if (!isset(self::$route) || is_null(self::$route)) {
56
            self::$route = new Route;
57
        }
58
59
        return self::$route;
60
    }
61
62
    /**
63
     * @return null|BaseRoute
64
     */
65
    private static function dispatch(): ?BaseRoute
66
    {
67
        if (!isset(self::$match) || is_null(self::$match)) {
68
            self::$match = self::route()->router->handle();
0 ignored issues
show
Documentation Bug introduced by
It seems like self::route()->router->handle() can also be of type Router\BaseRoute. However, the property $match is declared as type null. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
69
        }
70
71
        return self::$match;
72
    }
73
74
    /**
75
     * @param string $method
76
     * @param string $uri
77
     * @param $callback
78
     * @return void
79
     */
80
    private function handle(string $method, string $uri, $callback): void
81
    {
82
        $this->router->add([
83
            'uri' => $uri,
84
            'name' => '',
85
            'method' => $method,
86
            'callback' => $callback
87
        ]);
88
89
        $match = $this->dispatch();
90
91
        if ($match) {
92
            call_user_func_array($match->getCallback(), $match->getArgs());
93
            $this->status = true;
94
            exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
95
        }
96
    }
97
98
    /**
99
     * @param string $uri
100
     * @param $callback
101
     */
102
    public static function get(string $uri, $callback): void
103
    {
104
        self::route()->handle('get', $uri, $callback);
105
    }
106
107
    /**
108
     * @param string $uri
109
     * @param $callback
110
     */
111
    public static function post(string $uri, $callback): void
112
    {
113
        self::route()->handle('post', $uri, $callback);
114
    }
115
116
    /**
117
     * @param string $uri
118
     * @param $callback
119
     */
120
    public static function put(string $uri, $callback): void
121
    {
122
        self::route()->handle('put', $uri, $callback);
123
    }
124
125
    /**
126
     * @param string $uri
127
     * @param $callback
128
     */
129
    public static function patch(string $uri, $callback): void
130
    {
131
        self::route()->handle('patch', $uri, $callback);
132
    }
133
134
    /**
135
     * @param string $uri
136
     * @param $callback
137
     */
138
    public static function delete(string $uri, $callback): void
139
    {
140
        self::route()->handle('delete', $uri, $callback);
141
    }
142
143
    /**
144
     * @param string $uri
145
     * @param $callback
146
     */
147
    public static function options(string $uri, $callback): void
148
    {
149
        self::route()->handle('options', $uri, $callback);
150
    }
151
152
    public static function any(string $uri, $callback): void
153
    {
154
        $methods = ['get', 'post', 'put', 'patch', 'delete', 'options'];
155
        self::match($methods, $uri, $callback);
156
    }
157
158
    public static function match(array $methods, string $uri, $callback): void
159
    {
160
        foreach ($methods as $method) {
161
            self::route()->handle($method, $uri, $callback);
162
        }
163
    }
164
165
    public static function default($callback): void
0 ignored issues
show
Coding Style introduced by
Possible parse error: non-abstract method defined as abstract
Loading history...
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
166
    {
167
        self::$defaultCallback = $callback;
168
    }
169
}
170