Passed
Push — master ( 6abeaa...528eb4 )
by n
01:44
created

MethodAndPathMatcher::match()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
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 1
    public function __construct(string $method, string $pathRegex)
20
    {
21 1
        $this->method = $method;
22 1
        $this->pathRegex = $pathRegex;
23 1
    }
24
25 1
    public function match(RequestInterface $request): RequestMatchResultInterface
26
    {
27 1
        if (strtolower($this->method) !== strtolower($request->getMethod())) {
28 1
            return RequestMatchResult::failure();
29
        }
30
31 1
        if (!preg_match($this->pathRegex, $request->getUri()->getPath(), $matches)) {
32 1
            return RequestMatchResult::failure();
33
        }
34
35 1
        $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