Passed
Pull Request — master (#121)
by Rustam
02:22
created

CurrentRoute   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 87.5%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 14
c 3
b 0
f 0
dl 0
loc 67
ccs 14
cts 16
cp 0.875
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getRoute() 0 3 1
A setRoute() 0 7 2
A setUri() 0 7 2
A getUri() 0 3 1
A getName() 0 3 2
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|null
32
     */
33 1
    public function getName(): ?string
34
    {
35 1
        return $this->route !== null ? $this->route->getName() : null;
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 5
    public function setRoute(RouteParametersInterface $route): void
62
    {
63 5
        if ($this->route === null) {
64 5
            $this->route = $route;
65 5
            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