Passed
Pull Request — master (#128)
by Rustam
02:46
created

CurrentRoute::setParameters()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 5
cp 0.8
crap 2.032
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 RuntimeException;
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 parameters
29
     */
30
    private array $parameters = [];
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 7
    public function setRoute(RouteParametersInterface $route): void
66
    {
67 7
        if ($this->route === null) {
68 7
            $this->route = $route;
69 7
            return;
70
        }
71
        throw new RuntimeException('Can not set route since it was already set.');
72
    }
73
74
    /**
75
     * @param UriInterface $uri
76
     */
77 10
    public function setUri(UriInterface $uri): void
78
    {
79 10
        if ($this->uri === null) {
80 10
            $this->uri = $uri;
81 10
            return;
82
        }
83
        throw new RuntimeException('Can not set URI since it was already set.');
84
    }
85
86 2
    public function getParameters(): array
87
    {
88 2
        return $this->parameters;
89
    }
90
91 3
    public function getParameter(string $name, $default = null): ?string
92
    {
93 3
        return $this->parameters[$name] ?? $default;
94
    }
95
96 9
    public function setParameters(array $parameters)
97
    {
98 9
        if ($this->parameters === []) {
99 9
            $this->parameters = $parameters;
100 9
            return;
101
        }
102
        throw new RuntimeException('Can not set parameters since it was already set.');
103
    }
104
}
105