Routes::containsUrl()   B
last analyzed

Complexity

Conditions 7
Paths 6

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 16
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 31
ccs 0
cts 14
cp 0
crap 56
rs 8.8333
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((string) $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
                        if ($metadataType['type'] === 'integer') {
29
                            $argumentReplace = '[\d]+';
30
                        }
31
32
                        $routeMatcher = str_replace(
33
                            preg_quote('{' . $argumentName . '}', '/'),
34
                            $argumentReplace,
35
                            $routeMatcher
36
                        );
37
                    }
38
                }
39
40
                if (preg_match($routeMatcher, $uri) === 1) {
41
                    return true;
42
                }
43
            }
44
        }
45
46
        return false;
47
    }
48
}
49