Completed
Push — master ( fe63b4...6cf296 )
by Tomasz
02:42
created

CachedRouteDetector   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 95%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 55
ccs 19
cts 20
cp 0.95
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A detect() 0 9 2
A addRoute() 0 9 2
A setDefault() 0 8 2
A clearCache() 0 4 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 array
16
     */
17
    private $cachedRoutes = array();
18
19
    /**
20
     * {@inheritdoc}
21
     */
22 12
    public function detect($className)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
23
    {
24 12
        if (!empty($this->cachedRoutes[$className])) {
25
            return $this->cachedRoutes[$className];
26
        }
27 12
        $this->cachedRoutes[$className] = parent::detect($className);
28
29 12
        return $this->cachedRoutes[$className];
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 8
    public function addRoute($expression, $poolName)
36
    {
37 8
        $return = parent::addRoute($expression, $poolName);
38 8
        if ($return) {
39 8
            $this->clearCache();
40 8
        }
41
42 8
        return $return;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 12
    public function setDefault($route)
49
    {
50 12
        if ($this->getDefault() == $route) {
51 12
            return;
52
        }
53 9
        parent::setDefault($route);
54 9
        $this->clearCache();
55 9
    }
56
57
    /**
58
     * Clear route cache.
59
     */
60 11
    private function clearCache()
61
    {
62 11
        $this->cachedRoutes = array();
63 11
    }
64
}
65