Total Complexity | 81 |
Total Lines | 596 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Complex classes like AllFilesInfo 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 AllFilesInfo, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class AllFilesInfo implements FileInfo |
||
24 | { |
||
25 | use FilesystemRelatedTraits; |
||
26 | use Injectable; |
||
27 | use Configurable; |
||
28 | use Cacher; |
||
29 | use FlushNow; |
||
30 | |||
31 | public static function inst(): AllFilesInfo |
||
32 | { |
||
33 | return Injector::inst() |
||
34 | ->get(AllFilesInfo::class, true, [ASSETS_PATH]); |
||
35 | } |
||
36 | |||
37 | private static array $not_real_file_substrings = [ |
||
38 | DIRECTORY_SEPARATOR . '_resampled', |
||
39 | DIRECTORY_SEPARATOR . '__', |
||
40 | DIRECTORY_SEPARATOR . '.', |
||
41 | '__Fit', |
||
42 | '__Pad', |
||
43 | '__Fill', |
||
44 | '__Focus', |
||
45 | '__Scale', |
||
46 | '__ResizedImage', |
||
47 | ]; |
||
48 | |||
49 | protected $debug = false; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | protected string $path = ''; |
||
55 | |||
56 | /** |
||
57 | * @var array |
||
58 | */ |
||
59 | protected array $dataStaging = []; |
||
60 | |||
61 | /** |
||
62 | * @var array |
||
63 | */ |
||
64 | protected array $dataLive = []; |
||
65 | |||
66 | /** |
||
67 | * @var array |
||
68 | */ |
||
69 | protected array $listOfFiles = []; |
||
70 | |||
71 | /** |
||
72 | * @var array |
||
73 | */ |
||
74 | protected array $databaseLookupListStaging = []; |
||
75 | |||
76 | |||
77 | /** |
||
78 | * @var array |
||
79 | */ |
||
80 | protected array $databaseLookupListLive = []; |
||
81 | |||
82 | |||
83 | /** |
||
84 | * @var ArrayList |
||
85 | */ |
||
86 | protected ArrayList $filesAsArrayList; |
||
87 | |||
88 | /** |
||
89 | * @var ArrayList |
||
90 | */ |
||
91 | protected ArrayList $filesAsSortedArrayList; |
||
92 | |||
93 | /** |
||
94 | * @var array |
||
95 | */ |
||
96 | protected array $availableExtensions = []; |
||
97 | |||
98 | |||
99 | |||
100 | /** |
||
101 | * @var int |
||
102 | */ |
||
103 | protected int $totalFileCountRaw = 0; |
||
104 | |||
105 | |||
106 | /** |
||
107 | * @var int |
||
108 | */ |
||
109 | protected int $totalFileCountFiltered = 0; |
||
110 | |||
111 | /** |
||
112 | * @var int |
||
113 | */ |
||
114 | protected int $totalFileSizeFiltered = 0; |
||
115 | |||
116 | /** |
||
117 | * @var int |
||
118 | */ |
||
119 | protected int $limit = 1000; |
||
120 | |||
121 | /** |
||
122 | * @var int |
||
123 | */ |
||
124 | protected int $startLimit = 0; |
||
125 | |||
126 | /** |
||
127 | * @var int |
||
128 | */ |
||
129 | protected int $endLimit = 0; |
||
130 | |||
131 | /** |
||
132 | * @var int |
||
133 | */ |
||
134 | protected int $pageNumber = 1; |
||
135 | |||
136 | /** |
||
137 | * @var string |
||
138 | */ |
||
139 | protected string $sorter = 'byfolder'; |
||
140 | protected array $sorters = []; |
||
141 | |||
142 | /** |
||
143 | * @var string |
||
144 | */ |
||
145 | protected string $filter = ''; |
||
146 | |||
147 | /** |
||
148 | * @var string |
||
149 | */ |
||
150 | protected array $filters = []; |
||
151 | |||
152 | /** |
||
153 | * @var string |
||
154 | */ |
||
155 | protected string $displayer = 'thumbs'; |
||
156 | |||
157 | /** |
||
158 | * @var array |
||
159 | */ |
||
160 | protected array $allowedExtensions = []; |
||
161 | |||
162 | |||
163 | public function __construct($path) |
||
164 | { |
||
165 | $this->path = $path; |
||
166 | } |
||
167 | |||
168 | public function getTotalFilesCount(): int |
||
169 | { |
||
170 | return (int) count($this->listOfFiles); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * does the file exists in the database on staging? |
||
175 | */ |
||
176 | public function existsOnStaging(int $id): bool |
||
177 | { |
||
178 | return isset($this->dataStaging[$id]); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * does the file exists in the database on live? |
||
183 | */ |
||
184 | public function existsOnLive(int $id): bool |
||
185 | { |
||
186 | return isset($this->dataLive[$id]); |
||
187 | } |
||
188 | |||
189 | /** |
||
190 | * get data from staging database row. |
||
191 | */ |
||
192 | public function getAnyData(string $pathFromAssets, ?int $id = 0): array |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * get data from staging database row. |
||
204 | * |
||
205 | * @param string $pathFromAssets from the root of assets |
||
206 | * @param int $id |
||
207 | */ |
||
208 | public function getStagingData(string $pathFromAssets, ?int $id = 0): array |
||
209 | { |
||
210 | if (! $id) { |
||
211 | $id = $this->databaseLookupListStaging[$pathFromAssets] ?? 0; |
||
212 | } |
||
213 | |||
214 | return $this->dataStaging[$id] ?? []; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * get data from live database row. |
||
219 | * |
||
220 | * @param string $pathFromAssets - full lookup list |
||
221 | */ |
||
222 | public function getLiveData(string $pathFromAssets, ?int $id = 0): array |
||
223 | { |
||
224 | if (! $id) { |
||
225 | $id = $this->databaseLookupListLive[$pathFromAssets] ?? 0; |
||
226 | } |
||
227 | |||
228 | return $this->dataLive[$id] ?? []; |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * find a value in a field in staging |
||
233 | * returns ID of row. |
||
234 | * |
||
235 | * @param mixed $value |
||
236 | */ |
||
237 | public function findInStagingData(string $fieldName, $value): int |
||
238 | { |
||
239 | return self::findInData($this->dataStaging, $fieldName, $value); |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * find a value in a field in live |
||
244 | * returns ID of row. |
||
245 | * |
||
246 | * @param mixed $value |
||
247 | */ |
||
248 | public function findInLiveData(string $fieldName, $value): int |
||
249 | { |
||
250 | return self::findInData($this->dataLive, $fieldName, $value); |
||
251 | } |
||
252 | |||
253 | public function getTotalFileSizesRaw() |
||
254 | { |
||
255 | $bytestotal = 0; |
||
256 | $absoluteAssetPath = realpath(ASSETS_PATH); |
||
257 | if (false !== $absoluteAssetPath && '' !== $absoluteAssetPath && file_exists($absoluteAssetPath)) { |
||
258 | foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($absoluteAssetPath, FilesystemIterator::SKIP_DOTS)) as $object) { |
||
259 | $bytestotal += $object->getSize(); |
||
260 | } |
||
261 | } |
||
262 | |||
263 | return $bytestotal; |
||
264 | } |
||
265 | |||
266 | public function toArray(): array |
||
267 | { |
||
268 | if ([] === $this->listOfFiles) { |
||
269 | $cachekey = $this->getCacheKey(); |
||
270 | if ($this->hasCacheKey($cachekey)) { |
||
271 | $this->listOfFiles = $this->getCacheValue($cachekey); |
||
272 | $this->availableExtensions = $this->getCacheValue($cachekey . 'availableExtensions'); |
||
273 | $this->dataStaging = $this->getCacheValue($cachekey . 'dataStaging'); |
||
274 | $this->dataLive = $this->getCacheValue($cachekey . 'dataLive'); |
||
275 | $this->databaseLookupListStaging = $this->getCacheValue($cachekey . 'databaseLookupStaging'); |
||
276 | $this->databaseLookupListLive = $this->getCacheValue($cachekey . 'databaseLookupLive'); |
||
277 | } else { |
||
278 | //disk |
||
279 | $diskArray = $this->getArrayOfFilesOnDisk(); |
||
280 | foreach ($diskArray as $path) { |
||
281 | $this->registerFile($path, true); |
||
282 | } |
||
283 | |||
284 | //database |
||
285 | $databaseArray = $this->getArrayOfFilesInDatabase(); |
||
286 | foreach ($databaseArray as $path) { |
||
287 | $location = trim(str_replace(ASSETS_PATH, '', $path), '/'); |
||
288 | $this->registerFile($location, false); |
||
289 | } |
||
290 | |||
291 | asort($this->listOfFiles); |
||
292 | asort($this->availableExtensions); |
||
293 | $this->setCacheValue($cachekey, $this->listOfFiles); |
||
294 | $this->setCacheValue($cachekey . 'availableExtensions', $this->availableExtensions); |
||
295 | $this->setCacheValue($cachekey . 'dataStaging', $this->dataStaging); |
||
296 | $this->setCacheValue($cachekey . 'dataLive', $this->dataLive); |
||
297 | $this->setCacheValue($cachekey . 'databaseLookupStaging', $this->databaseLookupListStaging); |
||
298 | $this->setCacheValue($cachekey . 'databaseLookupLive', $this->databaseLookupListLive); |
||
299 | } |
||
300 | } |
||
301 | |||
302 | return $this->listOfFiles; |
||
303 | } |
||
304 | |||
305 | public function getFilesAsArrayList(): ArrayList |
||
306 | { |
||
307 | if (!isset($this->filesAsArrayList)) { |
||
308 | $rawArray = $this->toArray(); |
||
309 | //prepare loop |
||
310 | $this->totalFileCountRaw = self::getTotalFilesCount(); |
||
311 | $this->filesAsArrayList = ArrayList::create(); |
||
312 | $filterFree = true; |
||
313 | $filterField = null; |
||
314 | $filterValues = []; |
||
315 | if (isset($this->filters[$this->filter])) { |
||
316 | $filterFree = false; |
||
317 | $filterField = $this->filters[$this->filter]['Field']; |
||
318 | $filterValues = $this->filters[$this->filter]['Values']; |
||
319 | } |
||
320 | |||
321 | foreach ($rawArray as $location => $fileExists) { |
||
322 | if ($this->isPathWithAllowedExtension($this->allowedExtensions, $location)) { |
||
323 | $intel = $this->getDataAboutOneFile($location, $fileExists); |
||
324 | if ($filterFree || in_array($intel[$filterField], $filterValues, 1)) { |
||
325 | ++$this->totalFileCountFiltered; |
||
326 | $this->totalFileSizeFiltered += $intel['PathFileSize']; |
||
327 | $this->filesAsArrayList->push( |
||
328 | ArrayData::create($intel) |
||
329 | ); |
||
330 | } |
||
331 | } |
||
332 | } |
||
333 | } |
||
334 | |||
335 | return $this->filesAsArrayList; |
||
336 | } |
||
337 | |||
338 | |||
339 | public function getFilesAsSortedArrayList(): ArrayList |
||
340 | { |
||
341 | if (!isset($this->filesAsSortedArrayList)) { |
||
342 | $sortField = $this->sorters[$this->sorter]['Sort']; |
||
343 | $headerField = $this->sorters[$this->sorter]['Group']; |
||
344 | $this->filesAsSortedArrayList = ArrayList::create(); |
||
345 | $this->filesAsArrayList = $this->filesAsArrayList->Sort($sortField); |
||
346 | |||
347 | $count = 0; |
||
348 | |||
349 | $innerArray = ArrayList::create(); |
||
350 | $prevHeader = 'nothing here....'; |
||
351 | $newHeader = ''; |
||
352 | foreach ($this->filesAsArrayList as $file) { |
||
353 | $newHeader = $file->{$headerField}; |
||
354 | if ($newHeader !== $prevHeader) { |
||
355 | $this->addTofilesAsSortedArrayList( |
||
356 | $prevHeader, //correct! important ... |
||
357 | $innerArray |
||
358 | ); |
||
359 | $prevHeader = $newHeader; |
||
360 | unset($innerArray); |
||
361 | $innerArray = ArrayList::create(); |
||
362 | } |
||
363 | |||
364 | if ($count >= $this->startLimit && $count < $this->endLimit) { |
||
365 | $innerArray->push($file); |
||
366 | } elseif ($count >= $this->endLimit) { |
||
367 | break; |
||
368 | } |
||
369 | |||
370 | ++$count; |
||
371 | } |
||
372 | |||
373 | //last one! |
||
374 | $this->addTofilesAsSortedArrayList( |
||
375 | $newHeader, |
||
376 | $innerArray |
||
377 | ); |
||
378 | } |
||
379 | |||
380 | return $this->filesAsSortedArrayList; |
||
381 | } |
||
382 | |||
383 | protected function addTofilesAsSortedArrayList(string $header, ArrayList $arrayList) |
||
384 | { |
||
385 | if ($arrayList->exists()) { |
||
386 | $count = $this->filesAsSortedArrayList->count(); |
||
387 | $this->filesAsSortedArrayList->push( |
||
388 | ArrayData::create( |
||
389 | [ |
||
390 | 'Number' => $count, |
||
391 | 'SubTitle' => $header, |
||
392 | 'Items' => $arrayList, |
||
393 | ] |
||
394 | ) |
||
395 | ); |
||
396 | } |
||
397 | } |
||
398 | |||
399 | protected function getDataAboutOneFile(string $location, ?bool $fileExists): array |
||
400 | { |
||
401 | return OneFileInfo::inst($location, $fileExists) |
||
402 | ->toArray(); |
||
403 | } |
||
404 | |||
405 | /** |
||
406 | * @param string $path - does not have to be full path |
||
407 | */ |
||
408 | protected function isPathWithAllowedExtension(array $allowedExtensions, string $path): bool |
||
409 | { |
||
410 | $count = count($allowedExtensions); |
||
411 | if (0 === $count) { |
||
412 | return true; |
||
413 | } |
||
414 | |||
415 | $extension = strtolower($this->getExtension($path)); |
||
416 | if ($extension === '') { |
||
417 | $extension = 'n/a'; |
||
418 | } |
||
419 | |||
420 | return in_array($extension, $allowedExtensions, true); |
||
421 | } |
||
422 | |||
423 | protected function registerFile($path, ?bool $inFileSystem = true) |
||
424 | { |
||
425 | if ($path) { |
||
426 | if (! isset($this->listOfFiles[$path])) { |
||
427 | $this->listOfFiles[$path] = $inFileSystem; |
||
428 | if ($this->debug) { |
||
429 | echo $inFileSystem ? '✓ ' : 'x '; |
||
430 | } |
||
431 | |||
432 | $extension = strtolower($this->getExtension($path)); |
||
433 | $this->availableExtensions[$extension] = $extension; |
||
434 | } |
||
435 | } |
||
436 | } |
||
437 | |||
438 | |||
439 | /** |
||
440 | * @param mixed $value |
||
441 | */ |
||
442 | protected function findInData(array $data, string $fieldName, $value): int |
||
443 | { |
||
444 | foreach ($data as $id => $row) { |
||
445 | if (isset($row[$fieldName])) { |
||
446 | if ($row[$fieldName] === $value) { |
||
447 | return (int) $id; |
||
448 | } |
||
449 | } |
||
450 | } |
||
451 | |||
452 | return 0; |
||
453 | } |
||
454 | |||
455 | protected function isRealFile(string $absolutePath): bool |
||
456 | { |
||
457 | $listOfItemsToSearchFor = Config::inst()->get(self::class, 'not_real_file_substrings'); |
||
458 | foreach ($listOfItemsToSearchFor as $test) { |
||
459 | if (strpos($absolutePath, $test)) { |
||
460 | return false; |
||
461 | } |
||
462 | } |
||
463 | |||
464 | $fileName = basename($absolutePath); |
||
465 | $isErrorPage = ('error' === substr((string) $fileName, 0, 5) && '.html' === substr((string) $fileName, -5)); |
||
466 | return ! $isErrorPage; |
||
467 | } |
||
468 | |||
469 | protected function getArrayOfFilesOnDisk(): array |
||
470 | { |
||
471 | $finalArray = []; |
||
472 | $arrayRaw = new RecursiveIteratorIterator( |
||
473 | new RecursiveDirectoryIterator($this->path), |
||
474 | RecursiveIteratorIterator::SELF_FIRST |
||
475 | ); |
||
476 | foreach ($arrayRaw as $src) { |
||
477 | $absolutePath = $src->getPathName(); |
||
478 | if (false === $this->isRealFile($absolutePath)) { |
||
479 | continue; |
||
480 | } |
||
481 | $location = trim(str_replace(ASSETS_PATH, '', $absolutePath), '/'); |
||
482 | $finalArray[$location] = $location; |
||
483 | } |
||
484 | |||
485 | return $finalArray; |
||
486 | } |
||
487 | |||
488 | /** |
||
489 | * |
||
490 | * returns all the files in the database except for folders. |
||
491 | * @return array |
||
492 | */ |
||
493 | protected function getArrayOfFilesInDatabase(): array |
||
494 | { |
||
495 | $finalArray = []; |
||
496 | foreach (['Stage', 'Live'] as $stage) { |
||
497 | Versioned::set_stage($stage); |
||
498 | $files = File::get(); |
||
499 | foreach ($files as $file) { |
||
500 | $row = $file->toMap(); |
||
501 | $location = trim($file->getFilename(), '/'); |
||
502 | if (!$location) { |
||
503 | $location = $file->generateFilename(); |
||
504 | } |
||
505 | if ('Stage' === $stage) { |
||
506 | $this->dataStaging[$row['ID']] = $row; |
||
507 | $this->databaseLookupListStaging[$location] = $row['ID']; |
||
508 | } elseif ('Live' === $stage) { |
||
509 | $this->dataLive[$row['ID']] = $row; |
||
510 | $this->databaseLookupListLive[$location] = $row['ID']; |
||
511 | } else { |
||
512 | user_error('Can not find stage'); |
||
513 | } |
||
514 | |||
515 | $finalArray[$location] = $location; |
||
516 | } |
||
517 | } |
||
518 | Versioned::set_stage('Stage'); |
||
519 | return $finalArray; |
||
520 | } |
||
521 | |||
522 | //############################################# |
||
523 | // CACHE |
||
524 | //############################################# |
||
525 | |||
526 | protected function getCacheKey(): string |
||
527 | { |
||
528 | return 'allfiles'; |
||
529 | } |
||
530 | |||
531 | |||
532 | public function getAvailableExtensions(): array |
||
533 | { |
||
534 | return $this->availableExtensions; |
||
535 | } |
||
536 | |||
537 | |||
538 | public function getTotalFileCountRaw(): int |
||
539 | { |
||
540 | return $this->totalFileCountRaw; |
||
541 | } |
||
542 | |||
543 | |||
544 | public function getTotalFileCountFiltered(): int |
||
547 | } |
||
548 | |||
549 | |||
550 | public function getTotalFileSizeFiltered(): int |
||
551 | { |
||
552 | return $this->totalFileSizeFiltered; |
||
553 | } |
||
554 | |||
555 | public function setAvailableExtensions(array $availableExtensions): static |
||
556 | { |
||
557 | $this->availableExtensions = $availableExtensions; |
||
558 | return $this; |
||
559 | } |
||
560 | |||
561 | public function setFilters(array $filters): static |
||
565 | } |
||
566 | |||
567 | public function setSorters(array $sorters): static |
||
571 | } |
||
572 | |||
573 | public function setLimit(int $limit): static |
||
577 | } |
||
578 | |||
579 | public function setStartLimit(int $startLimit): static |
||
580 | { |
||
581 | $this->startLimit = $startLimit; |
||
582 | return $this; |
||
583 | } |
||
584 | |||
585 | public function setEndLimit(int $endLimit): static |
||
589 | } |
||
590 | |||
591 | public function setPageNumber(int $pageNumber): static |
||
592 | { |
||
593 | $this->pageNumber = $pageNumber; |
||
594 | return $this; |
||
595 | } |
||
596 | |||
597 | public function setSorter(string $sorter): static |
||
598 | { |
||
599 | $this->sorter = $sorter; |
||
600 | return $this; |
||
601 | } |
||
602 | |||
603 | public function setFilter(string $filter): static |
||
604 | { |
||
605 | $this->filter = $filter; |
||
606 | return $this; |
||
607 | } |
||
608 | |||
609 | public function setDisplayer(string $displayer): static |
||
610 | { |
||
611 | $this->displayer = $displayer; |
||
612 | return $this; |
||
613 | } |
||
614 | |||
615 | public function setAllowedExtensions(array $allowedExtensions): static |
||
619 | } |
||
620 | } |
||
621 |