Passed
Push — master ( f8dde9...fe9361 )
by Alexander
10:36
created

MatchingResult::isMethodFailure()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
cc 2
nc 2
nop 0
crap 2
1
<?php
2
3
namespace Yiisoft\Router;
4
5
use Psr\Http\Message\ResponseInterface;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Server\MiddlewareInterface;
8
use Psr\Http\Server\RequestHandlerInterface;
9
use Yiisoft\Http\Method;
10
11
final class MatchingResult implements MiddlewareInterface
12
{
13
    private bool $success;
14
    private Route $route;
15
    private array $parameters = [];
16
    private array $methods = [];
17
    private ?DispatcherInterface $dispatcher = null;
18
19
    private function __construct()
20 8
    {
21
    }
22 8
23
    public function withDispatcher(DispatcherInterface $dispatcher): self
24 1
    {
25
        $new = clone $this;
26 1
        $new->dispatcher = $dispatcher;
27 1
        return $new;
28 1
    }
29
30
    public static function fromSuccess(Route $route, array $parameters): self
31 3
    {
32
        $new = new self();
33 3
        $new->success = true;
34 3
        $new->route = $route;
35 3
        $new->parameters = $parameters;
36 3
        return $new;
37 3
    }
38
39
    public static function fromFailure(array $methods): self
40 5
    {
41
        $new = new self();
42 5
        $new->methods = $methods;
43 5
        $new->success = false;
44 5
        return $new;
45 5
    }
46
47
    public function isSuccess(): bool
48 5
    {
49
        return $this->success;
50 5
    }
51
52
    public function isMethodFailure(): bool
53 5
    {
54
        return !$this->success && $this->methods !== Method::ANY;
55 5
    }
56
57
    public function parameters(): array
58 2
    {
59
        return $this->parameters;
60 2
    }
61
62
    public function methods(): array
63 2
    {
64
        return $this->methods;
65 2
    }
66
67
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
68 3
    {
69
        if ($this->success === false) {
70 3
            return $handler->handle($request);
71 1
        }
72
        $route = $this->route;
73 2
74 2
        if ($this->dispatcher !== null && !$route->hasDispatcher()) {
75 1
            $route = $route->withDispatcher($this->dispatcher);
76
        }
77
78 2
        return $route->getDispatcherWithMiddlewares()->dispatch($request, $handler);
79
    }
80
}
81