SerializedRouteCollection   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 2
dl 0
loc 109
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

8 Methods

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

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
122
        }
123 4
    }
124
}
125