Passed
Push — master ( b75818...b2c04e )
by Alexander
11:23
created

MatchingResult::isMethodFailure()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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