Passed
Pull Request — master (#173)
by Rustam
02:30
created

CurrentRoute::setRouteWithArguments()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 2
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Router;
6
7
use LogicException;
8
use Psr\Http\Message\UriInterface;
9
10
/**
11
 * Holds information about current route e.g. matched last.
12
 */
13
final class CurrentRoute
14
{
15
    /**
16
     * Current Route.
17
     */
18
    private ?Route $route = null;
19
20
    /**
21
     * Current URI.
22
     */
23
    private ?UriInterface $uri = null;
24
25
    /**
26
     * Current Route arguments.
27
     *
28
     * @var string[]
29
     *
30
     * @psalm-var array<string, string>
31
     */
32
    private array $arguments = [];
33
34
    /**
35
     * Returns the current route name.
36
     *
37
     * @return string|null The current route name.
38
     */
39 3
    public function getName(): ?string
40
    {
41 3
        return $this->route !== null ? $this->route->getData('name') : null;
42
    }
43
44
    /**
45
     * Returns the current route host.
46
     *
47
     * @return string|null The current route host.
48
     */
49 1
    public function getHost(): ?string
50
    {
51 1
        return $this->route !== null ? $this->route->getData('host') : null;
52
    }
53
54
    /**
55
     * Returns the current route pattern.
56
     *
57
     * @return string|null The current route pattern.
58
     */
59 1
    public function getPattern(): ?string
60
    {
61 1
        return $this->route !== null ? $this->route->getData('pattern') : null;
62
    }
63
64
    /**
65
     * Returns the current route methods.
66
     *
67
     * @return string[]|null The current route methods.
68
     */
69 1
    public function getMethods(): ?array
70
    {
71 1
        return $this->route !== null ? $this->route->getData('methods') : null;
72
    }
73
74
    /**
75
     * Returns the current URI.
76
     *
77
     * @return UriInterface|null The current URI.
78
     */
79 3
    public function getUri(): ?UriInterface
80
    {
81 3
        return $this->uri;
82
    }
83
84
    /**
85
     * @param string[] $arguments
86
     *
87
     * @psalm-param array<string,string> $arguments
88
     *
89
     * @internal
90
     */
91 17
    public function setRouteWithArguments(Route $route, array $arguments): void
92
    {
93 17
        if ($this->route === null && $this->arguments === []) {
94 17
            $this->route = $route;
95 17
            $this->arguments = $arguments;
96 17
            return;
97
        }
98 2
        throw new LogicException('Can not set route/arguments since it was already set.');
99
    }
100
101
    /**
102
     * @internal
103
     */
104 13
    public function setUri(UriInterface $uri): void
105
    {
106 13
        if ($this->uri === null) {
107 13
            $this->uri = $uri;
108 13
            return;
109
        }
110 1
        throw new LogicException('Can not set URI since it was already set.');
111
    }
112
113
    /**
114
     * @return string[] Arguments.
115
     * @psalm-return array<string, string>
116
     */
117 3
    public function getArguments(): array
118
    {
119 3
        return $this->arguments;
120
    }
121
122 3
    public function getArgument(string $name, ?string $default = null): ?string
123
    {
124 3
        return $this->arguments[$name] ?? $default;
125
    }
126
}
127