CachedRouteDetector::clearCache()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Gendoria\CommandQueue\RouteDetection\Detector;
4
5
/**
6
 * Cached route detector caches detection result to speed up further searches for given class.
7
 *
8
 * @author Tomasz Struczyński <[email protected]>
9
 */
10
class CachedRouteDetector extends RouteDetector
11
{
12
    /**
13
     * Array of cached routes.
14
     *
15
     * @var string[]
16
     */
17
    private $cachedRoutes = array();
18
19
    /**
20
     * {@inheritdoc}
21
     * @return string
22
     */
23 12
    public function detect($className)
24
    {
25 12
        if (!empty($this->cachedRoutes[$className])) {
26 1
            return $this->cachedRoutes[$className];
27
        }
28 12
        $this->cachedRoutes[$className] = parent::detect($className);
29
30 12
        return $this->cachedRoutes[$className];
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 8
    public function addRoute($expression, $poolName)
37
    {
38 8
        $return = parent::addRoute($expression, $poolName);
39 8
        if ($return) {
40 8
            $this->clearCache();
41 8
        }
42
43 8
        return $return;
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 13
    public function setDefault($route)
50
    {
51 13
        if ($this->getDefault() == $route) {
52 13
            return;
53
        }
54 9
        parent::setDefault($route);
55 9
        $this->clearCache();
56 9
    }
57
58
    /**
59
     * Clear route cache.
60
     */
61 11
    private function clearCache()
62
    {
63 11
        $this->cachedRoutes = array();
64 11
    }
65
}
66