Passed
Push — master ( 19278e...10e1cd )
by butschster
07:45
created

CacheRepository::delete()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
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 9
ccs 5
cts 5
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Cache;
6
7
use Psr\EventDispatcher\EventDispatcherInterface;
8
use Psr\SimpleCache\CacheInterface;
9
use Spiral\Cache\Event\CacheHit;
10
use Spiral\Cache\Event\CacheMissed;
11
use Spiral\Cache\Event\KeyDeleted;
12
use Spiral\Cache\Event\KeyWritten;
13
14
/**
15
 * @internal
16
 */
17
class CacheRepository implements CacheInterface
18
{
19 51
    public function __construct(
20
        protected CacheInterface $storage,
21
        protected ?EventDispatcherInterface $dispatcher = null,
22
        protected ?string $prefix = null
23
    ) {
24 51
    }
25
26 12
    public function get(string $key, mixed $default = null): mixed
27
    {
28 12
        $value = $this->storage->get($this->resolveKey($key));
29
30 12
        if ($value === null) {
31 10
            $this->dispatcher?->dispatch(new CacheMissed($this->resolveKey($key)));
32
33 10
            return $default;
34
        }
35
36 2
        $this->dispatcher?->dispatch(new CacheHit($this->resolveKey($key), $value));
37
38 2
        return $value;
39
    }
40
41 12
    public function set(string $key, mixed $value, \DateInterval|int|null $ttl = null): bool
42
    {
43 12
        $result = $this->storage->set($this->resolveKey($key), $value, $ttl);
44
45 12
        if ($result) {
46 12
            $this->dispatcher?->dispatch(new KeyWritten($this->resolveKey($key), $value));
47
        }
48
49 12
        return $result;
50
    }
51
52 10
    public function delete(string $key): bool
53
    {
54 10
        $result = $this->storage->delete($this->resolveKey($key));
55
56 10
        if ($result) {
57 10
            $this->dispatcher?->dispatch(new KeyDeleted($this->resolveKey($key)));
58
        }
59
60 10
        return $result;
61
    }
62
63
    public function clear(): bool
64
    {
65
        return $this->storage->clear();
66
    }
67
68 6
    public function getMultiple(iterable $keys, mixed $default = null): iterable
69
    {
70 6
        $result = [];
71
72 6
        foreach ($keys as $key) {
73 6
            $result[$key] = $this->get($key, $default);
74
        }
75
76 6
        return $result;
77
    }
78
79 6
    public function setMultiple(iterable $values, \DateInterval|int|null $ttl = null): bool
80
    {
81 6
        $state = null;
82
83 6
        foreach ($values as $key => $value) {
84 6
            $result = $this->set($key, $value, $ttl);
85 6
            $state = \is_null($state) ? $result : $result && $state;
86
        }
87
88 6
        return $state ?: false;
89
    }
90
91 5
    public function deleteMultiple(iterable $keys): bool
92
    {
93 5
        $state = null;
94 5
        foreach ($keys as $key) {
95 5
            $result = $this->delete($key);
96 5
            $state = \is_null($state) ? $result : $result && $state;
97
        }
98
99 5
        return $state ?: false;
100
    }
101
102 4
    public function has(string $key): bool
103
    {
104 4
        return $this->storage->has($this->resolveKey($key));
105
    }
106
107 15
    public function getStorage(): CacheInterface
108
    {
109 15
        return $this->storage;
110
    }
111
112 34
    private function resolveKey(string $key): string
113
    {
114 34
        if (!empty($this->prefix)) {
115 7
            return $this->prefix . $key;
116
        }
117
118 27
        return $key;
119
    }
120
}
121