Completed
Push — v5.1 ( bb10e4...1e2251 )
by Georges
03:39
created

ExtendedCacheItemPoolTrait::detachItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 6
rs 9.4285
1
<?php
2
/**
3
 *
4
 * This file is part of phpFastCache.
5
 *
6
 * @license MIT License (MIT)
7
 *
8
 * For full copyright and license information, please see the docs/CREDITS.txt file.
9
 *
10
 * @author Khoa Bui (khoaofgod)  <[email protected]> http://www.phpfastcache.com
11
 * @author Georges.L (Geolim4)  <[email protected]>
12
 *
13
 */
14
15
namespace phpFastCache\Core\Pool;
16
17
use InvalidArgumentException;
18
use phpFastCache\Core\Item\ExtendedCacheItemInterface;
19
use Psr\Cache\CacheItemInterface;
20
21
22
trait ExtendedCacheItemPoolTrait
23
{
24
    use CacheItemPoolTrait;
25
26
    /**
27
     * Deletes all items in the pool.
28
     * @deprecated Use clear() instead
29
     * Will be removed in 5.1
30
     *
31
     * @return bool
32
     *   True if the pool was successfully cleared. False if there was an error.
33
     */
34
    public function clean()
35
    {
36
        trigger_error('Cache clean() method is deprecated, use clear() method instead', E_USER_DEPRECATED);
37
        return $this->clear();
38
    }
39
40
    /**
41
     * @param array $keys
42
     * An indexed array of keys of items to retrieve.
43
     * @param int $option json_encode() options
44
     * @param int $depth json_encode() depth
45
     * @return string
46
     * @throws \InvalidArgumentException
47
     */
48 View Code Duplication
    public function getItemsAsJsonString(array $keys = [], $option = 0, $depth = 512)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
    {
50
        $callback = function(CacheItemInterface $item){
51
            return $item->get();
52
        };
53
        return json_encode(array_map($callback, array_values($this->getItems($keys))), $option, $depth);
54
    }
55
56
    /**
57
     * @param string $tagName
58
     * @return \phpFastCache\Core\Item\ExtendedCacheItemInterface[]
59
     * @throws InvalidArgumentException
60
     */
61
    public function getItemsByTag($tagName)
62
    {
63
        if (is_string($tagName)) {
64
            $driverResponse = $this->getItem($this->getTagKey($tagName));
65
            if ($driverResponse->isHit()) {
66
                $items = (array) $driverResponse->get();
67
68
                /**
69
                 * getItems() may provides expired item(s)
70
                 * themselves provided by a cache of item
71
                 * keys based stored the tag item.
72
                 * Therefore we pass a filter callback
73
                 * to remove the expired Item(s) provided by
74
                 * the item keys passed through getItems()
75
                 *
76
                 * #headache
77
                 */
78
                return array_filter($this->getItems(array_unique(array_keys($items))), function(ExtendedCacheItemInterface $item){
79
                    return $item->isHit();
80
                });
81
            } else {
82
                return [];
83
            }
84
        } else {
85
            throw new InvalidArgumentException('$tagName must be a string');
86
        }
87
    }
88
89
    /**
90
     * @param array $tagNames
91
     * @return \phpFastCache\Core\Item\ExtendedCacheItemInterface[]
92
     * @throws InvalidArgumentException
93
     */
94
    public function getItemsByTags(array $tagNames)
95
    {
96
        $items = [];
97
        foreach (array_unique($tagNames) as $tagName) {
98
            $items = array_merge($items, $this->getItemsByTag($tagName));
99
        }
100
101
        return $items;
102
    }
103
104
    /**
105
     * Returns A json string that represents an array of items by tags-based.
106
     *
107
     * @param array $tagNames
108
     * An indexed array of keys of items to retrieve.
109
     * @param int $option json_encode() options
110
     * @param int $depth json_encode() depth
111
     *
112
     * @throws InvalidArgumentException
113
     *   If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException
114
     *   MUST be thrown.
115
     *
116
     * @return string
117
     */
118 View Code Duplication
    public function getItemsByTagsAsJsonString(array $tagNames, $option = 0, $depth = 512)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
    {
120
        $callback = function(CacheItemInterface $item){
121
            return $item->get();
122
        };
123
124
        return json_encode(array_map($callback, array_values($this->getItemsByTags($tagNames))), $option, $depth);
125
    }
126
127
    /**
128
     * @param string $tagName
129
     * @return bool|null
130
     * @throws InvalidArgumentException
131
     */
132
    public function deleteItemsByTag($tagName)
133
    {
134
        if (is_string($tagName)) {
135
            $return = null;
136
            foreach ($this->getItemsByTag($tagName) as $item) {
137
                $result = $this->deleteItem($item->getKey());
138
                if ($return !== false) {
139
                    $return = $result;
140
                }
141
            }
142
143
            return $return;
144
        } else {
145
            throw new InvalidArgumentException('$tagName must be a string');
146
        }
147
    }
148
149
    /**
150
     * @param array $tagNames
151
     * @return bool|null
152
     * @throws InvalidArgumentException
153
     */
154 View Code Duplication
    public function deleteItemsByTags(array $tagNames)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
155
    {
156
        $return = null;
157
        foreach ($tagNames as $tagName) {
158
            $result = $this->deleteItemsByTag($tagName);
159
            if ($return !== false) {
160
                $return = $result;
161
            }
162
        }
163
164
        return $return;
165
    }
166
167
    /**
168
     * @inheritdoc
169
     */
170 View Code Duplication
    public function incrementItemsByTag($tagName, $step = 1)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
171
    {
172
        if (is_string($tagName) && is_int($step)) {
173
            foreach ($this->getItemsByTag($tagName) as $item) {
174
                $item->increment($step);
175
                $this->saveDeferred($item);
176
            }
177
178
            return $this->commit();
179
        } else {
180
            throw new InvalidArgumentException('$tagName must be a string and $step an integer');
181
        }
182
    }
183
184
    /**
185
     * @inheritdoc
186
     */
187 View Code Duplication
    public function incrementItemsByTags(array $tagNames, $step = 1)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
188
    {
189
        $return = null;
190
        foreach ($tagNames as $tagName) {
191
            $result = $this->incrementItemsByTag($tagName, $step);
192
            if ($return !== false) {
193
                $return = $result;
194
            }
195
        }
196
197
        return $return;
198
    }
199
200
    /**
201
     * @inheritdoc
202
     */
203 View Code Duplication
    public function decrementItemsByTag($tagName, $step = 1)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
204
    {
205
        if (is_string($tagName) && is_int($step)) {
206
            foreach ($this->getItemsByTag($tagName) as $item) {
207
                $item->decrement($step);
208
                $this->saveDeferred($item);
209
            }
210
211
            return $this->commit();
212
        } else {
213
            throw new InvalidArgumentException('$tagName must be a string and $step an integer');
214
        }
215
    }
216
217
    /**
218
     * @inheritdoc
219
     */
220 View Code Duplication
    public function decrementItemsByTags(array $tagNames, $step = 1)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
221
    {
222
        $return = null;
223
        foreach ($tagNames as $tagName) {
224
            $result = $this->decrementItemsByTag($tagName, $step);
225
            if ($return !== false) {
226
                $return = $result;
227
            }
228
        }
229
230
        return $return;
231
    }
232
233
    /**
234
     * @inheritdoc
235
     */
236 View Code Duplication
    public function appendItemsByTag($tagName, $data)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
237
    {
238
        if (is_string($tagName)) {
239
            foreach ($this->getItemsByTag($tagName) as $item) {
240
                $item->append($data);
241
                $this->saveDeferred($item);
242
            }
243
244
            return $this->commit();
245
        } else {
246
            throw new InvalidArgumentException('$tagName must be a string');
247
        }
248
    }
249
250
    /**
251
     * @inheritdoc
252
     */
253 View Code Duplication
    public function appendItemsByTags(array $tagNames, $data)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
254
    {
255
        $return = null;
256
        foreach ($tagNames as $tagName) {
257
            $result = $this->decrementItemsByTag($tagName, $data);
258
            if ($return !== false) {
259
                $return = $result;
260
            }
261
        }
262
263
        return $return;
264
    }
265
266
    /**
267
     * @inheritdoc
268
     */
269 View Code Duplication
    public function prependItemsByTag($tagName, $data)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
270
    {
271
        if (is_string($tagName)) {
272
            foreach ($this->getItemsByTag($tagName) as $item) {
273
                $item->prepend($data);
274
                $this->saveDeferred($item);
275
            }
276
277
            return $this->commit();
278
        } else {
279
            throw new InvalidArgumentException('$tagName must be a string');
280
        }
281
    }
282
283
    /**
284
     * @inheritdoc
285
     */
286 View Code Duplication
    public function prependItemsByTags(array $tagNames, $data)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
287
    {
288
        $return = null;
289
        foreach ($tagNames as $tagName) {
290
            $result = $this->decrementItemsByTag($tagName, $data);
291
            if ($return !== false) {
292
                $return = $result;
293
            }
294
        }
295
296
        return $return;
297
    }
298
299
    /**
300
     * @param \Psr\Cache\CacheItemInterface $item
301
     * @return void
302
     */
303
    public function detachItem(CacheItemInterface $item)
304
    {
305
        if(isset($this->itemInstances[$item->getKey()])){
306
            $this->deregisterItem($item);
307
        }
308
    }
309
310
    /**
311
     * @return void
312
     */
313
    public function detachAllItems()
314
    {
315
        foreach ($this->itemInstances as $item) {
316
            $this->detachItem($item);
317
        }
318
    }
319
320
    /**
321
     * @param \Psr\Cache\CacheItemInterface $item
322
     * @return void
323
     * @throws \LogicException
324
     */
325
    public function attachItem(CacheItemInterface $item)
326
    {
327 View Code Duplication
        if(isset($this->itemInstances[$item->getKey()]) && spl_object_hash($item) !== spl_object_hash($this->itemInstances[ $item->getKey() ])){
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
328
            throw new \LogicException('The item already exists and cannot be overwritten because the Spl object hash mismatches ! You probably tried to re-attach a detached item which has been already retrieved from cache.');
329
        }else{
330
            $this->itemInstances[$item->getKey()] = $item;
331
        }
332
    }
333
334
335
    /**
336
     * @internal This method de-register an item from $this->itemInstances
337
     * @param CacheItemInterface|string $item
338
     * @throws \InvalidArgumentException
339
     */
340
    protected function deregisterItem($item)
341
    {
342
        if($item instanceof CacheItemInterface){
343
            unset($this->itemInstances[ $item->getKey() ]);
344
345
        }else if(is_string($item)){
346
            unset($this->itemInstances[ $item ]);
347
        }else{
348
            throw new \InvalidArgumentException('Invalid type for $item variable');
349
        }
350
        if(gc_enabled()){
351
            gc_collect_cycles();
352
        }
353
    }
354
355
    /**
356
     * Returns true if the item exists, is attached and the Spl Hash matches
357
     * Returns false if the item exists, is attached and the Spl Hash mismatches
358
     * Returns null if the item does not exists
359
     *
360
     * @param \Psr\Cache\CacheItemInterface $item
361
     * @return bool|null
362
     * @throws \LogicException
363
     */
364
    public function isAttached(CacheItemInterface $item)
365
    {
366
        if(isset($this->itemInstances[$item->getKey()])){
367
            return spl_object_hash($item) === spl_object_hash($this->itemInstances[ $item->getKey() ]);
368
        }
369
        return null;
370
    }
371
372
    /**
373
     * Driver-related methods
374
     */
375
376
    /**
377
     * @param \Psr\Cache\CacheItemInterface $item
378
     * @return array [
379
     *      'd' => 'THE ITEM DATA'
380
     *      't' => 'THE ITEM DATE EXPIRATION'
381
     *      'g' => 'THE ITEM TAGS'
382
     * ]
383
     *
384
     */
385
    abstract protected function driverRead(CacheItemInterface $item);
386
387
    /**
388
     * @param \Psr\Cache\CacheItemInterface $item
389
     * @return mixed
390
     */
391
    abstract protected function driverWrite(CacheItemInterface $item);
392
393
    /**
394
     * @return bool
395
     */
396
    abstract protected function driverClear();
397
398
    /**
399
     * @return bool
400
     */
401
    abstract protected function driverConnect();
402
403
    /**
404
     * @param \Psr\Cache\CacheItemInterface $item
405
     * @return bool
406
     */
407
    abstract protected function driverDelete(CacheItemInterface $item);
408
}