Passed
Pull Request — master (#121)
by Rustam
07:52
created

CurrentRoute::getName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 6
rs 10
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
14
{
15
    /**
16
     * Current Route
17
     *
18
     * @var RouteParametersInterface|null
19
     */
20
    private ?RouteParametersInterface $route = null;
21
    /**
22
     * Current URI
23
     *
24
     * @var UriInterface|null
25
     */
26
    private ?UriInterface $uri = null;
27
28
    /**
29
     * Returns the current Route name
30
     *
31
     * @return string
32
     */
33
    public function getName(): string
34
    {
35
        return $this->route === null ? '' : $this->route->getName();
36
    }
37
38
    /**
39
     * Returns the current Route object
40
     *
41
     * @return RouteParametersInterface|null current route
42
     */
43 2
    public function getRoute(): ?RouteParametersInterface
44
    {
45 2
        return $this->route;
46
    }
47
48
    /**
49
     * Returns current URI
50
     *
51
     * @return UriInterface|null current URI
52
     */
53 2
    public function getUri(): ?UriInterface
54
    {
55 2
        return $this->uri;
56
    }
57
58
    /**
59
     * @param RouteParametersInterface $route
60
     */
61 4
    public function setRoute(RouteParametersInterface $route): void
62
    {
63 4
        if ($this->route === null) {
64 4
            $this->route = $route;
65 4
            return;
66
        }
67
        throw new RuntimeException('Can not set route since it was already set.');
68
    }
69
70
    /**
71
     * @param UriInterface $uri
72
     */
73 6
    public function setUri(UriInterface $uri): void
74
    {
75 6
        if ($this->uri === null) {
76 6
            $this->uri = $uri;
77 6
            return;
78
        }
79
        throw new RuntimeException('Can not set URI since it was already set.');
80
    }
81
}
82