Passed
Push — master ( a4ddd3...82bd79 )
by Alex
06:44
created

RoutesSetBase::routeExists()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 8
c 1
b 1
f 0
dl 0
loc 15
rs 10
cc 4
nc 4
nop 1
1
<?php
2
declare(strict_types = 1);
3
namespace Mezon\Router;
4
5
/**
6
 * Trait RoutesSetBase
7
 *
8
 * @package Router
9
 * @author Dodonov A.A.
10
 * @version v.1.0 (2021/09/27)
11
 * @copyright Copyright (c) 2021, aeon.org
12
 */
13
trait RoutesSetBase
14
{
15
16
    /**
17
     * Method returns true if the param router exists
18
     *
19
     * @param string $route
20
     *            checking route
21
     * @param string $requestMethod
22
     *            HTTP request method
23
     * @return bool true if the param router exists, false otherwise
24
     */
25
    protected abstract function paramRouteExists(string $route, string $requestMethod): bool;
26
27
    /**
28
     * Method returns true if the router exists
29
     *
30
     * @param string $route
31
     *            checking route
32
     * @return bool true if the router exists, false otherwise
33
     */
34
    public function routeExists(string $route): bool
35
    {
36
        $route = trim($route, '/');
37
38
        foreach (SuppportedRequestMethods::getListOfSupportedRequestMethods() as $requestMethod) {
39
            if (isset($this->staticRoutes[$requestMethod][$route])) {
40
                return true;
41
            } else {
42
                if ($this->paramRouteExists($route, $requestMethod)) {
43
                    return true;
44
                }
45
            }
46
        }
47
48
        return false;
49
    }
50
51
    /**
52
     * Route names
53
     *
54
     * @var string[]
55
     */
56
    private $routeNames = [];
57
58
    /**
59
     * Method clears other data
60
     */
61
    protected function clearOtherData(): void
62
    {}
63
64
    /**
65
     * Method clears router data
66
     */
67
    public function clear(): void
68
    {
69
        $this->routeNames = [];
70
71
        foreach (SuppportedRequestMethods::getListOfSupportedRequestMethods() as $requestMethod) {
72
            $this->staticRoutes[$requestMethod] = [];
0 ignored issues
show
Bug Best Practice introduced by
The property staticRoutes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
73
            $this->paramRoutes[$requestMethod] = [];
0 ignored issues
show
Bug Best Practice introduced by
The property paramRoutes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
74
        }
75
76
        $this->middleware = [];
0 ignored issues
show
Bug Best Practice introduced by
The property middleware does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
77
78
        $this->clearOtherData();
79
    }
80
81
    /**
82
     * Getting route by name
83
     *
84
     * @param string $routeName
85
     *            route's name
86
     * @return string route
87
     */
88
    public function getRouteByName(string $routeName): string
89
    {
90
        if ($this->routeNameExists($routeName) === false) {
91
            throw (new \Exception('Route with name ' . $routeName . ' does not exist'));
92
        }
93
94
        return $this->routeNames[$routeName];
95
    }
96
97
    /**
98
     * Validating that route name exists
99
     *
100
     * @param string $routeName route name
101
     * @return bool true if the route exists, false otherwise
102
     */
103
    protected function routeNameExists(string $routeName): bool
104
    {
105
        return isset($this->routeNames[$routeName]);
106
    }
107
108
    /**
109
     * Method registers name of the route
110
     *
111
     * @param string $routeName
112
     *            route's name
113
     * @param string $route
114
     *            route
115
     */
116
    protected function registerRouteName(string $routeName, string $route): void
117
    {
118
        if ($routeName != '') {
119
            $this->routeNames[$routeName] = $route;
120
        }
121
    }
122
123
    /**
124
     * Additing route for GET request
125
     *
126
     * @param string $route
127
     *            route
128
     * @param object $object
129
     *            callback object
130
     * @param string $method
131
     *            callback method
132
     */
133
    public function addGetRoute(string $route, object $object, string $method): void
134
    {
135
        $this->addRoute($route, [
136
            $object,
137
            $method
138
        ], 'GET');
139
    }
140
141
    /**
142
     * Additing route for GET request
143
     *
144
     * @param string $route
145
     *            route
146
     * @param object $object
147
     *            callback object
148
     * @param string $method
149
     *            callback method
150
     */
151
    public function addPostRoute(string $route, object $object, string $method): void
152
    {
153
        $this->addRoute($route, [
154
            $object,
155
            $method
156
        ], 'POST');
157
    }
158
159
    /**
160
     * Method adds route and it's handler
161
     *
162
     * $callback function may have two parameters - $route and $parameters. Where $route is a called route,
163
     * and $parameters is associative array (parameter name => parameter value) with URL parameters
164
     *
165
     * @param string $route
166
     *            route
167
     * @param array{0: object, 1: string}|array{0: string, 1: string}|callable|string $callback
168
     *            callback wich will be processing route call.
169
     * @param string|string[] $requestMethod
170
     *            request type
171
     * @param string $routeName
172
     *            name of the route
173
     */
174
    public function addRoute(string $route, $callback, $requestMethod = 'GET', string $routeName = ''): void
175
    {
176
        $route = Utils::prepareRoute($route);
177
178
        if (is_array($requestMethod)) {
179
            foreach ($requestMethod as $r) {
180
                $this->addRoute($route, $callback, $r, $routeName);
181
            }
182
        } else {
183
            SuppportedRequestMethods::validateRequestMethod($requestMethod);
184
185
            if (strpos($route, '[') === false) {
186
                $this->staticRoutes[$requestMethod][$route] = $callback;
0 ignored issues
show
Bug Best Practice introduced by
The property staticRoutes does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
187
            } else {
188
                $this->addParamRoute($requestMethod, $route, $callback);
0 ignored issues
show
Bug introduced by
The method addParamRoute() does not exist on Mezon\Router\RoutesSetBase. Did you maybe mean addRoute()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

188
                $this->/** @scrutinizer ignore-call */ 
189
                       addParamRoute($requestMethod, $route, $callback);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
189
            }
190
            // register route name
191
            $this->registerRouteName($routeName, $route);
192
        }
193
    }
194
195
    /**
196
     * Compiling route into URL
197
     *
198
     * @param string $routeName
199
     *            route name
200
     * @param string[] $parameters
201
     *            parameters to use in URL
202
     * @return string compiled route
203
     */
204
    public function reverse(string $routeName, array $parameters = []): string
205
    {
206
        $route = $this->getRouteByName($routeName);
207
208
        foreach ($parameters as $name => $value) {
209
            $route = preg_replace('/\[([A-Za-z_\-])\:' . $name . ']/', $value, $route);
210
        }
211
212
        return $route;
213
    }
214
}
215