TextCacheItemPool   A
last analyzed

Complexity

Total Complexity 22

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 96.3%

Importance

Changes 0
Metric Value
wmc 22
lcom 1
cbo 2
dl 0
loc 141
ccs 52
cts 54
cp 0.963
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A configure() 0 8 2
A deleteItem() 0 10 3
A deleteItems() 0 9 3
A save() 0 8 1
A saveDeferred() 0 6 1
A commit() 0 8 2
A hasItem() 0 4 1
A getItem() 0 12 2
A getItems() 0 10 3
A clear() 0 11 3
1
<?php
2
3
namespace PhpAbac\Cache\Pool;
4
5
use Psr\Cache\CacheItemPoolInterface;
6
use Psr\Cache\CacheItemInterface;
7
8
use PhpAbac\Cache\Item\TextCacheItem;
9
10
class TextCacheItemPool implements CacheItemPoolInterface
11
{
12
    /** @var array **/
13
    protected $deferredItems;
14
    /** @var string **/
15
    protected $cacheFolder;
16
17
    /**
18
     * @param array $options
19
     */
20 10
    public function __construct($options = [])
21
    {
22 10
        $this->configure($options);
23 10
    }
24
25
    /**
26
     * @param array $options
27
     */
28 10
    protected function configure($options)
29
    {
30 10
        $this->cacheFolder =
31 10
            (isset($options['cache_folder']))
32 10
            ? "{$options['cache_folder']}/text"
33
            : __DIR__ . '/../../../data/cache/text'
34
        ;
35 10
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 2
    public function deleteItem($key)
41
    {
42 2
        if (is_file("{$this->cacheFolder}/$key.txt")) {
43 2
            unlink("{$this->cacheFolder}/$key.txt");
44
        }
45 2
        if (isset($this->deferredItems[$key])) {
46
            unset($this->deferredItems[$key]);
47
        }
48 2
        return true;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 1
    public function deleteItems(array $keys)
55
    {
56 1
        foreach ($keys as $key) {
57 1
            if (!$this->deleteItem($key)) {
58 1
                return false;
59
            }
60
        }
61 1
        return true;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67 9
    public function save(CacheItemInterface $item)
68
    {
69 9
        $data = "{$item->get()};{$item->getExpirationDate()->format('Y-m-d H:i:s')}";
70
71 9
        file_put_contents("{$this->cacheFolder}/{$item->getKey()}.txt", $data);
72
73 9
        return true;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 2
    public function saveDeferred(CacheItemInterface $item)
80
    {
81 2
        $this->deferredItems[$item->getKey()] = $item;
82
83 2
        return true;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 2
    public function commit()
90
    {
91 2
        foreach ($this->deferredItems as $key => $item) {
92 2
            $this->save($item);
93 2
            unset($this->deferredItems[$key]);
94
        }
95 2
        return true;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101 10
    public function hasItem($key)
102
    {
103 10
        return is_file("{$this->cacheFolder}/{$key}.txt");
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109 6
    public function getItem($key)
110
    {
111 6
        $item = new TextCacheItem($key);
112 6
        if (!$this->hasItem($key)) {
113 1
            return $item;
114
        }
115 5
        $data = explode(';', file_get_contents("{$this->cacheFolder}/{$key}.txt"));
116
        return $item
117 5
            ->set($data[0])
118 5
            ->expiresAt((new \DateTime($data[1])))
119
        ;
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 2
    public function getItems(array $keys = array())
126
    {
127 2
        $items = [];
128 2
        foreach ($keys as $key) {
129 2
            if ($this->hasItem($key)) {
130 2
                $items[$key] = $this->getItem($key);
131
            }
132
        }
133 2
        return $items;
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139 10
    public function clear()
140
    {
141 10
        $items = glob("{$this->cacheFolder}/*.txt"); // get all file names
142 10
        foreach ($items as $item) { // iterate files
143 8
          if (is_file($item)) {
144 8
              unlink($item);
145
          } // delete file
146
        }
147 10
        $this->deferredItems = [];
148 10
        return true;
149
    }
150
}
151