Total Complexity | 45 |
Total Lines | 317 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 1 | Features | 2 |
Complex classes like JibitCache often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use JibitCache, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class JibitCache |
||
16 | { |
||
17 | |||
18 | /** |
||
19 | * The path to the cache file folder |
||
20 | * |
||
21 | * @var string |
||
22 | */ |
||
23 | private $_cachepath = 'cache/'; |
||
24 | |||
25 | /** |
||
26 | * The name of the default cache file |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | private $_cachename = 'default'; |
||
31 | |||
32 | /** |
||
33 | * The cache file extension |
||
34 | * |
||
35 | * @var string |
||
36 | */ |
||
37 | private $_extension = '.cache'; |
||
38 | |||
39 | /** |
||
40 | * Default constructor |
||
41 | * |
||
42 | * @param string|array [optional] $config |
||
43 | * @return void |
||
44 | */ |
||
45 | public function __construct($config = null) |
||
46 | { |
||
47 | if (true === isset($config)) { |
||
48 | if (is_string($config)) { |
||
49 | $this->setCache($config); |
||
50 | } elseif (is_array($config)) { |
||
51 | $this->setCache($config['name']); |
||
52 | $this->setCachePath($config['path']); |
||
53 | $this->setExtension($config['extension']); |
||
54 | } |
||
55 | } |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * Cache name Setter |
||
60 | * |
||
61 | * @param string $name |
||
62 | * @return object |
||
63 | */ |
||
64 | public function setCache($name) |
||
65 | { |
||
66 | $this->_cachename = $name; |
||
67 | return $this; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Check whether data accociated with a key |
||
72 | * |
||
73 | * @param string $key |
||
74 | * @return boolean |
||
75 | */ |
||
76 | public function isCached($key) |
||
77 | { |
||
78 | if (false != $this->_loadCache()) { |
||
79 | $cachedData = $this->_loadCache(); |
||
80 | return isset($cachedData[$key]['data']); |
||
81 | } |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * Load appointed cache |
||
86 | * |
||
87 | * @return mixed |
||
88 | */ |
||
89 | private function _loadCache() |
||
90 | { |
||
91 | if (true === file_exists($this->getCacheDir())) { |
||
92 | $file = file_get_contents($this->getCacheDir()); |
||
93 | return json_decode($file, true); |
||
94 | } else { |
||
95 | return false; |
||
96 | } |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Get the cache directory path |
||
101 | * |
||
102 | * @return string |
||
103 | */ |
||
104 | public function getCacheDir() |
||
105 | { |
||
106 | if (true === $this->_checkCacheDir()) { |
||
|
|||
107 | $filename = $this->getCache(); |
||
108 | $filename = preg_replace('/[^0-9a-z\.\_\-]/i', '', strtolower($filename)); |
||
109 | return $this->getCachePath() . $this->_getHash($filename) . $this->getExtension(); |
||
110 | } |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Check if a writable cache directory exists and if not create a new one |
||
115 | * |
||
116 | * @return boolean |
||
117 | */ |
||
118 | private function _checkCacheDir() |
||
119 | { |
||
120 | if (!is_dir($this->getCachePath()) && !mkdir($this->getCachePath(), 0775, true)) { |
||
121 | throw new \Exception('Unable to create cache directory ' . $this->getCachePath()); |
||
122 | } elseif (!is_readable($this->getCachePath()) || !is_writable($this->getCachePath())) { |
||
123 | if (!chmod($this->getCachePath(), 0775)) { |
||
124 | throw new \Exception($this->getCachePath() . ' must be readable and writeable'); |
||
125 | } |
||
126 | } |
||
127 | return true; |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Cache path Getter |
||
132 | * |
||
133 | * @return string |
||
134 | */ |
||
135 | public function getCachePath() |
||
136 | { |
||
137 | return $this->_cachepath; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Cache path Setter |
||
142 | * |
||
143 | * @param string $path |
||
144 | * @return object |
||
145 | */ |
||
146 | public function setCachePath($path) |
||
147 | { |
||
148 | $this->_cachepath = $path; |
||
149 | return $this; |
||
150 | } |
||
151 | |||
152 | /** |
||
153 | * Cache name Getter |
||
154 | * |
||
155 | * @return void |
||
156 | */ |
||
157 | public function getCache() |
||
158 | { |
||
159 | return $this->_cachename; |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * Get the filename hash |
||
164 | * |
||
165 | * @return string |
||
166 | */ |
||
167 | private function _getHash($filename) |
||
168 | { |
||
169 | return sha1($filename); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Cache file extension Getter |
||
174 | * |
||
175 | * @return string |
||
176 | */ |
||
177 | public function getExtension() |
||
178 | { |
||
179 | return $this->_extension; |
||
180 | } |
||
181 | |||
182 | /** |
||
183 | * Cache file extension Setter |
||
184 | * |
||
185 | * @param string $ext |
||
186 | * @return object |
||
187 | */ |
||
188 | public function setExtension($ext) |
||
189 | { |
||
190 | $this->_extension = $ext; |
||
191 | return $this; |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * Store data in the cache |
||
196 | * |
||
197 | * @param string $key |
||
198 | * @param mixed $data |
||
199 | * @param integer [optional] $expiration |
||
200 | * @return object |
||
201 | */ |
||
202 | public function store($key, $data, $expiration = 0) |
||
203 | { |
||
204 | $storeData = array( |
||
205 | 'time' => time(), |
||
206 | 'expire' => $expiration, |
||
207 | 'data' => serialize($data) |
||
208 | ); |
||
209 | $dataArray = $this->_loadCache(); |
||
210 | if (true === is_array($dataArray)) { |
||
211 | $dataArray[$key] = $storeData; |
||
212 | } else { |
||
213 | $dataArray = array($key => $storeData); |
||
214 | } |
||
215 | $cacheData = json_encode($dataArray); |
||
216 | file_put_contents($this->getCacheDir(), $cacheData); |
||
217 | return $this; |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Retrieve cached data by its key |
||
222 | * |
||
223 | * @param string $key |
||
224 | * @param boolean [optional] $timestamp |
||
225 | * @return string |
||
226 | */ |
||
227 | public function retrieve($key, $timestamp = false) |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Retrieve all cached data |
||
237 | * |
||
238 | * @param boolean [optional] $meta |
||
239 | * @return array |
||
240 | */ |
||
241 | public function retrieveAll($meta = false) |
||
242 | { |
||
243 | if ($meta === false) { |
||
244 | $results = array(); |
||
245 | $cachedData = $this->_loadCache(); |
||
246 | if ($cachedData) { |
||
247 | foreach ($cachedData as $k => $v) { |
||
248 | $results[$k] = unserialize($v['data']); |
||
249 | } |
||
250 | } |
||
251 | return $results; |
||
252 | } else { |
||
253 | return $this->_loadCache(); |
||
254 | } |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Erase cached entry by its key |
||
259 | * |
||
260 | * @param string $key |
||
261 | * @return object |
||
262 | */ |
||
263 | public function erase($key) |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Erase all expired entries |
||
280 | * |
||
281 | * @return integer |
||
282 | */ |
||
283 | public function eraseExpired() |
||
299 | } |
||
300 | } |
||
301 | |||
302 | /** |
||
303 | * Check whether a timestamp is still in the duration |
||
304 | * |
||
305 | * @param integer $timestamp |
||
306 | * @param integer $expiration |
||
307 | * @return boolean |
||
308 | */ |
||
309 | private function _checkExpired($timestamp, $expiration) |
||
317 | } |
||
318 | |||
319 | /** |
||
320 | * Erase all cached entries |
||
321 | * |
||
322 | * @return object |
||
323 | */ |
||
324 | public function eraseAll() |
||
332 | } |
||
333 | } |
||
334 |