Passed
Push — master ( 34a32b...42166e )
by Alexander
07:42
created

MatchingResult::methods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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