ObjectCacheGroup::deleteMultiple()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Leonidas\Library\Core\Cache;
4
5
use Psr\SimpleCache\CacheInterface;
6
7
class ObjectCacheGroup implements CacheInterface
8
{
9
    protected string $group;
10
11
    protected array $keys;
12
13
    public function __construct(string $group = 'default')
14
    {
15
        $this->group = $group;
16
    }
17
18
    public function get($key, $default = null)
19
    {
20
        return wp_cache_get($key, $this->group) ?? $default;
0 ignored issues
show
Bug introduced by
The function wp_cache_get was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
        return /** @scrutinizer ignore-call */ wp_cache_get($key, $this->group) ?? $default;
Loading history...
21
    }
22
23
    public function set($key, $value, $ttl = null): bool
24
    {
25
        $this->addKey($key);
26
27
        return wp_cache_set($key, $value, $this->group, $ttl);
0 ignored issues
show
Bug introduced by
The function wp_cache_set was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

27
        return /** @scrutinizer ignore-call */ wp_cache_set($key, $value, $this->group, $ttl);
Loading history...
28
    }
29
30
    public function delete($key): bool
31
    {
32
        $this->removeKey($key);
33
34
        return wp_cache_delete($key, $this->group);
0 ignored issues
show
Bug introduced by
The function wp_cache_delete was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

34
        return /** @scrutinizer ignore-call */ wp_cache_delete($key, $this->group);
Loading history...
35
    }
36
37
    public function clear(): bool
38
    {
39
        foreach (array_unique($this->keys) as $key) {
40
            $this->delete($key);
41
        }
42
43
        return true;
44
    }
45
46
    public function getMultiple($keys, $default = null): iterable
47
    {
48
        $values = [];
49
50
        foreach ($keys as $key) {
51
            $values[$key] = $this->get($key, $default);
52
        }
53
54
        return $values;
55
    }
56
57
    public function setMultiple($values, $ttl = null): bool
58
    {
59
        foreach ($values as $key => $value) {
60
            $this->set($key, $value, $ttl);
61
        }
62
63
        return true;
64
    }
65
66
    public function deleteMultiple($keys): bool
67
    {
68
        foreach ($keys as $key) {
69
            $this->delete($key);
70
        }
71
72
        return true;
73
    }
74
75
    public function has($key): bool
76
    {
77
        wp_cache_get($key, $this->group, false, $found);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $found seems to be never defined.
Loading history...
Bug introduced by
The function wp_cache_get was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
        /** @scrutinizer ignore-call */ 
78
        wp_cache_get($key, $this->group, false, $found);
Loading history...
78
79
        return $found;
80
    }
81
82
    protected function addKey(string $key)
83
    {
84
        $this->keys[] = $key;
85
    }
86
87
    protected function removeKey(string $key)
88
    {
89
        $this->keys = array_diff($this->keys, [$key]);
90
    }
91
}
92