MethodAndPathMatcher::match()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 9.7333
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3
1
<?php
2
declare(strict_types=1);
3
4
namespace N1215\Hakudo\RequestMatcher;
5
6
use N1215\Http\RequestMatcher\RequestMatcherInterface;
7
use N1215\Http\RequestMatcher\RequestMatchResult;
8
use N1215\Http\RequestMatcher\RequestMatchResultInterface;
9
use Psr\Http\Message\RequestInterface;
10
11
class MethodAndPathMatcher implements RequestMatcherInterface {
12
13
    /** @var string */
14
    private $method;
15
16
    /** @var string */
17
    private $pathRegex;
18
19 2
    public function __construct(string $method, string $pathRegex)
20
    {
21 2
        $this->method = $method;
22 2
        $this->pathRegex = $pathRegex;
23 2
    }
24
25 2
    public function match(RequestInterface $request): RequestMatchResultInterface
26
    {
27 2
        if (strtolower($this->method) !== strtolower($request->getMethod())) {
28 2
            return RequestMatchResult::failure();
29
        }
30
31 1
        if (!preg_match($this->pathRegex, $request->getUri()->getPath(), $matches)) {
32 1
            return RequestMatchResult::failure();
33
        }
34
35
        $params = array_filter($matches, function($key) {
36 1
            return is_string($key);
37 1
        }, ARRAY_FILTER_USE_KEY);
38
39 1
        return RequestMatchResult::success($params);
40
    }
41
}
42