Lines 91.22% 52 / 57
Methods 69.23% 9 / 13
Classes 0.00% 0 / 1
Name Lines Methods CRAP
 get 75.00% 6 / 8 0.00% 0 / 1 4.25
 set 100.00% 7 / 7 100.00% 1 / 1 3
 delete 100.00% 3 / 3 100.00% 1 / 1 1
 clear 100.00% 2 / 2 100.00% 1 / 1 1
 has 100.00% 8 / 8 100.00% 1 / 1 4
 [Lucent\Cache\Cache] setDefaultTtl 100.00% 1 / 1 100.00% 1 / 1 1
 [Lucent\Cache\Cache] getDefaultTtl 100.00% 1 / 1 100.00% 1 / 1 1
 [Lucent\Cache\Cache] resolveTtl 100.00% 1 / 1 100.00% 1 / 1 1
 [Lucent\Cache\Cache] validateKey 100.00% 2 / 2 100.00% 1 / 1 3
 [Lucent\Cache\Cache] expiryFromTtl 85.71% 6 / 7 0.00% 0 / 1 4.05
 [Lucent\Cache\Cache] getMultiple 100.00% 5 / 5 100.00% 1 / 1 2
 [Lucent\Cache\Cache] setMultiple 83.33% 5 / 6 0.00% 0 / 1 3.04
 [Lucent\Cache\Cache] deleteMultiple 83.33% 5 / 6 0.00% 0 / 1 3.04
17class ArrayDriver extends Cache
18{
19    /**
20     * Stored values keyed by cache key.
21     *
22     * @var array<string, array{expires: int|null, value: mixed}>
23     */
24    private array $store = [];
25
26    /**
27     * @inheritDoc
28     */
29    public function get(string $key, mixed $default = null): mixed
30    {
31        $this->validateKey($key);
32
33        if (!array_key_exists($key, $this->store)) {
34            return $default;
35        }
36
37        $entry = $this->store[$key];
38
39        if ($entry['expires'] !== null && $entry['expires'] <= time()) {
40            unset($this->store[$key]);
41            return $default;
42        }
43
44        return $entry['value'];
45    }
46
47    /**
48     * @inheritDoc
49     */
50    public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool
51    {
52        $this->validateKey($key);
53
54        $expires = $this->expiryFromTtl($this->resolveTtl($ttl));
55
56        if ($expires !== null && $expires <= time()) {
57            unset($this->store[$key]);
58            return true;
59        }
60
61        $this->store[$key] = ['expires' => $expires, 'value' => $value];
62
63        return true;
64    }
65
66    /**
67     * @inheritDoc
68     */
69    public function delete(string $key): bool
70    {
71        $this->validateKey($key);
72
73        unset($this->store[$key]);
74
75        return true;
76    }
77
78    /**
79     * @inheritDoc
80     */
81    public function clear(): bool
82    {
83        $this->store = [];
84
85        return true;
86    }
87
88    /**
89     * @inheritDoc
90     */
91    public function has(string $key): bool
92    {
93        $this->validateKey($key);
94
95        if (!array_key_exists($key, $this->store)) {
96            return false;
97        }
98
99        $entry = $this->store[$key];
100
101        if ($entry['expires'] !== null && $entry['expires'] <= time()) {
102            unset($this->store[$key]);
103            return false;
104        }
105
106        return true;
107    }
108}

Inherited from Lucent\Cache\Cache

45    public function setDefaultTtl(?int $ttl): void
46    {
47        $this->defaultTtl = $ttl;
48    }
55    public function getDefaultTtl(): ?int
56    {
57        return $this->defaultTtl;
58    }
69    protected function resolveTtl(null|int|DateInterval $ttl): null|int|DateInterval
70    {
71        return $ttl ?? $this->defaultTtl;
72    }
89    protected function validateKey(mixed $key): void
90    {
91        if (!is_string($key) || preg_match('/^[A-Za-z0-9_.]{1,512}$/', $key) !== 1) {
92            throw new InvalidArgumentException($key);
93        }
94    }
102    protected function expiryFromTtl(null|int|DateInterval $ttl): ?int
103    {
104        if ($ttl === null) {
105            return null;
106        }
107
108        if ($ttl instanceof DateInterval) {
109            $ttl = (new \DateTimeImmutable())->add($ttl)->getTimestamp() - time();
110        }
111
112        if ($ttl <= 0) {
113            // Already expired: return a timestamp in the past so the item is
114            // treated as stale and removed rather than stored.
115            return time() - 1;
116        }
117
118        return time() + $ttl;
119    }
124    public function getMultiple(iterable $keys, mixed $default = null): iterable
125    {
126        $result = [];
127
128        foreach ($keys as $key) {
129            $this->validateKey($key);
130            $result[$key] = $this->get($key, $default);
131        }
132
133        return $result;
134    }
139    public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool
140    {
141        $success = true;
142
143        foreach ($values as $key => $value) {
144            $this->validateKey($key);
145            if (!$this->set($key, $value, $ttl)) {
146                $success = false;
147            }
148        }
149
150        return $success;
151    }
156    public function deleteMultiple(iterable $keys): bool
157    {
158        $success = true;
159
160        foreach ($keys as $key) {
161            $this->validateKey($key);
162            if (!$this->delete($key)) {
163                $success = false;
164            }
165        }
166
167        return $success;
168    }