Completed
Pull Request — master (#50)
by
unknown
03:45
created

Routes   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 0
loc 45
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B containsUrl() 0 33 7
1
<?php
2
3
namespace Aoe\Restler\System\Restler;
4
5
class Routes extends \Luracast\Restler\Routes
6
{
7
    /**
8
     * Check if a uri is handled by restler.
9
     *
10
     * Performs a preg_match after replacing the argument placeholders of the restler uris with matching
11
     * regular expresionns.
12
     *
13
     * @param string $uri Uri to check
14
     * @return bool If uri is handled by restler
15
     */
16
    public static function containsUrl(string $uri): bool
17
    {
18
        foreach (self::findAll() as $routes) {
19
            foreach ($routes as $route) {
20
                $routeMatcher = '/^' . preg_quote('/' . rtrim($route['route']['url'], '/*'), '/') . '/';
21
22
                if (is_array($route['route']['arguments'])) {
23
                    foreach ($route['route']['arguments'] as $argumentName => $argumentNumber) {
24
                        $metadataType = $route['route']['metadata']['param'][$argumentNumber];
25
26
                        $argumentReplace = '[^\/]+';
27
28
                        switch ($metadataType['type']) {
29
                            case 'integer':
30
                                $argumentReplace = '[\d]+';
31
                                break;
32
                        }
33
34
                        $routeMatcher = str_replace(
35
                            preg_quote('{' . $argumentName . '}', '/'),
36
                            $argumentReplace,
37
                            $routeMatcher
38
                        );
39
                    }
40
                }
41
                if (preg_match($routeMatcher, $uri) === 1) {
42
                    return true;
43
                }
44
            }
45
        }
46
47
        return false;
48
    }
49
}
50