Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Storage 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Storage, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class Storage implements IStorage { |
||
| 35 | |||
| 36 | // hidden file which indicate that the folder is a valid key storage |
||
| 37 | const KEY_STORAGE_MARKER = '.oc_key_storage'; |
||
| 38 | |||
| 39 | /** @var View */ |
||
| 40 | private $view; |
||
| 41 | |||
| 42 | /** @var Util */ |
||
| 43 | private $util; |
||
| 44 | |||
| 45 | // base dir where all the file related keys are stored |
||
| 46 | /** @var string */ |
||
| 47 | private $keys_base_dir; |
||
| 48 | |||
| 49 | // root of the key storage default is empty which means that we use the data folder |
||
| 50 | /** @var string */ |
||
| 51 | private $root_dir; |
||
| 52 | |||
| 53 | /** @var string */ |
||
| 54 | private $encryption_base_dir; |
||
| 55 | |||
| 56 | /** @var array */ |
||
| 57 | private $keyCache = []; |
||
| 58 | |||
| 59 | /** @var string */ |
||
| 60 | private $currentUser = null; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @param View $view view |
||
| 64 | * @param Util $util encryption util class |
||
| 65 | * @param IUserSession $session user session |
||
| 66 | */ |
||
| 67 | public function __construct(View $view, Util $util, IUserSession $session) { |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @inheritdoc |
||
| 82 | */ |
||
| 83 | public function getUserKey($uid, $keyId, $encryptionModuleId) { |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @inheritdoc |
||
| 90 | */ |
||
| 91 | public function getFileKey($path, $keyId, $encryptionModuleId) { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @inheritdoc |
||
| 109 | */ |
||
| 110 | public function getSystemUserKey($keyId, $encryptionModuleId) { |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @inheritdoc |
||
| 117 | */ |
||
| 118 | public function setUserKey($uid, $keyId, $key, $encryptionModuleId) { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @inheritdoc |
||
| 125 | */ |
||
| 126 | public function setFileKey($path, $keyId, $key, $encryptionModuleId) { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @inheritdoc |
||
| 133 | */ |
||
| 134 | public function setSystemUserKey($keyId, $key, $encryptionModuleId) { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * @inheritdoc |
||
| 141 | */ |
||
| 142 | public function deleteUserKey($uid, $keyId, $encryptionModuleId) { |
||
| 143 | try { |
||
| 144 | $path = $this->constructUserKeyPath($encryptionModuleId, $keyId, $uid); |
||
| 145 | return !$this->view->file_exists($path) || $this->view->unlink($path); |
||
| 146 | } catch (NoUserException $e) { |
||
| 147 | // this exception can come from initMountPoints() from setupUserMounts() |
||
| 148 | // for a deleted user. |
||
| 149 | // |
||
| 150 | // It means, that: |
||
| 151 | // - we are not running in alternative storage mode because we don't call |
||
| 152 | // initMountPoints() in that mode |
||
| 153 | // - the keys were in the user's home but since the user was deleted, the |
||
| 154 | // user's home is gone and so are the keys |
||
| 155 | // |
||
| 156 | // So there is nothing to do, just ignore. |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @inheritdoc |
||
| 162 | */ |
||
| 163 | public function deleteFileKey($path, $keyId, $encryptionModuleId) { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @inheritdoc |
||
| 170 | */ |
||
| 171 | public function deleteAllFileKeys($path) { |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @inheritdoc |
||
| 178 | */ |
||
| 179 | public function deleteSystemUserKey($keyId, $encryptionModuleId) { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @inheritdoc |
||
| 186 | */ |
||
| 187 | |||
| 188 | public function deleteAltUserStorageKeys($uid) { |
||
| 204 | |||
| 205 | /** |
||
| 206 | * construct path to users key |
||
| 207 | * |
||
| 208 | * @param string $encryptionModuleId |
||
| 209 | * @param string $keyId |
||
| 210 | * @param string $uid |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | protected function constructUserKeyPath($encryptionModuleId, $keyId, $uid) { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * read key from hard disk |
||
| 228 | * |
||
| 229 | * @param string $path to key |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | private function getKey($path) { |
||
| 247 | |||
| 248 | /** |
||
| 249 | * write key to disk |
||
| 250 | * |
||
| 251 | * |
||
| 252 | * @param string $path path to key directory |
||
| 253 | * @param string $key key |
||
| 254 | * @return bool |
||
| 255 | */ |
||
| 256 | private function setKey($path, $key) { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * get path to key folder for a given file |
||
| 271 | * |
||
| 272 | * @param string $encryptionModuleId |
||
| 273 | * @param string $path path to the file, relative to data/ |
||
| 274 | * @return string |
||
| 275 | */ |
||
| 276 | View Code Duplication | private function getFileKeyDir($encryptionModuleId, $path) { |
|
| 290 | |||
| 291 | /** |
||
| 292 | * move keys if a file was renamed |
||
| 293 | * |
||
| 294 | * @param string $source |
||
| 295 | * @param string $target |
||
| 296 | * @return boolean |
||
| 297 | */ |
||
| 298 | View Code Duplication | public function renameKeys($source, $target) { |
|
| 312 | |||
| 313 | |||
| 314 | /** |
||
| 315 | * copy keys if a file was renamed |
||
| 316 | * |
||
| 317 | * @param string $source |
||
| 318 | * @param string $target |
||
| 319 | * @return boolean |
||
| 320 | */ |
||
| 321 | View Code Duplication | public function copyKeys($source, $target) { |
|
| 334 | |||
| 335 | /** |
||
| 336 | * get system wide path and detect mount points |
||
| 337 | * |
||
| 338 | * @param string $path |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | View Code Duplication | protected function getPathToKeys($path) { |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Make preparations to filesystem for saving a key file |
||
| 357 | * |
||
| 358 | * @param string $path relative to the views root |
||
| 359 | */ |
||
| 360 | protected function keySetPreparation($path) { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Setup the mounts of the given user if different than |
||
| 376 | * the current user. |
||
| 377 | * |
||
| 378 | * This is needed because in many cases the keys are stored |
||
| 379 | * within the user's home storage. |
||
| 380 | * |
||
| 381 | * @param string $uid user id |
||
| 382 | */ |
||
| 383 | protected function setupUserMounts($uid) { |
||
| 393 | |||
| 394 | } |
||
| 395 |