Passed
Pull Request — master (#118)
by Alexander
03:16 queued 01:20
created

CurrentRoute::setUri()   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 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 2
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 4
cts 5
cp 0.8
crap 2.032
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 object
30
     *
31
     * @return RouteParametersInterface|null current route
32
     */
33 2
    public function getRoute(): ?RouteParametersInterface
34
    {
35 2
        return $this->route;
36
    }
37
38
    /**
39
     * Returns current URI
40
     *
41
     * @return UriInterface|null current URI
42
     */
43 2
    public function getUri(): ?UriInterface
44
    {
45 2
        return $this->uri;
46
    }
47
48
    /**
49
     * @param RouteParametersInterface $route
50
     */
51 4
    public function setRoute(RouteParametersInterface $route): void
52
    {
53 4
        if ($this->route === null) {
54 4
            $this->route = $route;
55 4
            return;
56
        }
57
        throw new RuntimeException('Can not set route since it was already set.');
58
    }
59
60
    /**
61
     * @param UriInterface $uri
62
     */
63 6
    public function setUri(UriInterface $uri): void
64
    {
65 6
        if ($this->uri === null) {
66 6
            $this->uri = $uri;
67 6
            return;
68
        }
69
        throw new RuntimeException('Can not set URI since it was already set.');
70
    }
71
}
72