| Total Complexity | 49 |
| Total Lines | 249 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| 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 |
||
| 28 | class SortOutFolders |
||
| 29 | { |
||
| 30 | |||
| 31 | use Configurable; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var Folder |
||
| 35 | */ |
||
| 36 | protected $unusedImagesFolder = null; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * if set to true then dont do it for real! |
||
| 40 | * @var bool |
||
| 41 | */ |
||
| 42 | protected $dryRun = false; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * |
||
| 46 | * @var bool |
||
| 47 | */ |
||
| 48 | protected $verbose = true; |
||
| 49 | |||
| 50 | private static $unused_images_folder_name = 'unusedimages'; |
||
| 51 | |||
| 52 | public function setVerbose(?bool $b = true) |
||
| 56 | } |
||
| 57 | |||
| 58 | public function setDryRun(?bool $b = true) |
||
| 59 | { |
||
| 60 | $this->dryRun = $b; |
||
| 61 | return $this; |
||
| 62 | } |
||
| 63 | |||
| 64 | |||
| 65 | /** |
||
| 66 | * @param string $unusedFolderName |
||
| 67 | * @param array $data |
||
| 68 | * Create test jobs for the purposes of testing. |
||
| 69 | * The array must contains arrays with |
||
| 70 | * - folder |
||
| 71 | * - used_by |
||
| 72 | * used_by is an array that has ClassNames and Relations |
||
| 73 | * (has_one / has_many / many_many relations) |
||
| 74 | * e.g. Page.Image, MyDataObject.MyImages |
||
| 75 | * |
||
| 76 | * @param HTTPRequest $request |
||
| 77 | */ |
||
| 78 | public function run(string $unusedFolderName, array $data) // phpcs:ignore |
||
| 79 | { |
||
| 80 | $this->unusedImagesFolder = Folder::find_or_make($unusedFolderName); |
||
| 81 | |||
| 82 | $folderArray = $this->getFolderArray($data); |
||
| 83 | if ($this->verbose) { |
||
| 84 | DB::alteration_message('==== List of folders ===='); |
||
| 85 | echo '<pre>'.print_r($folderArray, 1).'</pre>'; |
||
|
|
|||
| 86 | } |
||
| 87 | |||
| 88 | $listOfImageIds = $this->getListOfImages($folderArray); |
||
| 89 | |||
| 90 | // remove |
||
| 91 | $imagesLeft = []; |
||
| 92 | foreach($listOfImageIds as $folderName => $listOfIds) { |
||
| 93 | DB::alteration_message('==== DOING '.$folderName.' of Image IDs ===='. count($listOfIds).' images to keep'); |
||
| 94 | $imagesLeft[$folderName] = $this->removeUnusedFiles($folderName, $listOfIds); |
||
| 95 | } |
||
| 96 | |||
| 97 | // reintroduce |
||
| 98 | foreach($imagesLeft as $folderName => $listOfIds) { |
||
| 99 | DB::alteration_message('==== DOING '.$folderName.' of Image IDs ===='. count($listOfIds).' images to re-introduce'); |
||
| 100 | $this->moveUsedFilesIntoFolder($folderName, $listOfIds); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | |||
| 105 | public function getFolderArray(array $data) :array |
||
| 106 | { |
||
| 107 | // check folders |
||
| 108 | $folderArray = []; |
||
| 109 | foreach($data as $dataInner) { |
||
| 110 | $folder = $dataInner['folder'] ?? ''; |
||
| 111 | if($folder) { |
||
| 112 | $folderArray[$folder] = []; |
||
| 113 | $classes = $dataInner['used_by'] ?? []; |
||
| 114 | if(! empty($classes)) { |
||
| 115 | if(is_array($classes)) { |
||
| 116 | foreach($classes as $classAndMethodList) { |
||
| 117 | $folderArray[$folder][$classAndMethodList] = $classAndMethodList; |
||
| 118 | } |
||
| 119 | } else { |
||
| 120 | user_error('Bad definition for: '.print_r($dataInner, 1)); |
||
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | } |
||
| 125 | return $folderArray; |
||
| 126 | } |
||
| 127 | |||
| 128 | public function getListOfImages(array $folderArray) : array |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * returns the images in the ID list that were not found in the folder. |
||
| 163 | * @param string $folderName Folder moving to |
||
| 164 | * @param array $listOfImageIds Images that should be in the folder |
||
| 165 | * @return array Unused images |
||
| 166 | */ |
||
| 167 | public function removeUnusedFiles(string $folderName, array $listOfImageIds) : array |
||
| 168 | { |
||
| 169 | $unusedFolderName = $this->unusedImagesFolder->Name; |
||
| 170 | $folder = Folder::find_or_make($folderName); |
||
| 171 | $listAsString = implode(',', $listOfImageIds); |
||
| 172 | $where = ' ParentID = ' . $folder->ID. ' AND File.ID NOT IN('.$listAsString.')'; |
||
| 173 | $unused = Image::get()->where($where); |
||
| 174 | if ($unused->exists()) { |
||
| 175 | foreach ($unused as $file) { |
||
| 176 | if (in_array($file->ID, $listOfImageIds)) { |
||
| 177 | unset($array[array_search($file->ID, $listOfImageIds)]); |
||
| 178 | } |
||
| 179 | $oldName = $file->getFileName(); |
||
| 180 | if($this->verbose) { |
||
| 181 | DB::alteration_message('moving '.$file->getFileName().' to '.$unusedFolderName); |
||
| 182 | } |
||
| 183 | if($this->dryRun === false) { |
||
| 184 | $file->ParentID = $this->unusedImagesFolder->ID; |
||
| 185 | $file->write(); |
||
| 186 | $file->doPublish(); |
||
| 187 | $newName = str_replace($folder->Name, $unusedFolderName, $oldName); |
||
| 188 | if($newName !== $file->getFileName()) { |
||
| 189 | DB::alteration_message('ERROR: file names do not match. Compare: '.$newName. ' with ' . $file->getFileName()); |
||
| 190 | } |
||
| 191 | $this->physicallyMovingImage($oldName, $newName); |
||
| 192 | } |
||
| 193 | } |
||
| 194 | } |
||
| 195 | return $listOfImageIds; |
||
| 196 | } |
||
| 197 | |||
| 198 | public function moveUsedFilesIntoFolder(string $folderName, array $listOfImageIds) |
||
| 199 | { |
||
| 200 | $folder = Folder::find_or_make($folderName); |
||
| 201 | $listAsString = implode(',', $listOfImageIds); |
||
| 202 | $where = ' ParentID <> ' . $folder->ID. ' AND File.ID IN('.$listAsString.')'; |
||
| 203 | $used = Image::get()->where($where); |
||
| 204 | if ($used->exists()) { |
||
| 205 | foreach ($used as $file) { |
||
| 206 | $oldFolderName = $file->Parent()->Name; |
||
| 207 | $oldName = $file->getFileName(); |
||
| 208 | if($this->verbose) { |
||
| 209 | DB::alteration_message('moving '.$file->getFileName().' to '.$folderName); |
||
| 210 | } |
||
| 211 | if($this->dryRun === false) { |
||
| 212 | $file->ParentID = $folder->ID; |
||
| 213 | $file->write(); |
||
| 214 | $file->doPublish(); |
||
| 215 | if($oldFolderName === '') { |
||
| 216 | $newName = $folder->Name . '/' . $oldName; |
||
| 217 | } else { |
||
| 218 | $newName = str_replace($oldFolderName, $folder->Name, $oldName); |
||
| 219 | } |
||
| 220 | if($this->verbose && $newName !== $file->getFileName()) { |
||
| 221 | DB::alteration_message('ERROR: file names do not match. Compare: '.$newName. ' with ' . $file->getFileName(), 'deleted'); |
||
| 222 | } else { |
||
| 223 | $this->physicallyMovingImage($oldName, $newName); |
||
| 224 | } |
||
| 225 | } |
||
| 226 | } |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | protected static $my_field_cache = []; |
||
| 231 | |||
| 232 | protected function getFieldDetails(string $originClassName, string $originMethod) : array |
||
| 256 | } |
||
| 257 | |||
| 258 | protected function physicallyMovingImage(string $oldName, string $newName) |
||
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 281 |