| Total Complexity | 75 |
| Total Lines | 355 |
| Duplicated Lines | 0 % |
| Changes | 15 | ||
| Bugs | 0 | Features | 0 |
Complex classes like SortOutFolders 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 SortOutFolders, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class SortOutFolders |
||
| 21 | { |
||
| 22 | use Configurable; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * the folder where we move images that are not in use. |
||
| 26 | * |
||
| 27 | * @var Folder |
||
| 28 | */ |
||
| 29 | protected $unusedImagesFolder; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * if set to true then dont do it for real! |
||
| 33 | * |
||
| 34 | * @var bool |
||
| 35 | */ |
||
| 36 | protected $dryRun = false; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var bool |
||
| 40 | */ |
||
| 41 | protected $verbose = true; |
||
| 42 | |||
| 43 | protected static $my_field_cache = []; |
||
| 44 | |||
| 45 | public function setVerbose(?bool $b = true) |
||
| 50 | } |
||
| 51 | |||
| 52 | public function setDryRun(?bool $b = true) |
||
| 53 | { |
||
| 54 | $this->dryRun = $b; |
||
| 55 | |||
| 56 | return $this; |
||
| 57 | } |
||
| 58 | |||
| 59 | public function runStandard() |
||
| 60 | { |
||
| 61 | $this->runAdvanced( |
||
| 62 | Config::inst()->get(PerfectCMSImages::class, 'unused_images_folder_name'), |
||
| 63 | PerfectCMSImages::get_all_values_for_images() |
||
| 64 | ); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @param array $data |
||
| 69 | * Create test jobs for the purposes of testing. |
||
| 70 | * The array must contains arrays with |
||
| 71 | * - folder |
||
| 72 | * - used_by |
||
| 73 | * used_by is an array that has ClassNames and Relations |
||
| 74 | * (has_one / has_many / many_many relations) |
||
| 75 | * e.g. Page.Image, MyDataObject.MyImages |
||
| 76 | */ |
||
| 77 | public function runAdvanced(string $unusedFolderName, array $data) |
||
| 78 | { |
||
| 79 | $this->unusedImagesFolder = Folder::find_or_make($unusedFolderName); |
||
| 80 | |||
| 81 | $folderArray = $this->getFolderArray($data); |
||
| 82 | if ($this->verbose) { |
||
| 83 | DB::alteration_message('==== List of folders ===='); |
||
| 84 | echo '<pre>' . print_r($folderArray, 1) . '</pre>'; |
||
|
|
|||
| 85 | } |
||
| 86 | |||
| 87 | DB::alteration_message('=========================================='); |
||
| 88 | |||
| 89 | $listOfImageIds = $this->getListOfImages($folderArray); |
||
| 90 | |||
| 91 | // remove |
||
| 92 | foreach ($listOfImageIds as $folderName => $listOfIds) { |
||
| 93 | if ($this->verbose) { |
||
| 94 | DB::alteration_message('<br /><br /><br />==== Checking for images to remove from <u>' . $folderName . '</u>; there are ' . count($listOfIds) . ' images to keep'); |
||
| 95 | } |
||
| 96 | $imagesLeft[$folderName] = $this->removeUnusedFiles($folderName, $listOfIds); |
||
| 97 | } |
||
| 98 | |||
| 99 | DB::alteration_message('=========================================='); |
||
| 100 | // move to right folder |
||
| 101 | foreach ($listOfImageIds as $folderName => $listOfIds) { |
||
| 102 | if ($this->verbose) { |
||
| 103 | DB::alteration_message('<br /><br /><br />==== Checking for images to move to <u>' . $folderName . '</u>'); |
||
| 104 | } |
||
| 105 | $this->moveUsedFilesIntoFolder($folderName, $listOfIds); |
||
| 106 | } |
||
| 107 | |||
| 108 | DB::alteration_message('=========================================='); |
||
| 109 | |||
| 110 | // check for rogue files |
||
| 111 | foreach (array_keys($listOfImageIds) as $folderName) { |
||
| 112 | if ($this->verbose) { |
||
| 113 | DB::alteration_message('<br /><br /><br />==== Checking for rogue FILES in <u>' . $folderName . '</u>'); |
||
| 114 | } |
||
| 115 | $this->findRoqueFilesInFolder($folderName); |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | public function getFolderArray(array $data): array |
||
| 120 | { |
||
| 121 | // check folders |
||
| 122 | $folderArray = []; |
||
| 123 | foreach ($data as $dataInner) { |
||
| 124 | $folderName = $dataInner['folder'] ?? ''; |
||
| 125 | if ($folderName) { |
||
| 126 | $folderArray[$folderName] = []; |
||
| 127 | $folderArray[$folderName]['classesAndMethods'] = []; |
||
| 128 | // $folderArray[$folderName]['resize'] = isset($dataInner['force_resize']) && $dataInner['force_resize'] === true ? true : false; |
||
| 129 | $classes = $dataInner['used_by'] ?? []; |
||
| 130 | if (! empty($classes)) { |
||
| 131 | if (is_array($classes)) { |
||
| 132 | foreach ($classes as $classAndMethod) { |
||
| 133 | $folderArray[$folderName]['classesAndMethods'][$classAndMethod] = $classAndMethod; |
||
| 134 | } |
||
| 135 | } else { |
||
| 136 | user_error('Bad definition for: ' . print_r($dataInner, 1)); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | } |
||
| 140 | } |
||
| 141 | $test = []; |
||
| 142 | foreach ($folderArray as $folderName => $folderData) { |
||
| 143 | $classAndMethodList = $folderData['classesAndMethods']; |
||
| 144 | foreach ($classAndMethodList as $classAndMethod) { |
||
| 145 | if (! isset($test[$classAndMethod])) { |
||
| 146 | $test[$classAndMethod] = true; |
||
| 147 | } else { |
||
| 148 | user_error('You have doubled up on folder for Class and Method: ' . $classAndMethod); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | } |
||
| 152 | |||
| 153 | return $folderArray; |
||
| 154 | } |
||
| 155 | |||
| 156 | public function getListOfImages(array $folderArray): array |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * returns the images in the ID list that were not found in the folder. |
||
| 200 | * |
||
| 201 | * @param string $folderName Folder moving to |
||
| 202 | * @param array $listOfImageIds Images that should be in the folder |
||
| 203 | */ |
||
| 204 | public function removeUnusedFiles(string $folderName, array $listOfImageIds) |
||
| 205 | { |
||
| 206 | $unusedFolderName = $this->unusedImagesFolder->Name; |
||
| 207 | $folder = Folder::find_or_make($folderName); |
||
| 208 | $listAsString = implode(',', $listOfImageIds); |
||
| 209 | $where = ' ParentID = ' . $folder->ID . ' AND File.ID NOT IN(' . $listAsString . ')'; |
||
| 210 | $unused = Image::get()->where($where); |
||
| 211 | if ($unused->exists()) { |
||
| 212 | foreach ($unused as $file) { |
||
| 213 | echo '.'; |
||
| 214 | $oldName = $file->getFilename(); |
||
| 215 | if ($this->verbose) { |
||
| 216 | DB::alteration_message('moving ' . $file->getFilename() . ' to ' . $unusedFolderName); |
||
| 217 | } |
||
| 218 | if (false === $this->dryRun) { |
||
| 219 | $newName = Controller::join_links($this->unusedImagesFolder->getFileName(), $file->Name); |
||
| 220 | $file = $this->moveToNewFolder($file, $this->unusedImagesFolder, $newName); |
||
| 221 | if ($newName !== $file->getFilename()) { |
||
| 222 | DB::alteration_message('ERROR: file names do not match. Compare: ' . $newName . ' with ' . $file->getFilename(), 'deleted'); |
||
| 223 | } else { |
||
| 224 | $this->physicallyMovingImage($oldName, $newName); |
||
| 225 | } |
||
| 226 | } |
||
| 227 | } |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | public function moveUsedFilesIntoFolder(string $folderName, array $listOfImageIds) |
||
| 232 | { |
||
| 233 | $folder = Folder::find_or_make($folderName); |
||
| 234 | $listAsString = implode(',', $listOfImageIds); |
||
| 235 | $where = ' ParentID <> ' . $folder->ID . ' AND File.ID IN(' . $listAsString . ')'; |
||
| 236 | $used = Image::get()->where($where); |
||
| 237 | if ($used->exists()) { |
||
| 238 | foreach ($used as $file) { |
||
| 239 | $oldName = $file->getFilename(); |
||
| 240 | |||
| 241 | $oldFolderName = $file->Parent()->getFilename(); |
||
| 242 | $newFolderName = $folder->getFilename(); |
||
| 243 | |||
| 244 | if ($this->verbose) { |
||
| 245 | DB::alteration_message('moving ' . $file->getFilename() . ' to ' . $newFolderName, 'created'); |
||
| 246 | } |
||
| 247 | if (false === $this->dryRun) { |
||
| 248 | $newName = Controller::join_links($newFolderName, $file->Name); |
||
| 249 | $file = $this->moveToNewFolder($file, $folder, $newName); |
||
| 250 | if ($newName !== $file->getFilename()) { |
||
| 251 | DB::alteration_message('ERROR: file names do not match. Compare: ' . $newName . ' with ' . $file->getFilename(), 'deleted'); |
||
| 252 | } else { |
||
| 253 | $this->physicallyMovingImage($oldName, $newName); |
||
| 254 | } |
||
| 255 | } |
||
| 256 | } |
||
| 257 | } |
||
| 258 | } |
||
| 259 | |||
| 260 | public function findRoqueFilesInFolder(string $folderName) |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | protected function getFieldDetails(string $originClassName, string $originMethod): array |
||
| 296 | { |
||
| 297 | $key = $originClassName . '_' . $originMethod; |
||
| 298 | if (! isset(self::$my_field_cache[$key])) { |
||
| 299 | $types = ['has_one', 'has_many', 'many_many']; |
||
| 300 | $classNames = ClassInfo::ancestry($originClassName, true); |
||
| 301 | foreach ($classNames as $className) { |
||
| 302 | $obj = Injector::inst()->get($className); |
||
| 303 | foreach ($types as $type) { |
||
| 304 | $rels = Config::inst()->get($className, $type, Config::UNINHERITED); |
||
| 305 | if (is_array($rels) && ! empty($rels)) { |
||
| 306 | foreach ($rels as $relName => $relType) { |
||
| 307 | if (Image::class === $relType && $relName === $originMethod) { |
||
| 308 | self::$my_field_cache[$key] = [ |
||
| 309 | 'dataClassName' => $className, |
||
| 310 | 'dataType' => $type, |
||
| 311 | ]; |
||
| 312 | } |
||
| 313 | } |
||
| 314 | } |
||
| 315 | } |
||
| 316 | } |
||
| 317 | } |
||
| 318 | |||
| 319 | return self::$my_field_cache[$key]; |
||
| 320 | } |
||
| 321 | |||
| 322 | protected function physicallyMovingImage(string $oldName, string $newName) |
||
| 323 | { |
||
| 324 | if ($oldName !== $newName) { |
||
| 325 | $oldNameFull = Controller::join_links(ASSETS_PATH, $oldName); |
||
| 326 | $newNameFull = Controller::join_links(ASSETS_PATH, $newName); |
||
| 327 | if (file_exists($oldNameFull)) { |
||
| 328 | if (file_exists($newNameFull)) { |
||
| 329 | if ($this->verbose) { |
||
| 330 | DB::alteration_message('... ... Deleting ' . $newName . ' to make place for a new file.', 'deleted'); |
||
| 331 | } |
||
| 332 | if (false === $this->dryRun) { |
||
| 333 | unlink($newNameFull); |
||
| 334 | } |
||
| 335 | } |
||
| 336 | if ($this->verbose) { |
||
| 337 | DB::alteration_message('... Moving ' . $oldNameFull . ' to ' . $newNameFull . ' (file only)', 'created'); |
||
| 338 | } |
||
| 339 | if (false === $this->dryRun) { |
||
| 340 | rename($oldNameFull, $newNameFull); |
||
| 341 | } |
||
| 342 | } elseif ($this->verbose && ! file_exists($newNameFull)) { |
||
| 343 | DB::alteration_message('... Error: could not find: ' . $oldNameFull . ' and it is also not here: ' . $newNameFull, 'created'); |
||
| 344 | } |
||
| 345 | } elseif ($this->verbose) { |
||
| 346 | DB::alteration_message('... ERROR: old and new file names are the same ' . $oldName, 'deleted'); |
||
| 347 | } |
||
| 348 | } |
||
| 349 | |||
| 350 | protected function writeFileOrFolder($fileOrFolder) |
||
| 351 | { |
||
| 352 | $fileOrFolder->writeToStage(Versioned::DRAFT); |
||
| 353 | $fileOrFolder->publishSingle(); |
||
| 354 | |||
| 355 | return $fileOrFolder; |
||
| 356 | } |
||
| 357 | |||
| 358 | |||
| 359 | protected function moveToNewFolder($image, Folder $newFolder, string $newName) |
||
| 375 | } |
||
| 376 | } |
||
| 377 |