Passed
Push — master ( 159f4a...679316 )
by
unknown
02:11
created

MultipleTrait::getMultiple()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Vectorface\Cache\Common;
4
5
/**
6
 * Wraps (get|set|delete) operations with their multiple counterparts
7
 *
8
 * This is useful when the underlying cache doesn't implement multi ops
9
 */
10
trait MultipleTrait
11
{
12
    abstract public function get($key, $default);
13
    abstract public function set($key, $value, $ttl = null);
14
    abstract public function delete($key);
15
    abstract protected function keys($keys);
16
    abstract protected function values($values);
17
18
    /**
19
     * @inheritDoc
20
     */
21 10
    public function getMultiple($keys, $default = null)
22
    {
23 10
        $values = [];
24 10
        foreach ($this->keys($keys) as $key) {
25 6
            $values[$key] = $this->get($key, $default);
26
        }
27
28 6
        return $values;
29
    }
30
31
    /**
32
     * @inheritDoc
33
     */
34 8
    public function setMultiple($values, $ttl = null)
35
    {
36 8
        $success = true;
37 8
        foreach ($this->values($values) as $key => $value) {
38 6
            $success = $this->set($key, $value, $ttl) && $success;
39
        }
40 6
        return $success;
41
    }
42
43
    /**
44
     * @inheritDoc
45
     */
46 5
    public function deleteMultiple($keys)
47
    {
48 5
        $success = true;
49 5
        foreach ($keys as $key) {
50 5
            $success = $this->delete($key) && $success;
51
        }
52 5
        return $success;
53
    }
54
}
55