Passed
Pull Request — master (#138)
by Rustam
02:29
created

CurrentRoute   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Test Coverage

Coverage 94.29%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
dl 0
loc 149
ccs 33
cts 35
cp 0.9429
rs 10
c 1
b 0
f 0
wmc 22

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 2
A isOverride() 0 3 2
A getArguments() 0 3 1
A getRoute() 0 3 1
A getArgument() 0 3 1
A setRoute() 0 7 2
A setArguments() 0 7 2
A setUri() 0 7 2
A getUri() 0 3 1
A getMethods() 0 3 2
A getHost() 0 3 2
A getPattern() 0 3 2
A getDefaults() 0 3 2
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
     * @var Route|null
19
     */
20
    private ?Route $route = null;
21
22
    /**
23
     * Current URI
24
     */
25
    private ?UriInterface $uri = null;
26
27
    /**
28
     * Current Route arguments.
29
     */
30
    private array $arguments = [];
31
32
    /**
33
     * Returns the current route name.
34
     *
35
     * @return string|null The current route name.
36
     */
37 2
    public function getName(): ?string
38
    {
39 2
        return $this->route !== null ? $this->route->getData('name') : null;
40
    }
41
42
    /**
43
     * Returns the current route host.
44
     *
45
     * @return string|null The current route host.
46
     */
47 1
    public function getHost(): ?string
48
    {
49 1
        return $this->route !== null ? $this->route->getData('host') : null;
50
    }
51
52
    /**
53
     * Returns the current route pattern.
54
     *
55
     * @return string|null The current route pattern.
56
     */
57 1
    public function getPattern(): ?string
58
    {
59 1
        return $this->route !== null ? $this->route->getData('pattern') : null;
60
    }
61
62
    /**
63
     * Returns the current route methods.
64
     *
65
     * @return array|null The current route methods.
66
     */
67 1
    public function getMethods(): ?array
68
    {
69 1
        return $this->route !== null ? $this->route->getData('methods') : null;
70
    }
71
72
    /**
73
     * Returns the current route defaults.
74
     *
75
     * @return array|null The current route defaults.
76
     */
77 1
    public function getDefaults(): ?array
78
    {
79 1
        return $this->route !== null ? $this->route->getData('defaults') : null;
80
    }
81
82
    /**
83
     * Returns the current route override.
84
     *
85
     * @return bool|null The current route override.
86
     */
87
    public function isOverride(): ?bool
88
    {
89
        return $this->route !== null ? $this->route->getData('override') : null;
90
    }
91
92
    /**
93
     * Returns the current route object.
94
     *
95
     * @return Route|null The current route.
96
     */
97 2
    public function getRoute(): ?Route
98
    {
99 2
        return $this->route;
100
    }
101
102
    /**
103
     * Returns the current URI.
104
     *
105
     * @return UriInterface|null The current URI.
106
     */
107 2
    public function getUri(): ?UriInterface
108
    {
109 2
        return $this->uri;
110
    }
111
112
    /**
113
     * @param Route $route
114
     *
115
     * @internal
116
     */
117 13
    public function setRoute(Route $route): void
118
    {
119 13
        if ($this->route === null) {
120 13
            $this->route = $route;
121 13
            return;
122
        }
123 1
        throw new LogicException('Can not set route since it was already set.');
124
    }
125
126
    /**
127
     * @param UriInterface $uri
128
     *
129
     * @internal
130
     */
131 12
    public function setUri(UriInterface $uri): void
132
    {
133 12
        if ($this->uri === null) {
134 12
            $this->uri = $uri;
135 12
            return;
136
        }
137 1
        throw new LogicException('Can not set URI since it was already set.');
138
    }
139
140 2
    public function getArguments(): array
141
    {
142 2
        return $this->arguments;
143
    }
144
145 3
    public function getArgument(string $name, string $default = null): ?string
146
    {
147 3
        return $this->arguments[$name] ?? $default;
148
    }
149
150
    /**
151
     * @param array $arguments
152
     *
153
     * @internal
154
     */
155 11
    public function setArguments(array $arguments): void
156
    {
157 11
        if ($this->arguments === []) {
158 11
            $this->arguments = $arguments;
159 11
            return;
160
        }
161 1
        throw new LogicException('Can not set arguments since it was already set.');
162
    }
163
}
164