Completed
Push — master ( 2e23cf...730f80 )
by Lars
03:00
created

AdapterArray   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 91.89%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 0
dl 0
loc 121
ccs 34
cts 37
cp 0.9189
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A exists() 0 6 1
A get() 0 4 2
A installed() 0 4 1
A set() 0 6 1
A setExpired() 0 10 2
A remove() 0 12 2
A removeAll() 0 7 1
A removeExpired() 0 24 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace voku\cache;
6
7
/**
8
 * AdapterArray: simple array-cache adapter
9
 */
10
class AdapterArray implements iAdapter
11
{
12
    /**
13
     * @var array
14
     */
15
    private static $values = [];
16
17
    /**
18
     * @var array<string, array<int>>
19
     */
20
    private static $expired = [];
21
22
    /**
23
     * {@inheritdoc}
24
     */
25 13
    public function exists(string $key): bool
26
    {
27 13
        $this->removeExpired($key);
28
29 13
        return \array_key_exists($key, self::$values);
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 11
    public function get(string $key)
36
    {
37 11
        return $this->exists($key) ? self::$values[$key] : null;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function installed(): bool
44
    {
45
        return true;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 1
    public function remove(string $key): bool
52
    {
53 1
        $this->removeExpired($key);
54
55 1
        if (\array_key_exists($key, self::$values) === true) {
56 1
            unset(self::$values[$key]);
57
58 1
            return true;
59
        }
60
61
        return false;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 1
    public function removeAll(): bool
68
    {
69 1
        self::$values = [];
70 1
        self::$expired = [];
71
72 1
        return true;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 6
    public function set(string $key, $value): bool
79
    {
80 6
        self::$values[$key] = $value;
81
82 6
        return true;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 4
    public function setExpired(string $key, $value, int $ttl = 0): bool
89
    {
90 4
        self::$values[$key] = $value;
91
92 4
        if ($ttl !== 0) {
93 4
            self::$expired[$key] = [\time(), $ttl];
94
        }
95
96 4
        return true;
97
    }
98
99
    /**
100
     * Remove expired cache.
101
     *
102
     * @param string $key
103
     *
104
     * @return bool
105
     */
106 13
    private function removeExpired($key): bool
107
    {
108
        if (
109 13
            \array_key_exists($key, self::$expired) === false
110
            ||
111 13
            \array_key_exists($key, self::$values) === false
112
        ) {
113 9
            return false;
114
        }
115
116 4
        list($time, $ttl) = self::$expired[$key];
117 4
        \assert(\is_int($time));
118 4
        \assert(\is_int($ttl));
119
120 4
        if (\time() > ($time + $ttl)) {
121 3
            unset(self::$values[$key]);
122
        }
123
124 4
        if (!isset(self::$values[$key])) {
125 3
            unset(self::$expired[$key]);
126
        }
127
128 4
        return true;
129
    }
130
}
131