Passed
Push — master ( 83779e...f4b75d )
by Evgeniy
01:22
created

RouteCollection::set()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace HttpSoft\Router;
6
7
use ArrayIterator;
8
use HttpSoft\Router\Exception\RouteAlreadyExistsException;
9
use HttpSoft\Router\Exception\RouteNotFoundException;
10
use Psr\Http\Message\ServerRequestInterface;
11
12
use function count;
13
14
final class RouteCollection implements RouteCollectionInterface
15
{
16
    /**
17
     * @var Route[]
18
     */
19
    private array $routes = [];
20
21
    /**
22
     * {@inheritDoc}
23
     */
24 38
    public function set(Route $route): void
25
    {
26 38
        $name = $route->getName();
27
28 38
        if ($this->has($name)) {
29 1
            throw RouteAlreadyExistsException::create($name);
30
        }
31
32 38
        $this->routes[$name] = $route;
33 38
    }
34
35
    /**
36
     * {@inheritDoc}
37
     */
38 20
    public function get(string $name): Route
39
    {
40 20
        if (!$this->has($name)) {
41 2
            throw RouteNotFoundException::create($name);
42
        }
43
44 18
        return $this->routes[$name];
45
    }
46
47
    /**
48
     * {@inheritDoc}
49
     */
50 5
    public function getAll(): array
51
    {
52 5
        return $this->routes;
53
    }
54
55
    /**
56
     * {@inheritDoc}
57
     */
58 1
    public function getIterator(): ArrayIterator
59
    {
60 1
        return new ArrayIterator($this->routes);
61
    }
62
63
    /**
64
     * {@inheritDoc}
65
     */
66 41
    public function has(string $name): bool
67
    {
68 41
        return isset($this->routes[$name]);
69
    }
70
71
    /**
72
     * {@inheritDoc}
73
     */
74 3
    public function remove(string $name): Route
75
    {
76 3
        if (!$this->has($name)) {
77 1
            throw new RouteNotFoundException($name);
78
        }
79
80 2
        $removed = $this->routes[$name];
81 2
        unset($this->routes[$name]);
82
83 2
        return $removed;
84
    }
85
86
    /**
87
     * {@inheritDoc}
88
     */
89 1
    public function clear(): void
90
    {
91 1
        $this->routes = [];
92 1
    }
93
94
    /**
95
     * {@inheritDoc}
96
     */
97 4
    public function count(): int
98
    {
99 4
        return count($this->routes);
100
    }
101
102
    /**
103
     * {@inheritDoc}
104
     */
105 7
    public function match(ServerRequestInterface $request, bool $checkAllowedMethods = true): ?Route
106
    {
107 7
        foreach ($this->routes as $route) {
108 7
            if (!$route->match($request)) {
109 3
                continue;
110
            }
111
112 6
            if (!$checkAllowedMethods) {
113 5
                return $route;
114
            }
115
116 1
            if ($route->isAllowedMethod($request->getMethod())) {
117 1
                return $route;
118
            }
119
        }
120
121 3
        return null;
122
    }
123
124
    /**
125
     * {@inheritDoc}
126
     */
127 7
    public function path(string $name, array $parameters = []): string
128
    {
129 7
        $route = $this->get($name);
130 7
        return $route->path($parameters);
131
    }
132
133
    /**
134
     * {@inheritDoc}
135
     */
136 10
    public function url(string $name, array $parameters = [], string $host = null, bool $secure = null): string
137
    {
138 10
        $route = $this->get($name);
139 9
        return $route->url($parameters, $host, $secure);
140
    }
141
}
142