RouteCollection::getRoutes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 *  This file is part of the Micro framework package.
7
 *
8
 *  (c) Stanislau Komar <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace Micro\Plugin\Http\Business\Route;
15
16
use Micro\Plugin\Http\Exception\RouteAlreadyDeclaredException;
17
use Micro\Plugin\Http\Exception\RouteNotFoundException;
18
19
/**
20
 * @author Stanislau Komar <[email protected]>
21
 */
22
class RouteCollection implements RouteCollectionInterface
23
{
24
    /**
25
     * @var array<string, RouteInterface>
26
     */
27
    private array $routes;
28
29
    /**
30
     * @var string[]
31
     */
32
    private array $routesNamesStatic;
33
34
    /**
35
     * @var string[]
36
     */
37
    private array $routesNamesDynamic;
38
39
    /**
40
     * @param RouteInterface[] $routes
41
     */
42
    public function __construct(array $routes = [])
43
    {
44 12
        $this->routes = [];
45 12
        $this->routesNamesDynamic = [];
46 12
        $this->routesNamesStatic = [];
47
48 12
        $this->setRoutes($routes);
49
    }
50
51
    /**
52
     * {@inheritDoc}
53
     */
54
    public function setRoutes(iterable $routes): self
55
    {
56 12
        $this->routes = [];
57 12
        $this->routesNamesDynamic = [];
58 12
        $this->routesNamesStatic = [];
59
60 12
        foreach ($routes as $route) {
61 6
            $this->addRoute($route);
62
        }
63
64 12
        return $this;
65
    }
66
67
    /**
68
     * {@inheritDoc}
69
     */
70
    public function addRoute(RouteInterface $route): self
71
    {
72 12
        $routeName = $route->getName();
73 12
        $routeUri = $route->getUri();
74 12
        $routeKeyInCollection = $routeName ?? $routeUri;
75
76 12
        if ($routeName && \array_key_exists($routeName, $this->routes)) {
77 1
            throw new RouteAlreadyDeclaredException($routeName);
78
        }
79
80 12
        $this->routes[$routeKeyInCollection] = $route;
81
82 12
        if ($route->getPattern()) {
83 5
            $this->routesNamesDynamic[] = $routeKeyInCollection;
84
85 5
            return $this;
86
        }
87
88 7
        $this->routesNamesStatic[] = $routeKeyInCollection;
89
90 7
        return $this;
91
    }
92
93
    /**
94
     * {@inheritDoc}
95
     */
96
    public function getRouteByName(string $name): RouteInterface
97
    {
98 7
        if (!\array_key_exists($name, $this->routes)) {
99 1
            RouteNotFoundException::throwsRouteNotFoundByName($name);
100
        }
101
102 7
        return $this->routes[$name];
103
    }
104
105
    /**
106
     * {@inheritDoc}
107
     */
108
    public function getRoutes(): iterable
109
    {
110 2
        return array_values($this->routes);
111
    }
112
113
    /**
114
     * {@inheritDoc}
115
     */
116
    public function getRoutesNames(): array
117
    {
118 1
        return array_keys($this->routes);
119
    }
120
121
    /**
122
     * {@inheritDoc}
123
     */
124
    public function iterateRoutes(): iterable
125
    {
126 6
        foreach ($this->routesNamesStatic as $routeName) {
127 1
            yield $this->getRouteByName($routeName);
128
        }
129
130 6
        foreach ($this->routesNamesDynamic as $routeName) {
131 5
            yield $this->getRouteByName($routeName);
132
        }
133
    }
134
}
135