Passed
Push — master ( 8162ad...066103 )
by Evgeniy
02:47
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 94
    public function __construct(CacheInterface $cache, PsrSimpleCacheInterface $handler)
35
    {
36 94
        $this->cache = $cache;
37 94
        $this->handler = $handler;
38 94
    }
39
40 9
    public function get($key, $default = null)
41
    {
42 9
        $value = $this->handler->get($key, $default);
43 7
        return $this->checkAndGetValue($key, $value, $default);
44
    }
45
46 45
    public function set($key, $value, $ttl = null): bool
47
    {
48 45
        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 23
    public function getMultiple($keys, $default = null): iterable
62
    {
63 23
        $values = [];
64
65 23
        foreach ($this->handler->getMultiple($keys, $default) as $key => $value) {
66 23
            $values[$key] = $this->checkAndGetValue($key, $value, $default);
67
        }
68
69 23
        return $values;
70
    }
71
72 23
    public function setMultiple($values, $ttl = null): bool
73
    {
74 23
        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
     * Gets the raw cache value.
89
     *
90
     * @param string $key The unique key of this item in the cache.
91
     *
92
     * @return mixed The raw cache value or `null if the cache is outdated.
93
     */
94 49
    public function getRaw(string $key)
95
    {
96 49
        return $this->handler->get($key);
97
    }
98
99
    /**
100
     * Checks if the cache dependency has expired and returns a value.
101
     *
102
     * @param string $key The unique key of this item in the cache.
103
     * @param mixed $value The value of this item in the cache.
104
     * @param mixed $default Default value to return if the dependency has been changed.
105
     *
106
     * @return mixed The cache value or `$default` if the dependency has been changed.
107
     */
108 29
    private function checkAndGetValue(string $key, $value, $default = null)
109
    {
110 29
        if (is_array($value) && isset($value[1]) && $value[1] instanceof CacheItem) {
111 5
            [$value, $item] = $value;
112 5
            $dependency = $item->dependency();
113
114 5
            if ($item->key() !== $key || ($dependency !== null && $dependency->isChanged($this->cache))) {
115 2
                return $default;
116
            }
117
        }
118
119 29
        return $value;
120
    }
121
}
122