Passed
Pull Request — master (#118)
by Alexander
03:16 queued 01:20
created

MatchingResult::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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