Passed
Pull Request — master (#194)
by Alexander
02:33
created

MatchingResult   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Test Coverage

Coverage 94.59%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 29
c 2
b 0
f 0
dl 0
loc 108
ccs 35
cts 37
cp 0.9459
rs 10
wmc 17

11 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 route() 0 7 2
A arguments() 0 3 1
A isMethodFailure() 0 3 2
A withDispatcher() 0 5 1
A __construct() 0 2 1
A buildDispatcher() 0 15 3
A process() 0 8 3
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
    /**
18
     * @var array<string,string>
19
     */
20
    private array $arguments = [];
21
22
    /**
23
     * @var string[]
24
     */
25
    private array $methods = [];
26
27
    private ?MiddlewareDispatcher $dispatcher = null;
28
29 17
    private function __construct(private ?Route $route)
30
    {
31 17
    }
32
33 8
    public function withDispatcher(MiddlewareDispatcher $dispatcher): self
34
    {
35 8
        $new = clone $this;
36 8
        $new->dispatcher = $dispatcher;
37 8
        return $new;
38
    }
39
40
    /**
41
     * @param array<string,string> $arguments
42
     */
43 8
    public static function fromSuccess(Route $route, array $arguments): self
44
    {
45 8
        $new = new self($route);
46 8
        $new->arguments = $arguments;
47 8
        return $new;
48
    }
49
50
    /**
51
     * @param string[] $methods
52
     */
53 9
    public static function fromFailure(array $methods): self
54
    {
55 9
        $new = new self(null);
56 9
        $new->methods = $methods;
57 9
        return $new;
58
    }
59
60
    /**
61
     * @psalm-assert-if-true !null $this->route
62
     */
63 12
    public function isSuccess(): bool
64
    {
65 12
        return $this->route !== null;
66
    }
67
68 12
    public function isMethodFailure(): bool
69
    {
70 12
        return $this->route === null && $this->methods !== Method::ALL;
71
    }
72
73
    /**
74
     * @return array<string,string>
75
     */
76 7
    public function arguments(): array
77
    {
78 7
        return $this->arguments;
79
    }
80
81
    /**
82
     * @return string[]
83
     */
84 4
    public function methods(): array
85
    {
86 4
        return $this->methods;
87
    }
88
89 7
    public function route(): Route
90
    {
91 7
        if ($this->route === null) {
92 1
            throw new RuntimeException('There is no route in the matching result.');
93
        }
94
95 6
        return $this->route;
96
    }
97
98 8
    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
99
    {
100 8
        if ($this->isSuccess() && $this->dispatcher !== null) {
101 7
            return $this->buildDispatcher($this->dispatcher, $this->route)
102 7
                ->dispatch($request, $handler);
103
        }
104
105 1
        return $handler->handle($request);
106
    }
107
108 7
    private function buildDispatcher(
109
        ?MiddlewareDispatcher $dispatcher,
110
        Route $route,
111
    ): MiddlewareDispatcher {
112 7
        if ($dispatcher === null) {
113
            throw new RuntimeException(sprintf('There is no dispatcher in the route %s.', $route->name));
0 ignored issues
show
Bug introduced by
The property name is declared private in Yiisoft\Router\Route and cannot be accessed from this context.
Loading history...
114
        }
115
116
        // Don't add middlewares to dispatcher if we did it earlier.
117
        // This improves performance in event-loop applications.
118 7
        if ($dispatcher->hasMiddlewares()) {
119
            return $dispatcher;
120
        }
121
122 7
        return $dispatcher->withMiddlewares($route->getMiddlewares());
123
    }
124
}
125