Passed
Branch add-cache (aacd86)
by Michael
02:10
created

CachedConfigCollection::saveMetadata()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 0
cts 5
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 2
1
<?php
2
3
namespace micmania1\config;
4
5
use Psr\Cache\CacheItemPoolInterface;
6
7
class CachedConfigCollection implements ConfigCollectionInterface
8
{
9
    /**
10
     * @const string
11
     */
12
    const METADATA_KEY = '__METADATA__';
13
14
    /**
15
     * @const string
16
     */
17
    const HISTORY_KEY = '__HISTORY__';
18
19
    /**
20
     * @var CacheItemPoolInterface
21
     */
22
    protected $pool;
23
24
    /**
25
     * @var boolean
26
     */
27
    protected $trackMetadata = false;
28
29
    /**
30
     * @param boolean $trackMetadata
31
     * @param CacheItemPoolInterface $pool
32
     */
33
    public function __construct(CacheItemPoolInterface $pool, $trackMetadata = false)
34
    {
35
        $this->pool = $pool;
36
        $this->trackMetadata = (bool) $trackMetadata;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function set($key, $value, $metadata = [])
43
    {
44
        // We use null as the key to return an empty cache item
45
        $cacheItem = $this->pool->getItem($key);
46
47
        if($this->trackMetadata) {
48
            $cachedMetadata= $this->getMetadata();
49
            $cachedHistory= $this->getHistory();
50
51
            if($this->exists($key) && isset($metadata[$key])) {
52
                array_unshift($cachedHistory, [
53
                    'value' => $value,
54
                    'metadata' => $metadata,
55
                ]);
56
            }
57
58
            $cachedMetadata[$key] = $metadata;
59
60
            $this->saveMetadata($cachedMetadata);
61
            $this->saveHistory($cachedHistory);
62
        }
63
64
        // Save our new value
65
        $cacheItem->set($value);
66
        $this->pool->saveDeferred($cacheItem);
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function get($key)
73
    {
74
        if(!$this->exists($key)) {
75
            return null;
76
        }
77
78
        $cacheItem = $this->pool->getItem($key);
79
80
        return $cacheItem->get() ?: null;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function exists($key)
87
    {
88
        return $this->pool->hasItem($key);
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function delete($key)
95
    {
96
        $this->pool->deleteItem($key);
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function getMetadata()
103
    {
104
        $metadata = $this->pool->getItem(self::METADATA_KEY)->get();
105
106
        if(!is_array($metadata)) {
107
            $metadata = [];
108
        }
109
110
        return $metadata;
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function getHistory()
117
    {
118
        $history = $this->pool->getItem(self::HISTORY_KEY);
119
120
        if(!is_array($history)) {
121
            $history = [];
122
        }
123
124
        return $history;
125
    }
126
127
    /**
128
     * Saves the metadata to cache
129
     *
130
     * @param array $metadata
131
     */
132
    protected function saveMetadata($metadata)
133
    {
134
        $cached = $this->pool->getItem(self::METADATA_KEY);
135
        $cached->set($metadata);
136
137
        $this->pool->saveDeferred($cached);
138
    }
139
140
    /**
141
     * Saves the history to the cache
142
     *
143
     * @param array $history
144
     */
145
    protected function saveHistory($history)
146
    {
147
        $cached = $this->pool->getItem(self::HISTORY_KEY);
148
        $cached->set($history);
149
150
        $this->pool->saveDeferred($cached);
151
    }
152
153
    /**
154
     * Commits the cache
155
     */
156
    public function __destruct()
157
    {
158
        $this->pool->commit();
159
    }
160
}
161