Tagger::put()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Cache\Tagger;
15
16
use JsonException;
17
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Valkyrja\Cache\Manager\Contract\CacheContract;
19
use Valkyrja\Cache\Tagger\Contract\TaggerContract;
20
use Valkyrja\Type\BuiltIn\Support\Arr;
21
22
class Tagger implements TaggerContract
23
{
24
    /**
25
     * The tags.
26
     *
27
     * @var string[]
28
     */
29
    protected array $tags;
30
31
    public function __construct(
32
        protected CacheContract $adapter,
33
        string ...$tags
34
    ) {
35
        $this->tags = $tags;
36
    }
37
38
    /**
39
     * @inheritDoc
40
     */
41
    #[Override]
42
    public static function make(CacheContract $store, string ...$tags): static
43
    {
44
        return new static($store, ...$tags);
45
    }
46
47
    /**
48
     * @inheritDoc
49
     *
50
     * @throws JsonException
51
     */
52
    #[Override]
53
    public function has(string $key): bool
54
    {
55
        foreach ($this->tags as $tag) {
56
            if (isset($this->getKeys($tag)[$key]) && $this->adapter->has($key)) {
57
                return true;
58
            }
59
        }
60
61
        return false;
62
    }
63
64
    /**
65
     * @inheritDoc
66
     *
67
     * @throws JsonException
68
     */
69
    #[Override]
70
    public function get(string $key): string|null
71
    {
72
        foreach ($this->tags as $tag) {
73
            if (isset($this->getKeys($tag)[$key])) {
74
                return $this->adapter->get($key);
75
            }
76
        }
77
78
        return null;
79
    }
80
81
    /**
82
     * @inheritDoc
83
     *
84
     * @throws JsonException
85
     */
86
    #[Override]
87
    public function many(string ...$keys): array
88
    {
89
        $items = [];
90
91
        foreach ($this->tags as $tag) {
92
            $cachedKeys = $this->getKeys($tag);
93
94
            foreach ($keys as $key) {
95
                if (isset($cachedKeys[$key])) {
96
                    $value = $this->adapter->get($key);
97
98
                    if ($value === null) {
99
                        continue;
100
                    }
101
102
                    $items[] = $value;
103
                }
104
            }
105
        }
106
107
        return $items;
108
    }
109
110
    /**
111
     * @inheritDoc
112
     *
113
     * @throws JsonException
114
     */
115
    #[Override]
116
    public function put(string $key, string $value, int $minutes): void
117
    {
118
        $this->tag($key);
119
120
        $this->adapter->put($key, $value, $minutes);
121
    }
122
123
    /**
124
     * @inheritDoc
125
     *
126
     * @throws JsonException
127
     */
128
    #[Override]
129
    public function putMany(array $values, int $minutes): void
130
    {
131
        foreach ($values as $key => $value) {
132
            $this->put($key, $value, $minutes);
133
        }
134
    }
135
136
    /**
137
     * @inheritDoc
138
     *
139
     * @throws JsonException
140
     */
141
    #[Override]
142
    public function increment(string $key, int $value = 1): int
143
    {
144
        $this->tag($key);
145
146
        return $this->adapter->increment($key, $value);
147
    }
148
149
    /**
150
     * @inheritDoc
151
     *
152
     * @throws JsonException
153
     */
154
    #[Override]
155
    public function decrement(string $key, int $value = 1): int
156
    {
157
        $this->tag($key);
158
159
        return $this->adapter->decrement($key, $value);
160
    }
161
162
    /**
163
     * @inheritDoc
164
     *
165
     * @throws JsonException
166
     */
167
    #[Override]
168
    public function forever(string $key, string $value): void
169
    {
170
        $this->tag($key);
171
172
        $this->adapter->forever($key, $value);
173
    }
174
175
    /**
176
     * @inheritDoc
177
     *
178
     * @throws JsonException
179
     */
180
    #[Override]
181
    public function forget(string $key): bool
182
    {
183
        $this->untag($key);
184
185
        return $this->adapter->forget($key);
186
    }
187
188
    /**
189
     * @inheritDoc
190
     *
191
     * @throws JsonException
192
     */
193
    #[Override]
194
    public function flush(): bool
195
    {
196
        foreach ($this->tags as $tag) {
197
            foreach ($this->getKeys($tag) as $key) {
198
                $this->adapter->forget($key);
199
            }
200
        }
201
202
        return true;
203
    }
204
205
    /**
206
     * @inheritDoc
207
     *
208
     * @throws JsonException
209
     */
210
    #[Override]
211
    public function tag(string $key): static
212
    {
213
        foreach ($this->tags as $tag) {
214
            $keys = $this->getKeys($tag);
215
216
            $keys[$key] = $key;
217
218
            $this->putKeys($tag, $keys);
219
        }
220
221
        return $this;
222
    }
223
224
    /**
225
     * @inheritDoc
226
     *
227
     * @throws JsonException
228
     */
229
    #[Override]
230
    public function untag(string $key): static
231
    {
232
        foreach ($this->tags as $tag) {
233
            $keys = $this->getKeys($tag);
234
235
            unset($keys[$key]);
236
237
            $this->putKeys($tag, $keys);
238
        }
239
240
        return $this;
241
    }
242
243
    /**
244
     * @inheritDoc
245
     *
246
     * @throws JsonException
247
     */
248
    #[Override]
249
    public function tagMany(string ...$keys): static
250
    {
251
        foreach ($keys as $key) {
252
            $this->tag($key);
253
        }
254
255
        return $this;
256
    }
257
258
    /**
259
     * @inheritDoc
260
     *
261
     * @throws JsonException
262
     */
263
    #[Override]
264
    public function untagMany(string ...$keys): static
265
    {
266
        foreach ($keys as $key) {
267
            $this->untag($key);
268
        }
269
270
        return $this;
271
    }
272
273
    /**
274
     * Get a tag.
275
     *
276
     * @throws JsonException
277
     *
278
     * @return string[]
279
     */
280
    protected function getKeys(string $tag): array
281
    {
282
        $keysFromCache = $this->adapter->get($tag);
283
284
        if ($keysFromCache !== null && $keysFromCache !== '') {
285
            /** @var string[] $keys */
286
            $keys = Arr::fromString($keysFromCache);
287
288
            return $keys;
289
        }
290
291
        return [];
292
    }
293
294
    /**
295
     * Put a tag.
296
     *
297
     * @param string[] $keys
298
     *
299
     * @throws JsonException
300
     */
301
    protected function putKeys(string $tag, array $keys): void
302
    {
303
        $this->adapter->forever($tag, Arr::toString($keys));
304
    }
305
}
306