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

CurrentRoute   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
c 1
b 0
f 0
dl 0
loc 112
ccs 25
cts 25
cp 1
rs 10
wmc 16

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getMethods() 0 3 2
A getName() 0 3 2
A getHost() 0 3 2
A getUri() 0 3 1
A getPattern() 0 3 2
A getArguments() 0 3 1
A getArgument() 0 3 1
A setUri() 0 7 2
A setRouteWithArguments() 0 8 3
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