Completed
Push — final ( 221312...0c167f )
by Georges
10s
created

ExtendedCacheItemPoolTrait::isAttached()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 7
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;
16
17
use InvalidArgumentException;
18
use phpFastCache\Cache\ExtendedCacheItemInterface;
19
use Psr\Cache\CacheItemInterface;
20
21
trait ExtendedCacheItemPoolTrait
22
{
23
    use StandardPsr6StructureTrait;
24
25
    /**
26
     * Deletes all items in the pool.
27
     * @deprecated Use clear() instead
28
     * Will be removed in 5.1
29
     *
30
     * @return bool
31
     *   True if the pool was successfully cleared. False if there was an error.
32
     */
33
    public function clean()
34
    {
35
        trigger_error('Cache clean() method is deprecated, use clear() method instead', E_USER_DEPRECATED);
36
        return $this->clear();
37
    }
38
39
    /**
40
     * @param array $keys
41
     * An indexed array of keys of items to retrieve.
42
     * @param int $option json_encode() options
43
     * @param int $depth json_encode() depth
44
     * @return string
45
     * @throws \InvalidArgumentException
46
     */
47
    public function getItemsAsJsonString(array $keys = [], $option = 0, $depth = 512)
48
    {
49
        $callback = function(CacheItemInterface $item){
50
            return $item->get();
51
        };
52
        return json_encode(array_map($callback, array_values($this->getItems($keys))), $option, $depth);
53
    }
54
55
    /**
56
     * @param string $tagName
57
     * @return \phpFastCache\Cache\ExtendedCacheItemInterface[]
58
     * @throws InvalidArgumentException
59
     */
60
    public function getItemsByTag($tagName)
61
    {
62
        if (is_string($tagName)) {
63
            $driverResponse = $this->getItem($this->getTagKey($tagName));
64
            if ($driverResponse->isHit()) {
65
                $items = (array) $driverResponse->get();
66
67
                /**
68
                 * getItems() may provides expired item(s)
69
                 * themselves provided by a cache of item
70
                 * keys based stored the tag item.
71
                 * Therefore we pass a filter callback
72
                 * to remove the expired Item(s) provided by
73
                 * the item keys passed through getItems()
74
                 *
75
                 * #headache
76
                 */
77
                return array_filter($this->getItems(array_unique(array_keys($items))), function(ExtendedCacheItemInterface $item){
78
                    return $item->isHit();
79
                });
80
            } else {
81
                return [];
82
            }
83
        } else {
84
            throw new InvalidArgumentException('$tagName must be a string');
85
        }
86
    }
87
88
    /**
89
     * @param array $tagNames
90
     * @return \phpFastCache\Cache\ExtendedCacheItemInterface[]
91
     * @throws InvalidArgumentException
92
     */
93
    public function getItemsByTags(array $tagNames)
94
    {
95
        $items = [];
96
        foreach (array_unique($tagNames) as $tagName) {
97
            $items = array_merge($items, $this->getItemsByTag($tagName));
98
        }
99
100
        return $items;
101
    }
102
103
    /**
104
     * Returns A json string that represents an array of items by tags-based.
105
     *
106
     * @param array $tagNames
107
     * An indexed array of keys of items to retrieve.
108
     * @param int $option json_encode() options
109
     * @param int $depth json_encode() depth
110
     *
111
     * @throws InvalidArgumentException
112
     *   If any of the keys in $keys are not a legal value a \Psr\Cache\InvalidArgumentException
113
     *   MUST be thrown.
114
     *
115
     * @return string
116
     */
117
    public function getItemsByTagsAsJsonString(array $tagNames, $option = 0, $depth = 512)
118
    {
119
        $callback = function(CacheItemInterface $item){
120
            return $item->get();
121
        };
122
123
        return json_encode(array_map($callback, array_values($this->getItemsByTags($tagNames))), $option, $depth);
124
    }
125
126
    /**
127
     * @param string $tagName
128
     * @return bool|null
129
     * @throws InvalidArgumentException
130
     */
131
    public function deleteItemsByTag($tagName)
132
    {
133
        if (is_string($tagName)) {
134
            $return = null;
135
            foreach ($this->getItemsByTag($tagName) as $item) {
136
                $result = $this->deleteItem($item->getKey());
137
                if ($return !== false) {
138
                    $return = $result;
139
                }
140
            }
141
142
            return $return;
143
        } else {
144
            throw new InvalidArgumentException('$tagName must be a string');
145
        }
146
    }
147
148
    /**
149
     * @param array $tagNames
150
     * @return bool|null
151
     * @throws InvalidArgumentException
152
     */
153 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...
154
    {
155
        $return = null;
156
        foreach ($tagNames as $tagName) {
157
            $result = $this->deleteItemsByTag($tagName);
158
            if ($return !== false) {
159
                $return = $result;
160
            }
161
        }
162
163
        return $return;
164
    }
165
166
    /**
167
     * @inheritdoc
168
     */
169 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...
170
    {
171
        if (is_string($tagName) && is_int($step)) {
172
            foreach ($this->getItemsByTag($tagName) as $item) {
173
                $item->increment($step);
174
                $this->saveDeferred($item);
175
            }
176
177
            return $this->commit();
178
        } else {
179
            throw new InvalidArgumentException('$tagName must be a string and $step an integer');
180
        }
181
    }
182
183
    /**
184
     * @inheritdoc
185
     */
186 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...
187
    {
188
        $return = null;
189
        foreach ($tagNames as $tagName) {
190
            $result = $this->incrementItemsByTag($tagName, $step);
191
            if ($return !== false) {
192
                $return = $result;
193
            }
194
        }
195
196
        return $return;
197
    }
198
199
    /**
200
     * @inheritdoc
201
     */
202 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...
203
    {
204
        if (is_string($tagName) && is_int($step)) {
205
            foreach ($this->getItemsByTag($tagName) as $item) {
206
                $item->decrement($step);
207
                $this->saveDeferred($item);
208
            }
209
210
            return $this->commit();
211
        } else {
212
            throw new InvalidArgumentException('$tagName must be a string and $step an integer');
213
        }
214
    }
215
216
    /**
217
     * @inheritdoc
218
     */
219 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...
220
    {
221
        $return = null;
222
        foreach ($tagNames as $tagName) {
223
            $result = $this->decrementItemsByTag($tagName, $step);
224
            if ($return !== false) {
225
                $return = $result;
226
            }
227
        }
228
229
        return $return;
230
    }
231
232
    /**
233
     * @inheritdoc
234
     */
235 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...
236
    {
237
        if (is_string($tagName)) {
238
            foreach ($this->getItemsByTag($tagName) as $item) {
239
                $item->append($data);
240
                $this->saveDeferred($item);
241
            }
242
243
            return $this->commit();
244
        } else {
245
            throw new InvalidArgumentException('$tagName must be a string');
246
        }
247
    }
248
249
    /**
250
     * @inheritdoc
251
     */
252 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...
253
    {
254
        $return = null;
255
        foreach ($tagNames as $tagName) {
256
            $result = $this->decrementItemsByTag($tagName, $data);
257
            if ($return !== false) {
258
                $return = $result;
259
            }
260
        }
261
262
        return $return;
263
    }
264
265
    /**
266
     * @inheritdoc
267
     */
268 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...
269
    {
270
        if (is_string($tagName)) {
271
            foreach ($this->getItemsByTag($tagName) as $item) {
272
                $item->prepend($data);
273
                $this->saveDeferred($item);
274
            }
275
276
            return $this->commit();
277
        } else {
278
            throw new InvalidArgumentException('$tagName must be a string');
279
        }
280
    }
281
282
    /**
283
     * @inheritdoc
284
     */
285 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...
286
    {
287
        $return = null;
288
        foreach ($tagNames as $tagName) {
289
            $result = $this->decrementItemsByTag($tagName, $data);
290
            if ($return !== false) {
291
                $return = $result;
292
            }
293
        }
294
295
        return $return;
296
    }
297
298
    /**
299
     * @param \Psr\Cache\CacheItemInterface $item
300
     * @return void
301
     */
302
    public function detachItem(CacheItemInterface $item)
303
    {
304
        if(isset($this->itemInstances[$item->getKey()])){
305
            $this->deregisterItem($item);
306
        }
307
    }
308
309
    /**
310
     * @return void
311
     */
312
    public function detachAllItems()
313
    {
314
        foreach ($this->itemInstances as $item) {
315
            $this->detachItem($item);
316
        }
317
    }
318
319
    /**
320
     * @param \Psr\Cache\CacheItemInterface $item
321
     * @return void
322
     * @throws \LogicException
323
     */
324
    public function attachItem(CacheItemInterface $item)
325
    {
326 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...
327
            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.');
328
        }else{
329
            $this->itemInstances[$item->getKey()] = $item;
330
        }
331
    }
