| Total Complexity | 41 |
| Total Lines | 292 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like CacheJail 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 CacheJail, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 39 | class CacheJail extends CacheWrapper { |
||
| 40 | /** |
||
| 41 | * @var string |
||
| 42 | */ |
||
| 43 | protected $root; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param \OCP\Files\Cache\ICache $cache |
||
| 47 | * @param string $root |
||
| 48 | */ |
||
| 49 | public function __construct($cache, $root) { |
||
| 50 | parent::__construct($cache); |
||
| 51 | $this->root = $root; |
||
| 52 | } |
||
| 53 | |||
| 54 | protected function getRoot() { |
||
| 55 | return $this->root; |
||
| 56 | } |
||
| 57 | |||
| 58 | protected function getSourcePath($path) { |
||
| 59 | if ($path === '') { |
||
| 60 | return $this->getRoot(); |
||
| 61 | } else { |
||
| 62 | return $this->getRoot() . '/' . ltrim($path, '/'); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @param string $path |
||
| 68 | * @return null|string the jailed path or null if the path is outside the jail |
||
| 69 | */ |
||
| 70 | protected function getJailedPath($path) { |
||
| 71 | if ($this->getRoot() === '') { |
||
| 72 | return $path; |
||
| 73 | } |
||
| 74 | $rootLength = strlen($this->getRoot()) + 1; |
||
| 75 | if ($path === $this->getRoot()) { |
||
| 76 | return ''; |
||
| 77 | } else if (substr($path, 0, $rootLength) === $this->getRoot() . '/') { |
||
| 78 | return substr($path, $rootLength); |
||
| 79 | } else { |
||
| 80 | return null; |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param ICacheEntry|array $entry |
||
| 86 | * @return array |
||
| 87 | */ |
||
| 88 | protected function formatCacheEntry($entry) { |
||
| 89 | if (isset($entry['path'])) { |
||
| 90 | $entry['path'] = $this->getJailedPath($entry['path']); |
||
| 91 | } |
||
| 92 | return $entry; |
||
|
|
|||
| 93 | } |
||
| 94 | |||
| 95 | protected function filterCacheEntry($entry) { |
||
| 96 | $rootLength = strlen($this->getRoot()) + 1; |
||
| 97 | return $rootLength === 1 || ($entry['path'] === $this->getRoot()) || (substr($entry['path'], 0, $rootLength) === $this->getRoot() . '/'); |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * get the stored metadata of a file or folder |
||
| 102 | * |
||
| 103 | * @param string /int $file |
||
| 104 | * @return ICacheEntry|false |
||
| 105 | */ |
||
| 106 | public function get($file) { |
||
| 107 | if (is_string($file) or $file == '') { |
||
| 108 | $file = $this->getSourcePath($file); |
||
| 109 | } |
||
| 110 | return parent::get($file); |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * insert meta data for a new file or folder |
||
| 115 | * |
||
| 116 | * @param string $file |
||
| 117 | * @param array $data |
||
| 118 | * |
||
| 119 | * @return int file id |
||
| 120 | * @throws \RuntimeException |
||
| 121 | */ |
||
| 122 | public function insert($file, array $data) { |
||
| 123 | return $this->getCache()->insert($this->getSourcePath($file), $data); |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * update the metadata in the cache |
||
| 128 | * |
||
| 129 | * @param int $id |
||
| 130 | * @param array $data |
||
| 131 | */ |
||
| 132 | public function update($id, array $data) { |
||
| 133 | $this->getCache()->update($id, $data); |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * get the file id for a file |
||
| 138 | * |
||
| 139 | * @param string $file |
||
| 140 | * @return int |
||
| 141 | */ |
||
| 142 | public function getId($file) { |
||
| 143 | return $this->getCache()->getId($this->getSourcePath($file)); |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * get the id of the parent folder of a file |
||
| 148 | * |
||
| 149 | * @param string $file |
||
| 150 | * @return int |
||
| 151 | */ |
||
| 152 | public function getParentId($file) { |
||
| 153 | return $this->getCache()->getParentId($this->getSourcePath($file)); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * check if a file is available in the cache |
||
| 158 | * |
||
| 159 | * @param string $file |
||
| 160 | * @return bool |
||
| 161 | */ |
||
| 162 | public function inCache($file) { |
||
| 163 | return $this->getCache()->inCache($this->getSourcePath($file)); |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * remove a file or folder from the cache |
||
| 168 | * |
||
| 169 | * @param string $file |
||
| 170 | */ |
||
| 171 | public function remove($file) { |
||
| 172 | $this->getCache()->remove($this->getSourcePath($file)); |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Move a file or folder in the cache |
||
| 177 | * |
||
| 178 | * @param string $source |
||
| 179 | * @param string $target |
||
| 180 | */ |
||
| 181 | public function move($source, $target) { |
||
| 182 | $this->getCache()->move($this->getSourcePath($source), $this->getSourcePath($target)); |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Get the storage id and path needed for a move |
||
| 187 | * |
||
| 188 | * @param string $path |
||
| 189 | * @return array [$storageId, $internalPath] |
||
| 190 | */ |
||
| 191 | protected function getMoveInfo($path) { |
||
| 192 | return [$this->getNumericStorageId(), $this->getSourcePath($path)]; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * remove all entries for files that are stored on the storage from the cache |
||
| 197 | */ |
||
| 198 | public function clear() { |
||
| 199 | $this->getCache()->remove($this->getRoot()); |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param string $file |
||
| 204 | * |
||
| 205 | * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE |
||
| 206 | */ |
||
| 207 | public function getStatus($file) { |
||
| 209 | } |
||
| 210 | |||
| 211 | private function formatSearchResults($results) { |
||
| 212 | $results = array_filter($results, array($this, 'filterCacheEntry')); |
||
| 213 | $results = array_values($results); |
||
| 214 | return array_map(array($this, 'formatCacheEntry'), $results); |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * search for files matching $pattern |
||
| 219 | * |
||
| 220 | * @param string $pattern |
||
| 221 | * @return array an array of file data |
||
| 222 | */ |
||
| 223 | public function search($pattern) { |
||
| 224 | $results = $this->getCache()->search($pattern); |
||
| 225 | return $this->formatSearchResults($results); |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * search for files by mimetype |
||
| 230 | * |
||
| 231 | * @param string $mimetype |
||
| 232 | * @return array |
||
| 233 | */ |
||
| 234 | public function searchByMime($mimetype) { |
||
| 237 | } |
||
| 238 | |||
| 239 | public function searchQuery(ISearchQuery $query) { |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * update the folder size and the size of all parent folders |
||
| 252 | * |
||
| 253 | * @param string|boolean $path |
||
| 254 | * @param array $data (optional) meta data of the folder |
||
| 255 | */ |
||
| 256 | public function correctFolderSize($path, $data = null, $isBackgroundSize = false) { |
||
| 257 | if ($this->getCache() instanceof Cache) { |
||
| 258 | $this->getCache()->correctFolderSize($this->getSourcePath($path), $data, $isBackgroundSize); |
||
| 259 | } |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * get the size of a folder and set it in the cache |
||
| 264 | * |
||
| 265 | * @param string $path |
||
| 266 | * @param array $entry (optional) meta data of the folder |
||
| 267 | * @return int |
||
| 268 | */ |
||
| 269 | public function calculateFolderSize($path, $entry = null) { |
||
| 270 | if ($this->getCache() instanceof Cache) { |
||
| 271 | return $this->getCache()->calculateFolderSize($this->getSourcePath($path), $entry); |
||
| 272 | } else { |
||
| 273 | return 0; |
||
| 274 | } |
||
| 275 | |||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * get all file ids on the files on the storage |
||
| 280 | * |
||
| 281 | * @return int[] |
||
| 282 | */ |
||
| 283 | public function getAll() { |
||
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * find a folder in the cache which has not been fully scanned |
||
| 290 | * |
||
| 291 | * If multiply incomplete folders are in the cache, the one with the highest id will be returned, |
||
| 292 | * use the one with the highest id gives the best result with the background scanner, since that is most |
||
| 293 | * likely the folder where we stopped scanning previously |
||
| 294 | * |
||
| 295 | * @return string|bool the path of the folder or false when no folder matched |
||
| 296 | */ |
||
| 297 | public function getIncomplete() { |
||
| 298 | // not supported |
||
| 299 | return false; |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * get the path of a file on this storage by it's id |
||
| 304 | * |
||
| 305 | * @param int $id |
||
| 306 | * @return string|null |
||
| 307 | */ |
||
| 308 | public function getPathById($id) { |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Move a file or folder in the cache |
||
| 319 | * |
||
| 320 | * Note that this should make sure the entries are removed from the source cache |
||
| 321 | * |
||
| 322 | * @param \OCP\Files\Cache\ICache $sourceCache |
||
| 323 | * @param string $sourcePath |
||
| 324 | * @param string $targetPath |
||
| 325 | */ |
||
| 326 | public function moveFromCache(\OCP\Files\Cache\ICache $sourceCache, $sourcePath, $targetPath) { |
||
| 331 | } |
||
| 332 | } |
||
| 333 |