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