RouteCollection   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
eloc 12
dl 0
loc 46
ccs 14
cts 14
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 3 1
A get() 0 7 2
A has() 0 3 1
A getIterator() 0 4 2
A count() 0 3 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Northwoods\Router;
5
6
final class RouteCollection implements
7
    \Countable,
8
    \IteratorAggregate
9
{
10
    /** @var Route[] */
11
    private $routes = [];
12
13
    /**
14
     * Check if a route exists in the collection.
15
     */
16 5
    public function has(string $name): bool
17
    {
18 5
        return isset($this->routes[$name]);
19
    }
20
21
    /**
22
     * Get a route from the collection.
23
     */
24 5
    public function get(string $name): Route
25
    {
26 5
        if (! $this->has($name)) {
27 1
            throw Error\RouteNotSetException::from($name);
28
        }
29
30 4
        return $this->routes[$name];
31
    }
32
33
    /**
34
     * Set a route in the collection.
35
     */
36 14
    public function set(string $name, Route $route): void
37
    {
38 14
        $this->routes[$name] = $route;
39 14
    }
40
41
    // Countable
42 2
    public function count(): int
43
    {
44 2
        return count($this->routes);
45
    }
46
47
    // IteratorAggregate
48 10
    public function getIterator(): iterable
49
    {
50 10
        foreach ($this->routes as $name => $route) {
51 9
            yield $route;
52
        }
53 10
    }
54
}
55