TestResultCache   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 10
eloc 21
c 3
b 1
f 0
dl 0
loc 66
ccs 22
cts 24
cp 0.9167
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCached() 0 7 2
A isCached() 0 13 4
A clear() 0 3 1
A cache() 0 10 3
1
<?php
2
3
namespace HtaccessCapabilityTester;
4
5
use \HtaccessCapabilityTester\Testers\AbstractTester;
6
7
/**
8
 * Class caching test results
9
 *
10
 * @package    HtaccessCapabilityTester
11
 * @author     Bjørn Rosell <[email protected]>
12
 * @since      Class available since the beginning
13
 */
14
class TestResultCache
15
{
16
17
    /* @var array   Array for caching */
18
    protected static $cache;
19
20
    /**
21
     *
22
     * @param  array       $cacheKeys   Two keys for caching (usually: basedir and the getCacheKey() for the Tester)
23
     * @param  TestResult  $testResult  The test result to cache
24
     *
25
     * @return void
26
     */
27 35
    public static function cache($cacheKeys, $testResult)
28
    {
29 35
        if (!isset(self::$cache)) {
30 35
            self::$cache = [];
31
        }
32 35
        list($key1, $key2) = $cacheKeys;
33 35
        if (!isset(self::$cache[$key1])) {
34 35
            self::$cache[$key1] = [];
35
        }
36 35
        self::$cache[$key1][$key2] = $testResult;
37 35
    }
38
39
    /**
40
     * Check if in cache.
41
     *
42
     * @param  array       $cacheKeys   Keys for caching (usually: basedir and the getCacheKey() for the Tester)
43
     *
44
     * @return bool
45
     */
46 35
    public static function isCached($cacheKeys)
47
    {
48 35
        if (!isset(self::$cache)) {
49 35
            return false;
50
        }
51 21
        list($key1, $key2) = $cacheKeys;
52 21
        if (!isset(self::$cache[$key1])) {
53
            return false;
54
        }
55 21
        if (!isset(self::$cache[$key1][$key2])) {
56 21
            return false;
57
        }
58 18
        return true;
59
    }
60
61
    /**
62
     * Get from cache.
63
     *
64
     * @param  array       $cacheKeys   Keys for caching (usually: basedir and the getCacheKey() for the Tester)
65
     *
66
     * @return TestResult   The test result
67
     */
68 18
    public static function getCached($cacheKeys)
69
    {
70 18
        if (!self::isCached($cacheKeys)) {
71
            throw new \Exception('Not in cache');
72
        }
73 18
        list($key1, $key2) = $cacheKeys;
74 18
        return self::$cache[$key1][$key2];
75
    }
76
77 80
    public static function clear()
78
    {
79 80
        self::$cache = null;
80 80
    }
81
}
82