| Total Complexity | 45 |
| Total Lines | 319 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 2 | 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) |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Cache name Getter |
||
| 154 | * |
||
| 155 | * @return void |
||
| 156 | */ |
||
| 157 | public function getCache() |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Retrieve all cached data |
||
| 239 | * |
||
| 240 | * @param boolean [optional] $meta |
||
| 241 | * @return array |
||
| 242 | */ |
||
| 243 | public function retrieveAll($meta = false) |
||
| 244 | { |
||
| 245 | if ($meta === false) { |
||
| 246 | $results = array(); |
||
| 247 | $cachedData = $this->_loadCache(); |
||
| 248 | if ($cachedData) { |
||
| 249 | foreach ($cachedData as $k => $v) { |
||
| 250 | $results[$k] = unserialize($v['data']); |
||
| 251 | } |
||
| 252 | } |
||
| 253 | return $results; |
||
| 254 | } else { |
||
| 255 | return $this->_loadCache(); |
||
| 256 | } |
||
| 257 | } |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Erase cached entry by its key |
||
| 261 | * |
||
| 262 | * @param string $key |
||
| 263 | * @return object |
||
| 264 | */ |
||
| 265 | public function erase($key) |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Erase all expired entries |
||
| 282 | * |
||
| 283 | * @return integer |
||
| 284 | */ |
||
| 285 | public function eraseExpired() |
||
| 301 | } |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Check whether a timestamp is still in the duration |
||
| 306 | * |
||
| 307 | * @param integer $timestamp |
||
| 308 | * @param integer $expiration |
||
| 309 | * @return boolean |
||
| 310 | */ |
||
| 311 | private function _checkExpired($timestamp, $expiration) |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Erase all cached entries |
||
| 323 | * |
||
| 324 | * @return object |
||
| 325 | */ |
||
| 326 | public function eraseAll() |
||
| 336 |