MultipleTrait   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
eloc 13
c 3
b 0
f 0
dl 0
loc 43
ccs 15
cts 15
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setMultiple() 0 7 3
A getMultiple() 0 8 2
A deleteMultiple() 0 7 3
1
<?php
2
3
namespace Vectorface\Cache\Common;
4
5
use DateInterval;
6
7
/**
8
 * Wraps (get|set|delete) operations with their multiple counterparts
9
 *
10
 * This is useful when the underlying cache doesn't implement multi ops
11
 */
12
trait MultipleTrait
13
{
14
    abstract public function get(string $key, mixed $default);
15
    abstract public function set(string $key, mixed $value, DateInterval|int|null $ttl = null);
16
    abstract public function delete(string $key);
17
    abstract protected function keys(iterable $keys);
18
    abstract protected function values(iterable $values);
19
20
    /**
21 10
     * @inheritDoc
22
     */
23 10
    public function getMultiple(iterable $keys, mixed $default = null) : iterable
24 10
    {
25 6
        $values = [];
26
        foreach ($this->keys($keys) as $key) {
27
            $values[$key] = $this->get($key, $default);
28 6
        }
29
30
        return $values;
31
    }
32
33
    /**
34 8
     * @inheritDoc
35
     */
36 8
    public function setMultiple(iterable $values, DateInterval|int|null $ttl = null) : bool
37 8
    {
38 6
        $success = true;
39
        foreach ($this->values($values) as $key => $value) {
40 6
            $success = $this->set($key, $value, $ttl) && $success;
41
        }
42
        return $success;
43
    }
44
45
    /**
46 5
     * @inheritDoc
47
     */
48 5
    public function deleteMultiple($keys) : bool
49 5
    {
50 5
        $success = true;
51
        foreach ($keys as $key) {
52 5
            $success = $this->delete($key) && $success;
53
        }
54
        return $success;
55
    }
56
}
57