Passed
Pull Request — master (#140)
by Alexander
02:11
created

MatchingResult   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Test Coverage

Coverage 96.97%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 27
dl 0
loc 97
ccs 32
cts 33
cp 0.9697
rs 10
c 2
b 0
f 0
wmc 15

10 Methods

Rating   Name   Duplication   Size   Complexity  
A isSuccess() 0 3 1
A fromSuccess() 0 5 1
A methods() 0 3 1
A fromFailure() 0 5 1
A process() 0 11 4
A route() 0 7 2
A arguments() 0 3 1
A isMethodFailure() 0 3 2
A withDispatcher() 0 5 1
A __construct() 0 3 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 RuntimeException;
12
use Yiisoft\Http\Method;
13
use Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher;
14
15
final class MatchingResult implements MiddlewareInterface
16
{
17
    private ?Route $route;
18
19
    /**
20
     * @var string[]
21
     */
22
    private array $arguments = [];
23
24
    /**
25
     * @var string[]
26
     */
27
    private array $methods = [];
28
29
    private ?MiddlewareDispatcher $dispatcher = null;
30
31 15
    private function __construct(?Route $route)
32
    {
33 15
        $this->route = $route;
34 15
    }
35
36 6
    public function withDispatcher(MiddlewareDispatcher $dispatcher): self
37
    {
38 6
        $new = clone $this;
39 6
        $new->dispatcher = $dispatcher;
40 6
        return $new;
41
    }
42
43
    /**
44
     * @param string[] $arguments
45
     */
46 8
    public static function fromSuccess(Route $route, array $arguments): self
47
    {
48 8
        $new = new self($route);
49 8
        $new->arguments = $arguments;
50 8
        return $new;
51
    }
52
53
    /**
54
     * @param string[] $methods
55
     */
56 7
    public static function fromFailure(array $methods): self
57
    {
58 7
        $new = new self(null);
59 7
        $new->methods = $methods;
60 7
        return $new;
61
    }
62
63
    /**
64
     * @psalm-assert-if-true !null $this->route
65
     */
66 12
    public function isSuccess(): bool
67
    {
68 12
        return $this->route !== null;
69
    }
70
71 12
    public function isMethodFailure(): bool
72
    {
73 12
        return $this->route === null && $this->methods !== Method::ALL;
74
    }
75
76
    /**
77
     * @return string[]
78
     */
79 7
    public function arguments(): array
80
    {
81 7
        return $this->arguments;
82
    }
83
84
    /**
85
     * @return string[]
86
     */
87 4
    public function methods(): array
88
    {
89 4
        return $this->methods;
90
    }
91
92 6
    public function route(): Route
93
    {
94 6
        if ($this->route === null) {
95
            throw new RuntimeException('There is no route in the matching result.');
96
        }
97
98 6
        return $this->route;
99
    }
100
101 8
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
102
    {
103 8
        if (!$this->isSuccess()) {
104 1
            return $handler->handle($request);
105
        }
106
107 7
        if ($this->dispatcher !== null && !$this->route->getData('hasDispatcher')) {
108 6
            $this->route->injectDispatcher($this->dispatcher);
109
        }
110
111 7
        return $this->route->getData('dispatcherWithMiddlewares')->dispatch($request, $handler);
112
    }
113
}
114