Passed
Push — master ( 34a32b...42166e )
by Alexander
07:42
created

CurrentRoute::getArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Router;
6
7
use Psr\Http\Message\UriInterface;
8
use LogicException;
9
10
/**
11
 * Holds information about current route e.g. matched last.
12
 */
13
final class CurrentRoute implements CurrentRouteInterface
14
{
15
    /**
16
     * Current Route
17
     *
18
     * @var RouteParametersInterface|null
19
     */
20
    private ?RouteParametersInterface $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 1
    public function getName(): ?string
38
    {
39 1
        return $this->route !== null ? $this->route->getName() : null;
40
    }
41
42
    /**
43
     * Returns the current route object.
44
     *
45
     * @return RouteParametersInterface|null The current route.
46
     */
47 2
    public function getRoute(): ?RouteParametersInterface
48
    {
49 2
        return $this->route;
50
    }
51
52
    /**
53
     * Returns the current URI.
54
     *
55
     * @return UriInterface|null The current URI.
56
     */
57 2
    public function getUri(): ?UriInterface
58
    {
59 2
        return $this->uri;
60
    }
61
62
    /**
63
     * @param RouteParametersInterface $route
64
     */
65 8
    public function setRoute(RouteParametersInterface $route): void
66
    {
67 8
        if ($this->route === null) {
68 8
            $this->route = $route;
69 8
            return;
70
        }
71 1
        throw new LogicException('Can not set route since it was already set.');
72
    }
73
74
    /**
75
     * @param UriInterface $uri
76
     */
77 11
    public function setUri(UriInterface $uri): void
78
    {
79 11
        if ($this->uri === null) {
80 11
            $this->uri = $uri;
81 11
            return;
82
        }
83 1
        throw new LogicException('Can not set URI since it was already set.');
84
    }
85
86 2
    public function getArguments(): array
87
    {
88 2
        return $this->arguments;
89
    }
90
91 3
    public function getArgument(string $name, $default = null): ?string
92
    {
93 3
        return $this->arguments[$name] ?? $default;
94
    }
95
96 10
    public function setArguments(array $arguments): void
97
    {
98 10
        if ($this->arguments === []) {
99 10
            $this->arguments = $arguments;
100 10
            return;
101
        }
102 1
        throw new LogicException('Can not set arguments since it was already set.');
103
    }
104
}
105