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
|
3 |
|
public function __construct(CacheItemPoolInterface $pool, $trackMetadata = false) |
34
|
|
|
{ |
35
|
3 |
|
$this->pool = $pool; |
36
|
3 |
|
$this->trackMetadata = (bool) $trackMetadata; |
37
|
3 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
2 |
|
public function set($key, $value, $metadata = []) |
43
|
|
|
{ |
44
|
|
|
// We use null as the key to return an empty cache item |
45
|
2 |
|
$cacheItem = $this->pool->getItem($key); |
46
|
|
|
|
47
|
2 |
|
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
|
2 |
|
$cacheItem->set($value); |
66
|
2 |
|
$this->pool->saveDeferred($cacheItem); |
67
|
2 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
1 |
|
public function get($key) |
73
|
|
|
{ |
74
|
1 |
|
if(!$this->exists($key)) { |
75
|
1 |
|
return null; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$cacheItem = $this->pool->getItem($key); |
79
|
|
|
|
80
|
|
|
return $cacheItem->get() ?: null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
*/ |
86
|
1 |
|
public function exists($key) |
87
|
|
|
{ |
88
|
1 |
|
return $this->pool->hasItem($key); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritdoc} |
93
|
|
|
*/ |
94
|
1 |
|
public function delete($key) |
95
|
|
|
{ |
96
|
1 |
|
$this->pool->deleteItem($key); |
97
|
1 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
2 |
View Code Duplication |
public function getMetadata() |
|
|
|
|
103
|
|
|
{ |
104
|
2 |
|
if(!$this->trackMetadata) { |
105
|
1 |
|
return []; |
106
|
|
|
} |
107
|
|
|
|
108
|
1 |
|
$metadata = $this->pool->getItem(self::METADATA_KEY)->get(); |
109
|
|
|
|
110
|
1 |
|
if(!is_array($metadata)) { |
111
|
|
|
$metadata = []; |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
return $metadata; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
2 |
View Code Duplication |
public function getHistory() |
|
|
|
|
121
|
|
|
{ |
122
|
2 |
|
if(!$this->trackMetadata) { |
123
|
1 |
|
return []; |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
$history = $this->pool->getItem(self::HISTORY_KEY)->get(); |
127
|
|
|
|
128
|
1 |
|
if(!is_array($history)) { |
129
|
|
|
$history = []; |
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
return $history; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Saves the metadata to cache |
137
|
|
|
* |
138
|
|
|
* @param array $metadata |
139
|
|
|
*/ |
140
|
|
|
protected function saveMetadata($metadata) |
141
|
|
|
{ |
142
|
|
|
$cached = $this->pool->getItem(self::METADATA_KEY); |
143
|
|
|
$cached->set($metadata); |
144
|
|
|
|
145
|
|
|
$this->pool->saveDeferred($cached); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Saves the history to the cache |
150
|
|
|
* |
151
|
|
|
* @param array $history |
152
|
|
|
*/ |
153
|
|
|
protected function saveHistory($history) |
154
|
|
|
{ |
155
|
|
|
$cached = $this->pool->getItem(self::HISTORY_KEY); |
156
|
|
|
$cached->set($history); |
157
|
|
|
|
158
|
|
|
$this->pool->saveDeferred($cached); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Commits the cache |
163
|
|
|
*/ |
164
|
3 |
|
public function __destruct() |
165
|
|
|
{ |
166
|
3 |
|
$this->pool->commit(); |
167
|
3 |
|
} |
168
|
|
|
} |
169
|
|
|
|
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.