Passed
Push — master ( 8aa69a...8e436e )
by Edson
02:15
created

Route::go()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
ccs 0
cts 6
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EdsonOnildo\Router;
4
5
use Symfony\Component\HttpFoundation\Request;
6
7
/**
8
 * Class Route
9
 * @package Router
10
 */
11
/**
12
 * Class Route
13
 * @package Router
14
 */
15
/**
16
 * Class Route
17
 * @package Router
18
 */
19
class Route
20
{
21
    /**
22
     * @var Router
23
     */
24
    private $router;
25
26
    /**
27
     * @var bool
28
     */
29
    private $status = false;
30
31
    /**
32
     * @var
33
     */
34
    private static $route;
35
36
    /**
37
     * @var null
38
     */
39
    private static $match = null;
40
41
    /**
42
     * @var null
43
     */
44
    private static $defaultCallback = null;
45
46
    /**
47
     * Route constructor.
48
     */
49
    private function __construct()
50
    {
51
        $this->router = new Router();
52
    }
53
54
    /**
55
     * Route destructor.
56
     */
57
    public function __destruct()
58
    {
59
        if (!$this->status) {
60
            $callback = self::$defaultCallback;
61
            if (is_callable($callback)) {
62
                $callback();
63
                unset($_SESSION['sketch']['errors']);
64
                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...
65
            }
66
        }
67
    }
68
69
    /**
70
     * @return Route
71
     */
72
    private static function route(): Route
73
    {
74
        if (!isset(self::$route) || is_null(self::$route)) {
75
            self::$route = new Route;
76
        }
77
78
        return self::$route;
79
    }
80
81
    /**
82
     * @return Request|null
83
     */
84
    private static function dispatch(): ?Request
85
    {
86
        if (!isset(self::$match) || is_null(self::$match)) {
87
            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 Symfony\Component\HttpFoundation\Request. 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...
88
        }
89
90
        return self::$match;
91
    }
92
93
    /**
94
     * @param string $method
95
     * @param string $uri
96
     * @param $callback
97
     * @return void
98
     */
99
    private function handle(string $method, string $uri, $callback): void
100
    {
101
        $this->router->add([
102
            'uri' => $uri,
103
            'method' => $method,
104
            'callback' => $callback,
105
        ]);
106
107
        $match = $this->dispatch();
108
109
        if ($match) {
110
            $callback = $match->attributes->get('callback');
111
            $callback();
112
            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...
113
        }
114
    }
115
116
    /**
117
     * @param string $uri
118
     * @param $callback
119
     */
120
    public static function get(string $uri, $callback): void
121
    {
122
        self::route()->handle('get', $uri, $callback);
123
    }
124
125
    /**
126
     * @param string $uri
127
     * @param $callback
128
     */
129
    public static function post(string $uri, $callback): void
130
    {
131
        self::route()->handle('post', $uri, $callback);
132
    }
133
134
    /**
135
     * @param string $uri
136
     * @param $callback
137
     */
138
    public static function put(string $uri, $callback): void
139
    {
140
        self::route()->handle('put', $uri, $callback);
141
    }
142
143
    /**
144
     * @param string $uri
145
     * @param $callback
146
     */
147
    public static function patch(string $uri, $callback): void
148
    {
149
        self::route()->handle('patch', $uri, $callback);
150
    }
151
152
    /**
153
     * @param string $uri
154
     * @param $callback
155
     */
156
    public static function delete(string $uri, $callback): void
157
    {
158
        self::route()->handle('delete', $uri, $callback);
159
    }
160
161
    /**
162
     * @param string $uri
163
     * @param $callback
164
     */
165
    public static function options(string $uri, $callback): void
166
    {
167
        self::route()->handle('options', $uri, $callback);
168
    }
169
170
    /**
171
     * @param string $uri
172
     * @param $callback
173
     */
174
    public static function any(string $uri, $callback): void
175
    {
176
        $methods = ['get', 'post', 'put', 'patch', 'delete', 'options'];
177
        self::match($methods, $uri, $callback);
178
    }
179
180
    /**
181
     * @param array $methods
182
     * @param string $uri
183
     * @param $callback
184
     */
185
    public static function match(array $methods, string $uri, $callback): void
186
    {
187
        foreach ($methods as $method) {
188
            self::route()->handle($method, $uri, $callback);
189
        }
190
    }
191
192
    /**
193
     * @param $callback
194
     */
195
    public static function default($callback): void
196
    {
197
        self::$defaultCallback = $callback;
198
    }
199
}
200