Conditions | 32 |
Paths | 110 |
Total Lines | 181 |
Code Lines | 94 |
Lines | 0 |
Ratio | 0 % |
Changes | 11 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
211 | public function isEndpoint($api, $endpoint) |
||
212 | { |
||
213 | /** |
||
214 | * Return if it's a system endpoint |
||
215 | */ |
||
216 | if (null !== ($endpointSettings = $this->isSystemEndpoint($api, $endpoint))) { |
||
217 | return $endpointSettings; |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Check if the endpoint is a RouterInterface |
||
222 | */ |
||
223 | $endpoint = htmlspecialchars($endpoint, ENT_QUOTES, 'UTF-8'); |
||
224 | $index = array_search($api, $this->BASE_ENDPOINTS); |
||
225 | |||
226 | if ($index !== false) { |
||
227 | if (isset($this->registry[$api])) { |
||
228 | $endpointSettings = array( |
||
229 | 'path' => $endpoint, |
||
230 | 'model' => array( |
||
231 | 'namespace' => $this->NAMESPACE_ENDPOINTS[$api], |
||
232 | 'class' => $this->BASE_ENDPOINTS[$index], |
||
233 | 'method' => basename($endpoint), |
||
234 | 'scope' => 'private', |
||
235 | ), |
||
236 | ); |
||
237 | |||
238 | /** |
||
239 | * Nothing dynamic, found an exact match |
||
240 | * @var array |
||
241 | */ |
||
242 | if (array_search($endpoint, $this->registry[$api]) !== false) { |
||
243 | if (method_exists($this->NAMESPACE_ENDPOINTS[$api], 'scope')) { |
||
244 | $classScope = (new $this->NAMESPACE_ENDPOINTS[$api]())->scope(); |
||
245 | $position = array_search($endpoint, $this->registry[$api]); |
||
246 | |||
247 | if (is_array($classScope) && isset($classScope[$position])) { |
||
248 | $endpointSettings['model']['scope'] = $classScope[$position]; |
||
249 | } else { |
||
250 | if (!is_array($classScope)) { |
||
251 | $endpointSettings['model']['scope'] = $classScope; |
||
252 | } |
||
253 | } |
||
254 | } |
||
255 | return (object) $endpointSettings; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Check for exact match in middleware routes |
||
260 | */ |
||
261 | foreach ($this->registry[$api] as $i => $path) { |
||
262 | if ($path instanceof \responsible\core\endpoints\RouteMiddlewareInterface) { |
||
263 | $middlewareRoute = $path->getRoute(); |
||
264 | $scope = $path->getScope(); |
||
265 | |||
266 | if ($this->isWildcardEndpoint($middlewareRoute, $endpoint)) { |
||
267 | // If the endpoint is a wildcard endpoint, we can return it directly |
||
268 | if ($scope == 'public') { |
||
269 | $scope = 'anonymous'; |
||
270 | } |
||
271 | $endpointSettings['model']['scope'] = $scope; |
||
272 | $endpointSettings['model']['middleware'] = $path; |
||
273 | $endpointSettings['model']['method'] = ''; |
||
274 | return (object) $endpointSettings; |
||
275 | } |
||
276 | |||
277 | if ($middlewareRoute === $endpoint) { |
||
278 | $scope = $path->getScope(); |
||
279 | if (isset($_SERVER['REQUEST_METHOD'])) { |
||
280 | $serverMethod = $_SERVER['REQUEST_METHOD']; |
||
281 | if ($path->getVerb() !== $serverMethod) { |
||
282 | continue; |
||
283 | } |
||
284 | } |
||
285 | if ($scope == 'public') { |
||
286 | $scope = 'anonymous'; |
||
287 | } |
||
288 | $endpointSettings['model']['scope'] = $scope; |
||
289 | $endpointSettings['model']['middleware'] = $path; |
||
290 | return (object) $endpointSettings; |
||
291 | } |
||
292 | } |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * Check for dynamic uri eg: {asset_id} |
||
297 | * Dynamic uri's must be wrapped in {} for a true return |
||
298 | */ |
||
299 | foreach ($this->registry[$api] as $i => $path) { |
||
300 | $endpointRegister = $path; |
||
301 | $methodArgs = []; |
||
302 | |||
303 | $routeInstance = null; |
||
304 | if ($path instanceof \responsible\core\endpoints\RouteMiddlewareInterface) { |
||
305 | $routeInstance = $path; |
||
306 | $endpointRegister = $path = $routeInstance->getRoute(); |
||
307 | $scope = $routeInstance->getScope(); |
||
|
|||
308 | |||
309 | if (isset($_SERVER['REQUEST_METHOD'])) { |
||
310 | $serverMethod = $_SERVER['REQUEST_METHOD']; |
||
311 | if ($routeInstance->getVerb() !== $serverMethod) { |
||
312 | continue; |
||
313 | } |
||
314 | } else { |
||
315 | // If no request method is set then continue to force a bad request |
||
316 | continue; |
||
317 | } |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * If comparing the two sizes are not equal |
||
322 | * then no use continuing through the loop |
||
323 | */ |
||
324 | if (!$this->uriCheckSize($endpointRegister, $endpoint)) { |
||
325 | continue; |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * This replacment will create a pattern to use as a match all expression |
||
330 | * @var [string] |
||
331 | */ |
||
332 | $endpointRegister = preg_replace('@/{(.*?)}@', '/(.*?)', $endpointRegister); |
||
333 | |||
334 | if (preg_match_all('@^' . $endpointRegister . '$@i', $endpoint, $dynamicParts)) { |
||
335 | $endpointFilter = $this->filterParts($endpoint, $dynamicParts); |
||
336 | $model = $this->getClassModel($path); |
||
337 | |||
338 | /** |
||
339 | * Find the dynamic parts and set them as argument key(s) |
||
340 | * then combine them with the endpoint request and set the request parts |
||
341 | * as argument value(s) |
||
342 | */ |
||
343 | if (preg_match_all("/(?<={).*?(?=})/", $path, $registerParts)) { |
||
344 | if (isset($registerParts[0][0])) { |
||
345 | $registerParts = $registerParts[0]; |
||
346 | |||
347 | if (sizeof($endpointFilter) == sizeof($registerParts)) { |
||
348 | $methodArgs = array_combine($registerParts, $endpointFilter); |
||
349 | } |
||
350 | } |
||
351 | } |
||
352 | |||
353 | $scope = 'private'; |
||
354 | if ($routeInstance instanceof \responsible\core\endpoints\RouteMiddlewareInterface) { |
||
355 | $scope = $routeInstance->getScope(); |
||
356 | if ($scope == 'public') { |
||
357 | $scope = 'anonymous'; |
||
358 | } |
||
359 | } |
||
360 | |||
361 | if (method_exists($this->NAMESPACE_ENDPOINTS[$api], 'scope')) { |
||
362 | $classScope = (new $this->NAMESPACE_ENDPOINTS[$api]())->scope(); |
||
363 | $position = array_search($path, $this->registry[$api]); |
||
364 | |||
365 | if (is_array($classScope) && isset($classScope[$position])) { |
||
366 | $scope = $classScope[$position]; |
||
367 | } else { |
||
368 | if (!is_array($classScope)) { |
||
369 | $scope = $classScope; |
||
370 | } |
||
371 | } |
||
372 | } |
||
373 | |||
374 | $endpointSettings['model'] = array( |
||
375 | 'scope' => $scope, |
||
376 | 'namespace' => $this->NAMESPACE_ENDPOINTS[$api], |
||
377 | 'class' => $model['class'], |
||
378 | 'method' => $model['method'], |
||
379 | 'arguments' => $methodArgs, |
||
380 | 'middleware' => $routeInstance, |
||
381 | ); |
||
382 | |||
383 | return (object) $endpointSettings; |
||
384 | } else { |
||
385 | continue; |
||
386 | } |
||
387 | } |
||
388 | } |
||
389 | } |
||
390 | |||
391 | return; |
||
392 | } |
||
473 |