MethodAndPathMatcher   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 31
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A match() 0 16 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