UriMatcher::match()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6.0087

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 30
ccs 15
cts 16
cp 0.9375
rs 9.0777
c 0
b 0
f 0
cc 6
nc 6
nop 2
crap 6.0087
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 *  This file is part of the Micro framework package.
7
 *
8
 *  (c) Stanislau Komar <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace Micro\Plugin\Http\Business\Matcher\Route\Matchers;
15
16
use Micro\Plugin\Http\Business\Matcher\Route\RouteMatcherInterface;
17
use Micro\Plugin\Http\Business\Route\RouteInterface;
18
use Symfony\Component\HttpFoundation\Request;
19
20
/**
21
 * @author Stanislau Komar <[email protected]>
22
 */
23
class UriMatcher implements RouteMatcherInterface
24
{
25
    /**
26
     * {@inheritDoc}
27
     */
28
    public function match(RouteInterface $route, Request $request): bool
29
    {
30 10
        $pathInfo = $request->getPathInfo();
31 10
        $pattern = $route->getPattern();
32 10
        if (!$pattern) {
33 3
            return $pathInfo === $route->getUri();
34
        }
35
36 8
        $matched = preg_match_all($pattern, $pathInfo, $matches);
37
38 8
        if (0 === $matched) {
39 1
            return false;
40
        }
41
42 7
        $i = 0;
43
44 7
        $parameters = $route->getParameters();
45 7
        if (!$parameters) {
46 1
            return true;
47
        }
48
49 6
        foreach ($parameters as $parameter) {
50 6
            if (!$parameter) {
51
                continue;
52
            }
53
54 6
            $request->request->set($parameter, $matches[++$i][0]);
55
        }
56
57 6
        return true;
58
    }
59
}
60