PrefixReset::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace MatthiasMullie\Scrapbook\Adapters\Collections\Utils;
4
5
use MatthiasMullie\Scrapbook\KeyValueStore;
6
7
/**
8
 * @author Matthias Mullie <[email protected]>
9
 * @copyright Copyright (c) 2014, Matthias Mullie. All rights reserved
10
 * @license LICENSE MIT
11
 */
12
class PrefixReset extends PrefixKeys
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $collection;
18
19
    /**
20
     * @param string $name
21
     */
22
    public function __construct(KeyValueStore $cache, $name)
23
    {
24
        $this->cache = $cache;
25
        $this->collection = $name;
26
        parent::__construct($cache, $this->getPrefix());
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function flush()
33
    {
34
        $index = $this->cache->increment($this->collection);
35
        $this->setPrefix($this->collection.':'.$index.':');
36
37
        return false !== $index;
38
    }
39
40
    /**
41
     * @return string
42
     */
43
    protected function getPrefix()
44
    {
45
        /*
46
         * It's easy enough to just set a prefix to be used, but we can not
47
         * flush only a prefix!
48
         * Instead, we'll generate a unique prefix key, based on some name.
49
         * If we want to flush, we just create a new prefix and use that one.
50
         */
51
        $index = $this->cache->get($this->collection);
52
53
        if (false === $index) {
54
            $index = $this->cache->set($this->collection, 1);
55
        }
56
57
        return $this->collection.':'.$index.':';
58
    }
59
}
60