Completed
Push — master ( ba9072...49bd4f )
by Matze
06:56
created

SerializedRouteCollection   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 10
c 5
b 0
f 1
lcom 1
cbo 2
dl 0
loc 93
ccs 25
cts 25
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A get() 0 12 3
A all() 0 6 1
A getIterator() 0 6 1
A count() 0 4 1
A add() 0 4 1
A remove() 0 5 1
A initAll() 0 4 1
1
<?php
2
3
namespace BrainExe\Core\Application;
4
5
use ArrayIterator;
6
use BrainExe\Annotations\Annotations\Service;
7
use Psr\Log\InvalidArgumentException;
8
use RuntimeException;
9
use Symfony\Component\Routing\Route;
10
use Symfony\Component\Routing\RouteCollection;
11
12
/**
13
 * @Service("Core.RouteCollection", public=false)
14
 */
15
class SerializedRouteCollection extends RouteCollection
16
{
17
    /**
18
     * @var string[]
19
     */
20
    private $serializedRoutes;
21
22
    /**
23
     * @var Route[]
24
     */
25
    private $cache = [];
26
27
    /**
28
     * @param string[] $routes
29
     */
30 6
    public function __construct(array $routes = [])
31
    {
32 6
        $this->serializedRoutes = $routes;
33 6
    }
34
35
    /**
36
     * @param string $name
37
     * @return Route
38
     */
39 3
    public function get($name)
40
    {
41 3
        if (isset($this->cache[$name])) {
42 2
            return $this->cache[$name];
43
        }
44
45 3
        if (!isset($this->serializedRoutes[$name])) {
46 1
            throw new InvalidArgumentException(sprintf('invalid route: %s', $name));
47
        }
48
49 2
        return $this->cache[$name] = unserialize($this->serializedRoutes[$name]);
50
    }
51
52 2
    public function all()
53
    {
54 2
        $this->initAll();
55
56
        return $this->cache;
57 2
    }
58
59 2
    /**
60
     * Gets the current RouteCollection as an Iterator that includes all routes.
61 2
     *
62
     * It implements \IteratorAggregate.
63
     *
64
     * @see all()
65
     *
66
     * @return ArrayIterator An \ArrayIterator object for iterating over routes
67
     */
68
    public function getIterator()
69
    {
70
        $this->all();
71
72
        return new ArrayIterator($this->cache);
73 2
    }
74
75 2
    /**
76
     * Gets the number of Routes in this collection.
77 2
     *
78
     * @return int The number of routes
79
     */
80
    public function count()
81
    {
82
        return count($this->serializedRoutes);
83
    }
84
85 1
    /**
86
     * @param string $name
87 1
     * @param Route $route
88
     */
89
    public function add($name, Route $route)
90 1
    {
91
        $this->cache[$name] = $route;
92 1
    }
93 1
94
    /**
95
     * @param array|string $name
96 1
     */
97
    public function remove($name)
98 1
    {
99 1
        unset($name);
100
        throw new RuntimeException('RoutCollection::remove is not implemented');
101
    }
102
103
    private function initAll()
104
    {
105
        return array_map([$this, 'get'], array_keys($this->serializedRoutes));
106
    }
107
}
108