332
333
334
    /**
335
     * @internal This method de-register an item from $this->itemInstances
336
     * @param CacheItemInterface|string $item
337
     * @throws \InvalidArgumentException
338
     */
339
    protected function deregisterItem($item)
340
    {
341
        if($item instanceof CacheItemInterface){
342
            unset($this->itemInstances[ $item->getKey() ]);
343
344
        }else if(is_string($item)){
345
            unset($this->itemInstances[ $item ]);
346
        }else{
347
            throw new \InvalidArgumentException('Invalid type for $item variable');
348
        }
349
        if(gc_enabled()){
350
            gc_collect_cycles();
351
        }
352
    }
353
354
    /**
355
     * Returns true if the item exists, is attached and the Spl Hash matches
356
     * Returns false if the item exists, is attached and the Spl Hash mismatches
357
     * Returns null if the item does not exists
358
     *
359
     * @param \Psr\Cache\CacheItemInterface $item
360
     * @return bool|null
361
     * @throws \LogicException
362
     */
363
    public function isAttached(CacheItemInterface $item)
364
    {
365
        if(isset($this->itemInstances[$item->getKey()])){
366
            return spl_object_hash($item) === spl_object_hash($this->itemInstances[ $item->getKey() ]);
367
        }
368
        return null;
369
    }
370
}