Completed
Push — master ( fa7c53...535778 )
by Matze
04:38
created

SerializedRouteCollection::initAll()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 2
1
<?php
2
3
namespace BrainExe\Core\Application;
4
5
use ArrayIterator;
6
use BrainExe\Annotations\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("Core.RouteCollection", public=false, 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 = null;
26
27
    /**
28
     * @var Route[]
29
     */
30 6
    private $cache = [];
31
32 6
    /**
33 6
     * @param string $name
34
     * @return Route
35
     */
36
    public function get($name)
37
    {
38
        $this->loadFromCache();
39 3
40
        if (isset($this->cache[$name])) {
41 3
            return $this->cache[$name];
42 2
        }
43
44
        if (!isset($this->serializedRoutes[$name])) {
45 3
            throw new InvalidArgumentException(sprintf('invalid route: %s', $name));
46 1
        }
47
48
        return $this->cache[$name] = unserialize($this->serializedRoutes[$name]);
49 2
    }
50
51
    public function all()
52 2
    {
53
        $this->initAll();
54 2
55
        return $this->cache;
56
    }
57 2
58
    /**
59 2
     * Gets the current RouteCollection as an Iterator that includes all routes.
60
     *
61 2
     * It implements \IteratorAggregate.
62
     *
63
     * @see all()
64
     *
65
     * @return ArrayIterator An \ArrayIterator object for iterating over routes
66
     */
67
    public function getIterator()
68
    {
69
        $this->all();
70
71
        return new ArrayIterator($this->cache);
72
    }
73 2
74
    /**
75 2
     * Gets the number of Routes in this collection.
76
     *
77 2
     * @return int The number of routes
78
     */
79
    public function count()
80
    {
81
        $this->loadFromCache();
82
83
        return count($this->serializedRoutes);
84
    }
85 1
86
    /**
87 1
     * @param string $name
88
     * @param Route $route
89
     */
90 1
    public function add($name, Route $route)
91
    {
92 1
        $this->cache[$name] = $route;
93 1
    }
94
95
    /**
96 1
     * @param array|string $name
97
     */
98 1
    public function remove($name)
99 1
    {
100
        unset($name);
101
        throw new RuntimeException('RoutCollection::remove is not implemented');
102
    }
103
104
    private function initAll()
105
    {
106
        $this->loadFromCache();
107
108
        return array_map([$this, 'get'], array_keys($this->serializedRoutes));
109
    }
110
111
    private function loadFromCache()
112
    {
113
        if ($this->serializedRoutes == null) {
114
            $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...
115
        }
116
    }
117
}
118