Passed
Push — master ( c2b4c4...fd18cf )
by Alexander
03:23 queued 01:03
created

DependencyAwareCache::setMultiple()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Cache;
6
7
use Psr\SimpleCache\CacheInterface as PsrSimpleCacheInterface;
8
use Yiisoft\Cache\Metadata\CacheItem;
9
10
use function is_array;
11
12
/**
13
 * Cache provides support for the data caching, including dependencies.
14
 * The actual data caching is performed via {@see DependencyAwareCache::handler()}.
15
 *
16
 * @internal
17
 */
18
final class DependencyAwareCache implements PsrSimpleCacheInterface
19
{
20
    /**
21
     * @var CacheInterface The actual cache.
22
     */
23
    private CacheInterface $cache;
24
25
    /**
26
     * @var PsrSimpleCacheInterface The actual cache handler.
27
     */
28
    private PsrSimpleCacheInterface $handler;
29
30
    /**
31
     * @param CacheInterface $cache The actual cache handler.
32
     * @param PsrSimpleCacheInterface $handler The actual cache handler.
33
     */
34 82
    public function __construct(CacheInterface $cache, PsrSimpleCacheInterface $handler)
35
    {
36 82
        $this->cache = $cache;
37 82
        $this->handler = $handler;
38 82
    }
39
40 48
    public function get($key, $default = null)
41
    {
42 48
        $value = $this->handler->get($key, $default);
43 48
        return $this->checkAndGetValue($key, $value, $default);
44
    }
45
46 42
    public function set($key, $value, $ttl = null): bool
47
    {
48 42
        return $this->handler->set($key, $value, $ttl);
49
    }
50
51 15
    public function delete($key): bool
52
    {
53 15
        return $this->handler->delete($key);
54
    }
55
56 1
    public function clear(): bool
57
    {
58 1
        return $this->handler->clear();
59
    }
60
61 16
    public function getMultiple($keys, $default = null): iterable
62
    {
63 16
        $values = [];
64
65 16
        foreach ($this->handler->getMultiple($keys, $default) as $key => $value) {
66 16
            $values[$key] = $this->checkAndGetValue($key, $value, $default);
67
        }
68
69 16
        return $values;
70
    }
71
72 22
    public function setMultiple($values, $ttl = null): bool
73
    {
74 22
        return $this->handler->setMultiple($values, $ttl);
75
    }
76
77 9
    public function deleteMultiple($keys): bool
78
    {
79 9
        return $this->handler->deleteMultiple($keys);
80
    }
81
82 7
    public function has($key): bool
83
    {
84 7
        return $this->get($key) !== null;
85
    }
86
87
    /**
88
     * Checks if the cache dependency has expired and returns a value
89
     *
90
     * @param string $key The unique key of this item in the cache.
91
     * @param mixed $value The value of this item in the cache.
92
     * @param mixed $default Default value to return if the key does not exist.
93
     *
94
     * @return mixed|null The cache value or `null` if the cache is outdated or a dependency has been changed.
95
     */
96 58
    private function checkAndGetValue(string $key, $value, $default = null)
97
    {
98 58
        if (is_array($value) && isset($value[1]) && $value[1] instanceof CacheItem) {
99 14
            [$value, $item] = $value;
100 14
            $dependency = $item->dependency();
101
102 14
            if ($item->key() !== $key || ($dependency !== null && $dependency->isChanged($this->cache))) {
103 2
                return $default;
104
            }
105
        }
106
107 58
        return $value;
108
    }
109
}
110