| Total Complexity | 45 |
| Total Lines | 318 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 |
||
| 14 | class JibitCache |
||
| 15 | { |
||
| 16 | |||
| 17 | /** |
||
| 18 | * The path to the cache file folder |
||
| 19 | * |
||
| 20 | * @var string |
||
| 21 | */ |
||
| 22 | private $_cachepath = 'cache/'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The name of the default cache file |
||
| 26 | * |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | private $_cachename = 'default'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * The cache file extension |
||
| 33 | * |
||
| 34 | * @var string |
||
| 35 | */ |
||
| 36 | private $_extension = '.cache'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Default constructor |
||
| 40 | * |
||
| 41 | * @param string|array [optional] $config |
||
| 42 | * @return void |
||
| 43 | */ |
||
| 44 | public function __construct($config = null) |
||
| 45 | { |
||
| 46 | if (true === isset($config)) { |
||
| 47 | if (is_string($config)) { |
||
| 48 | $this->setCache($config); |
||
| 49 | } else if (is_array($config)) { |
||
| 50 | $this->setCache($config['name']); |
||
| 51 | $this->setCachePath($config['path']); |
||
| 52 | $this->setExtension($config['extension']); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Cache name Setter |
||
| 59 | * |
||
| 60 | * @param string $name |
||
| 61 | * @return object |
||
| 62 | */ |
||
| 63 | public function setCache($name) |
||
| 64 | { |
||
| 65 | $this->_cachename = $name; |
||
| 66 | return $this; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Check whether data accociated with a key |
||
| 71 | * |
||
| 72 | * @param string $key |
||
| 73 | * @return boolean |
||
| 74 | */ |
||
| 75 | public function isCached($key) |
||
| 76 | { |
||
| 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 | } |
||
| 335 |