MultipleTrait::setMultiple()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
cc 3
nc 3
nop 2
crap 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