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

CurrentRoute::setArguments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
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
     * @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 URI.
74
     *
75
     * @return UriInterface|null The current URI.
76
     */
77 2
    public function getUri(): ?UriInterface
78
    {
79 2
        return $this->uri;
80
    }
81
82
    /**
83
     * @param Route $route
84
     * @param array $arguments
85
     *
86
     * @internal
87
     */
88 16
    public function setRouteWithArguments(Route $route, array $arguments): void
89
    {
90 16
        if ($this->route === null && $this->arguments === []) {
91 16
            $this->route = $route;
92 16
            $this->arguments = $arguments;
93 16
            return;
94
        }
95 2
        throw new LogicException('Can not set route/arguments since it was already set.');
96
    }
97
98
    /**
99
     * @param UriInterface $uri
100
     *
101
     * @internal
102
     */
103 12
    public function setUri(UriInterface $uri): void
104
    {
105 12
        if ($this->uri === null) {
106 12
            $this->uri = $uri;
107 12
            return;
108
        }
109 1
        throw new LogicException('Can not set URI since it was already set.');
110
    }
111
112 2
    public function getArguments(): array
113
    {
114 2
        return $this->arguments;
115
    }
116
117 3
    public function getArgument(string $name, string $default = null): ?string
118
    {
119 3
        return $this->arguments[$name] ?? $default;
120
    }
121
}
122