Completed
Push — master ( d03e73...3597b9 )
by Matthias
01:56
created

MemoryStore::flush()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 25
rs 8.8571
cc 3
eloc 9
nc 3
nop 0
1
<?php
2
3
namespace MatthiasMullie\Scrapbook\Adapters\Collections;
4
5
use MatthiasMullie\Scrapbook\Adapters\MemoryStore as Adapter;
6
use MatthiasMullie\Scrapbook\Adapters\Collections\Utils\PrefixKeys;
7
use ReflectionObject;
8
9
/**
10
 * MemoryStore adapter for a subset of data.
11
 *
12
 * @author Matthias Mullie <[email protected]>
13
 * @copyright Copyright (c) 2014, Matthias Mullie. All rights reserved
14
 * @license LICENSE MIT
15
 */
16
class MemoryStore extends PrefixKeys
17
{
18
    /**
19
     * @param Adapter $cache
20
     * @param string  $name
21
     */
22
    public function __construct(Adapter $cache, $name)
23
    {
24
        parent::__construct($cache, $name.':');
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function flush()
31
    {
32
        /*
33
         * It's not done to use ReflectionObject, but:
34
         * - I *really* don't want to expose $cache->items publicly
35
         * - This is very specific to MemoryStore implementation, it can assume
36
         *   these kind of implementation details (like how it's ok for a child
37
         *   to use protected methods - this just can't be a subclass for
38
         *   practical reasons, but it mostly acts like one)
39
         * - Reflection is not the most optimized thing, but that doesn't matter
40
         *   too much for MemoryStore, which is not a *real* cache
41
         */
42
        $object = new ReflectionObject($this->cache);
43
        $property = $object->getProperty('items');
44
        $property->setAccessible(true);
45
        $items = $property->getValue($this->cache);
46
47
        foreach ($items as $key => $value) {
48
            if (strpos($key, $this->prefix) === 0) {
49
                $this->cache->delete($key);
50
            }
51
        }
52
53
        return true;
54
    }
55
}
56