Complex classes like FlysystemDriver 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 FlysystemDriver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
48 | abstract class FlysystemDriver extends AbstractHierarchicalFilesystemDriver |
||
49 | { |
||
50 | /** |
||
51 | * @var FilesystemInterface |
||
52 | */ |
||
53 | protected $filesystem; |
||
54 | |||
55 | /** |
||
56 | * @var AdapterInterface |
||
57 | */ |
||
58 | protected $adapter; |
||
59 | |||
60 | /** |
||
61 | * @var string |
||
62 | */ |
||
63 | protected $entryPath; |
||
64 | |||
65 | /** |
||
66 | * FlysystemDriver constructor. |
||
67 | * @param array $configuration |
||
68 | */ |
||
69 | 72 | public function __construct(array $configuration = []) |
|
78 | |||
79 | /** |
||
80 | * Processes the configuration for this driver. |
||
81 | * @return void |
||
82 | */ |
||
83 | public function processConfiguration() |
||
86 | |||
87 | /** |
||
88 | * Merges the capabilities merged by the user at the storage |
||
89 | * configuration into the actual capabilities of the driver |
||
90 | * and returns the result. |
||
91 | * |
||
92 | * @param int $capabilities |
||
93 | * @return int |
||
94 | */ |
||
95 | public function mergeConfigurationCapabilities($capabilities) |
||
100 | |||
101 | /** |
||
102 | * Returns the identifier of the root level folder of the storage. |
||
103 | * |
||
104 | * @return string |
||
105 | */ |
||
106 | public function getRootLevelFolder() |
||
110 | |||
111 | /** |
||
112 | * Returns the identifier of the default folder new files should be put into. |
||
113 | * |
||
114 | * @return string |
||
115 | */ |
||
116 | 3 | public function getDefaultFolder() |
|
125 | |||
126 | /** |
||
127 | * Checks if a folder exists. |
||
128 | * |
||
129 | * @param string $folderIdentifier |
||
130 | * @return bool |
||
131 | */ |
||
132 | 12 | public function folderExists($folderIdentifier) |
|
146 | |||
147 | /** |
||
148 | * Creates a folder, within a parent folder. |
||
149 | * If no parent folder is given, a root level folder will be created |
||
150 | * |
||
151 | * @param string $newFolderName |
||
152 | * @param string $parentFolderIdentifier |
||
153 | * @param bool $recursive |
||
154 | * @return string the Identifier of the new folder |
||
155 | */ |
||
156 | 9 | public function createFolder($newFolderName, $parentFolderIdentifier = '', $recursive = false) |
|
167 | |||
168 | /** |
||
169 | * Returns the public URL to a file. |
||
170 | * Either fully qualified URL or relative to PATH_site (rawurlencoded). |
||
171 | * |
||
172 | * @param string $identifier |
||
173 | * @return string |
||
174 | */ |
||
175 | public function getPublicUrl($identifier) |
||
179 | |||
180 | /** |
||
181 | * Renames a folder in this storage. |
||
182 | * |
||
183 | * @param string $folderIdentifier |
||
184 | * @param string $newName |
||
185 | * @return array A map of old to new file identifiers of all affected resources |
||
186 | */ |
||
187 | 3 | public function renameFolder($folderIdentifier, $newName) |
|
197 | |||
198 | /** |
||
199 | * Removes a folder in filesystem. |
||
200 | * |
||
201 | * @param string $folderIdentifier |
||
202 | * @param bool $deleteRecursively |
||
203 | * @return bool |
||
204 | * @throws FileOperationErrorException |
||
205 | */ |
||
206 | public function deleteFolder($folderIdentifier, $deleteRecursively = false) |
||
218 | |||
219 | /** |
||
220 | * Checks if a file exists. |
||
221 | * |
||
222 | * @param string $fileIdentifier |
||
223 | * @return bool |
||
224 | */ |
||
225 | 18 | public function fileExists($fileIdentifier) |
|
232 | |||
233 | /** |
||
234 | * Checks if a folder contains files and (if supported) other folders. |
||
235 | * |
||
236 | * @param string $folderIdentifier |
||
237 | * @return bool TRUE if there are no files and folders within $folder |
||
238 | */ |
||
239 | 3 | public function isFolderEmpty($folderIdentifier) |
|
243 | |||
244 | /** |
||
245 | * Adds a file from the local server hard disk to a given path in TYPO3s |
||
246 | * virtual file system. This assumes that the local file exists, so no |
||
247 | * further check is done here! After a successful the original file must |
||
248 | * not exist anymore. |
||
249 | * |
||
250 | * @param string $localFilePath (within PATH_site) |
||
251 | * @param string $targetFolderIdentifier |
||
252 | * @param string $newFileName optional, if not given original name is used |
||
253 | * @param bool $removeOriginal if set the original file will be removed |
||
254 | * after successful operation |
||
255 | * @return string the identifier of the new file |
||
256 | */ |
||
257 | 3 | public function addFile($localFilePath, $targetFolderIdentifier, $newFileName = '', $removeOriginal = true) |
|
279 | |||
280 | /** |
||
281 | * Creates a new (empty) file and returns the identifier. |
||
282 | * |
||
283 | * @param string $fileName |
||
284 | * @param string $parentFolderIdentifier |
||
285 | * @return string |
||
286 | * @throws InvalidFileNameException |
||
287 | */ |
||
288 | 3 | public function createFile($fileName, $parentFolderIdentifier) |
|
311 | |||
312 | /** |
||
313 | * Copies a file *within* the current storage. |
||
314 | * Note that this is only about an inner storage copy action, |
||
315 | * where a file is just copied to another folder in the same storage. |
||
316 | * |
||
317 | * @param string $fileIdentifier |
||
318 | * @param string $targetFolderIdentifier |
||
319 | * @param string $fileName |
||
320 | * @return string the Identifier of the new file |
||
321 | */ |
||
322 | public function copyFileWithinStorage($fileIdentifier, $targetFolderIdentifier, $fileName) |
||
331 | |||
332 | /** |
||
333 | * Renames a file in this storage. |
||
334 | * |
||
335 | * @param string $fileIdentifier |
||
336 | * @param string $newName The target path (including the file name!) |
||
337 | * @return string The identifier of the file after renaming |
||
338 | * @throws ExistingTargetFileNameException |
||
339 | */ |
||
340 | 6 | public function renameFile($fileIdentifier, $newName) |
|
362 | |||
363 | /** |
||
364 | * Replaces a file with file in local file system. |
||
365 | * |
||
366 | * @param string $fileIdentifier |
||
367 | * @param string $localFilePath |
||
368 | * @return bool TRUE if the operation succeeded |
||
369 | */ |
||
370 | public function replaceFile($fileIdentifier, $localFilePath) |
||
378 | |||
379 | /** |
||
380 | * Removes a file from the filesystem. This does not check if the file is |
||
381 | * still used or if it is a bad idea to delete it for some other reason |
||
382 | * this has to be taken care of in the upper layers (e.g. the Storage)! |
||
383 | * |
||
384 | * @param string $fileIdentifier |
||
385 | * @return bool TRUE if deleting the file succeeded |
||
386 | */ |
||
387 | 3 | public function deleteFile($fileIdentifier) |
|
391 | |||
392 | /** |
||
393 | * Creates a hash for a file. |
||
394 | * |
||
395 | * @param string $fileIdentifier |
||
396 | * @param string $hashAlgorithm The hash algorithm to use |
||
397 | * @return string |
||
398 | */ |
||
399 | 9 | public function hash($fileIdentifier, $hashAlgorithm) |
|
420 | |||
421 | /** |
||
422 | * Moves a file *within* the current storage. |
||
423 | * Note that this is only about an inner-storage move action, |
||
424 | * where a file is just moved to another folder in the same storage. |
||
425 | * |
||
426 | * @param string $fileIdentifier |
||
427 | * @param string $targetFolderIdentifier |
||
428 | * @param string $newFileName |
||
429 | * @return string |
||
430 | */ |
||
431 | public function moveFileWithinStorage($fileIdentifier, $targetFolderIdentifier, $newFileName) |
||
435 | |||
436 | /** |
||
437 | * Folder equivalent to moveFileWithinStorage(). |
||
438 | * |
||
439 | * @param string $sourceFolderIdentifier |
||
440 | * @param string $targetFolderIdentifier |
||
441 | * @param string $newFolderName |
||
442 | * @return array All files which are affected, map of old => new file identifiers |
||
443 | */ |
||
444 | public function moveFolderWithinStorage($sourceFolderIdentifier, $targetFolderIdentifier, $newFolderName) |
||
448 | |||
449 | /** |
||
450 | * Folder equivalent to copyFileWithinStorage(). |
||
451 | * |
||
452 | * @param string $sourceFolderIdentifier |
||
453 | * @param string $targetFolderIdentifier |
||
454 | * @param string $newFolderName |
||
455 | * @return bool |
||
456 | */ |
||
457 | public function copyFolderWithinStorage($sourceFolderIdentifier, $targetFolderIdentifier, $newFolderName) |
||
461 | |||
462 | /** |
||
463 | * Returns the contents of a file. Beware that this requires to load the |
||
464 | * complete file into memory and also may require fetching the file from an |
||
465 | * external location. So this might be an expensive operation (both in terms |
||
466 | * of processing resources and money) for large files. |
||
467 | * |
||
468 | * @param string $fileIdentifier |
||
469 | * @return string The file contents |
||
470 | */ |
||
471 | 3 | public function getFileContents($fileIdentifier) |
|
475 | |||
476 | /** |
||
477 | * Sets the contents of a file to the specified value. |
||
478 | * |
||
479 | * @param string $fileIdentifier |
||
480 | * @param string $contents |
||
481 | * @return int The number of bytes written to the file |
||
482 | */ |
||
483 | 3 | public function setFileContents($fileIdentifier, $contents) |
|
489 | |||
490 | /** |
||
491 | * Checks if a file inside a folder exists |
||
492 | * |
||
493 | * @param string $fileName |
||
494 | * @param string $folderIdentifier |
||
495 | * @return bool |
||
496 | */ |
||
497 | 3 | public function fileExistsInFolder($fileName, $folderIdentifier) |
|
503 | |||
504 | /** |
||
505 | * Checks if a folder inside a folder exists. |
||
506 | * |
||
507 | * @param string $folderName |
||
508 | * @param string $folderIdentifier |
||
509 | * @return bool |
||
510 | */ |
||
511 | 3 | public function folderExistsInFolder($folderName, $folderIdentifier) |
|
517 | |||
518 | /** |
||
519 | * Returns a path to a local copy of a file for processing it. When changing the |
||
520 | * file, you have to take care of replacing the current version yourself! |
||
521 | * |
||
522 | * @param string $fileIdentifier |
||
523 | * @param bool $writable Set this to FALSE if you only need the file for read |
||
524 | * operations. This might speed up things, e.g. by using |
||
525 | * a cached local version. Never modify the file if you |
||
526 | * have set this flag! |
||
527 | * @return string The path to the file on the local disk |
||
528 | */ |
||
529 | public function getFileForLocalProcessing($fileIdentifier, $writable = true) |
||
533 | |||
534 | /** |
||
535 | * Returns the permissions of a file/folder as an array |
||
536 | * (keys r, w) of boolean flags |
||
537 | * |
||
538 | * @param string $identifier |
||
539 | * @return array |
||
540 | */ |
||
541 | public function getPermissions($identifier) |
||
548 | |||
549 | /** |
||
550 | * Directly output the contents of the file to the output |
||
551 | * buffer. Should not take care of header files or flushing |
||
552 | * buffer before. Will be taken care of by the Storage. |
||
553 | * |
||
554 | * @param string $identifier |
||
555 | * @return void |
||
556 | */ |
||
557 | public function dumpFileContents($identifier) |
||
564 | |||
565 | /** |
||
566 | * Checks if a given identifier is within a container, e.g. if |
||
567 | * a file or folder is within another folder. |
||
568 | * This can e.g. be used to check for web-mounts. |
||
569 | * |
||
570 | * Hint: this also needs to return TRUE if the given identifier |
||
571 | * matches the container identifier to allow access to the root |
||
572 | * folder of a filemount. |
||
573 | * |
||
574 | * @param string $folderIdentifier |
||
575 | * @param string $identifier identifier to be checked against $folderIdentifier |
||
576 | * @return bool TRUE if $content is within or matches $folderIdentifier |
||
577 | */ |
||
578 | public function isWithin($folderIdentifier, $identifier) |
||
592 | |||
593 | /** |
||
594 | * Returns information about a file. |
||
595 | * |
||
596 | * @param string $fileIdentifier |
||
597 | * @param array $propertiesToExtract Array of properties which are be extracted |
||
598 | * If empty all will be extracted |
||
599 | * @return array |
||
600 | * @throws FileDoesNotExistException |
||
601 | */ |
||
602 | 6 | public function getFileInfoByIdentifier($fileIdentifier, array $propertiesToExtract = []) |
|
612 | |||
613 | /** |
||
614 | * Returns information about a file. |
||
615 | * |
||
616 | * @param string $folderIdentifier |
||
617 | * @return array |
||
618 | * @throws FolderDoesNotExistException |
||
619 | */ |
||
620 | 6 | public function getFolderInfoByIdentifier($folderIdentifier) |
|
621 | { |
||
622 | 6 | $folderIdentifier = $this->canonicalizeAndCheckFolderIdentifier($folderIdentifier); |
|
623 | |||
624 | 6 | if (!$this->folderExists($folderIdentifier)) { |
|
625 | 3 | throw new FolderDoesNotExistException( |
|
626 | 3 | 'Folder "' . $folderIdentifier . '" does not exist.', |
|
627 | 1 | 1314516810 |
|
628 | 2 | ); |
|
629 | } |
||
630 | return [ |
||
631 | 3 | 'identifier' => $folderIdentifier, |
|
632 | 3 | 'name' => PathUtility::basename($folderIdentifier), |
|
633 | 3 | 'storage' => $this->storageUid |
|
634 | 2 | ]; |
|
635 | } |
||
636 | |||
637 | /** |
||
638 | * Returns the identifier of a file inside the folder |
||
639 | * |
||
640 | * @param string $fileName |
||
641 | * @param string $folderIdentifier |
||
642 | * @return string file identifier |
||
643 | */ |
||
644 | 3 | public function getFileInFolder($fileName, $folderIdentifier) |
|
645 | { |
||
646 | 3 | return $this->canonicalizeAndCheckFileIdentifier($folderIdentifier . '/' . $fileName); |
|
647 | } |
||
648 | |||
649 | /** |
||
650 | * Returns a list of files inside the specified path |
||
651 | * |
||
652 | * @param string $folderIdentifier |
||
653 | * @param int $start |
||
654 | * @param int $numberOfItems |
||
655 | * @param bool $recursive |
||
656 | * @param array $filenameFilterCallbacks callbacks for filtering the items |
||
657 | * @param string $sort Property name used to sort the items. |
||
658 | * Among them may be: '' (empty, no sorting), name, |
||
659 | * fileext, size, tstamp and rw. |
||
660 | * If a driver does not support the given property, it |
||
661 | * should fall back to "name". |
||
662 | * @param bool $sortRev TRUE to indicate reverse sorting (last to first) |
||
663 | * @return array of FileIdentifiers |
||
664 | */ |
||
665 | 3 | public function getFilesInFolder( |
|
689 | |||
690 | /** |
||
691 | * Returns the identifier of a folder inside the folder |
||
692 | * |
||
693 | * @param string $folderName The name of the target folder |
||
694 | * @param string $folderIdentifier |
||
695 | * @return string folder identifier |
||
696 | */ |
||
697 | 3 | public function getFolderInFolder($folderName, $folderIdentifier) |
|
702 | |||
703 | /** |
||
704 | * Returns a list of folders inside the specified path |
||
705 | * |
||
706 | * @param string $folderIdentifier |
||
707 | * @param int $start |
||
708 | * @param int $numberOfItems |
||
709 | * @param bool $recursive |
||
710 | * @param array $folderNameFilterCallbacks callbacks for filtering the items |
||
711 | * @param string $sort Property name used to sort the items. |
||
712 | * Among them may be: '' (empty, no sorting), name, |
||
713 | * fileext, size, tstamp and rw. |
||
714 | * If a driver does not support the given property, it |
||
715 | * should fall back to "name". |
||
716 | * @param bool $sortRev TRUE to indicate reverse sorting (last to first) |
||
717 | * @return array of Folder Identifier |
||
718 | * @TODO: Implement pagination with $start and $numberOfItems |
||
719 | * @TODO: Implement directory filter callbacks |
||
720 | * @TODO: Implement sorting |
||
721 | */ |
||
722 | 3 | public function getFoldersInFolder( |
|
747 | |||
748 | /** |
||
749 | * Returns the number of files inside the specified path |
||
750 | * |
||
751 | * @param string $folderIdentifier |
||
752 | * @param bool $recursive |
||
753 | * @param array $filenameFilterCallbacks callbacks for filtering the items |
||
754 | * @return int Number of files in folder |
||
755 | * @TODO: Implement recursive count |
||
756 | * @TODO: Implement filename filtering |
||
757 | */ |
||
758 | public function countFilesInFolder($folderIdentifier, $recursive = false, array $filenameFilterCallbacks = []) |
||
763 | |||
764 | /** |
||
765 | * Returns the number of folders inside the specified path |
||
766 | * |
||
767 | * @param string $folderIdentifier |
||
768 | * @param bool $recursive |
||
769 | * @param array $folderNameFilterCallbacks callbacks for filtering the items |
||
770 | * @return int Number of folders in folder |
||
771 | */ |
||
772 | 3 | public function countFoldersInFolder($folderIdentifier, $recursive = false, array $folderNameFilterCallbacks = []) |
|
785 | |||
786 | /** |
||
787 | * Extracts information about a file from the filesystem. |
||
788 | * |
||
789 | * @param string $filePath The absolute path to the file |
||
790 | * @param string $containerPath The relative path to the file's container |
||
791 | * @param array $propertiesToExtract array of properties which should be returned, if empty all will be extracted |
||
792 | * @return array |
||
793 | */ |
||
794 | 6 | protected function extractFileInformation($filePath, $containerPath, array $propertiesToExtract = array()) |
|
817 | |||
818 | /** |
||
819 | * Extracts a specific FileInformation from the FileSystems. |
||
820 | * |
||
821 | * @param string $fileIdentifier |
||
822 | * @param string $containerPath |
||
823 | * @param string $property |
||
824 | * |
||
825 | * @return bool|int|string |
||
826 | * @throws \InvalidArgumentException |
||
827 | */ |
||
828 | 6 | public function getSpecificFileInformation($fileIdentifier, $containerPath, $property) |
|
858 | |||
859 | /** |
||
860 | * Copies a file to a temporary path and returns that path. |
||
861 | * |
||
862 | * @param string $fileIdentifier |
||
863 | * @return string The temporary path |
||
864 | * @throws \RuntimeException |
||
865 | */ |
||
866 | protected function copyFileToTemporaryPath($fileIdentifier) |
||
883 | } |
||
884 |