Passed
Pull Request — master (#91)
by Alexander
09:37 queued 07:29
created

FallbackMatcher::getCurrentRoute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Router;
6
7
use Psr\Http\Message\ServerRequestInterface;
8
use Psr\Http\Message\UriInterface;
9
10
class FallbackMatcher implements UrlMatcherInterface
11
{
12
    private UrlMatcherInterface $urlMatcher;
13
    private ?Route $currentRoute = null;
14
    private ?UriInterface $currentUri = null;
15
    private bool $isFallback = false;
16
17
    public function __construct(UrlMatcherInterface $urlMatcher)
18
    {
19
        $this->urlMatcher = $urlMatcher;
20
    }
21
22
    public function match(ServerRequestInterface $request): MatchingResult
23
    {
24
        $result = $this->urlMatcher->match($request);
25
        if (!$result->isSuccess()) {
26
            $this->isFallback = true;
27
            // fallback match
28
            // $this->currentRoute = ...
29
            // $this->currentUrl = ...
30
        }
31
        return $result;
32
    }
33
34
    public function getCurrentRoute(): ?Route
35
    {
36
        return $this->isFallback ? $this->currentRoute : $this->urlMatcher->getCurrentRoute();
37
    }
38
39
    public function getCurrentUri(): ?UriInterface
40
    {
41
        return $this->isFallback ? $this->currentUri : $this->urlMatcher->getCurrentUri();
42
    }
43
}
44