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 |
||
50 | abstract class FlysystemDriver extends AbstractHierarchicalFilesystemDriver |
||
51 | { |
||
52 | /** |
||
53 | * @var FilesystemInterface |
||
54 | */ |
||
55 | protected $filesystem; |
||
56 | |||
57 | /** |
||
58 | * @var AdapterInterface |
||
59 | */ |
||
60 | protected $adapter; |
||
61 | |||
62 | /** |
||
63 | * @var string |
||
64 | */ |
||
65 | protected $entryPath; |
||
66 | |||
67 | /** |
||
68 | * FlysystemDriver constructor. |
||
69 | * @param array $configuration |
||
70 | */ |
||
71 | 78 | public function __construct(array $configuration = []) |
|
80 | |||
81 | /** |
||
82 | * Processes the configuration for this driver. |
||
83 | * @return void |
||
84 | */ |
||
85 | public function processConfiguration() |
||
88 | |||
89 | /** |
||
90 | * Merges the capabilities merged by the user at the storage |
||
91 | * configuration into the actual capabilities of the driver |
||
92 | * and returns the result. |
||
93 | * |
||
94 | * @param int $capabilities |
||
95 | * @return int |
||
96 | */ |
||
97 | public function mergeConfigurationCapabilities($capabilities) |
||
102 | |||
103 | /** |
||
104 | * Returns the identifier of the root level folder of the storage. |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | public function getRootLevelFolder() |
||
112 | |||
113 | /** |
||
114 | * Returns the identifier of the default folder new files should be put into. |
||
115 | * |
||
116 | * @return string |
||
117 | */ |
||
118 | 3 | public function getDefaultFolder() |
|
127 | |||
128 | /** |
||
129 | * Checks if a folder exists. |
||
130 | * |
||
131 | * @param string $folderIdentifier |
||
132 | * @return bool |
||
133 | */ |
||
134 | 12 | public function folderExists($folderIdentifier) |
|
148 | |||
149 | /** |
||
150 | * Creates a folder, within a parent folder. |
||
151 | * If no parent folder is given, a root level folder will be created |
||
152 | * |
||
153 | * @param string $newFolderName |
||
154 | * @param string $parentFolderIdentifier |
||
155 | * @param bool $recursive |
||
156 | * @return string the Identifier of the new folder |
||
157 | */ |
||
158 | 9 | public function createFolder($newFolderName, $parentFolderIdentifier = '', $recursive = false) |
|
169 | |||
170 | /** |
||
171 | * Returns the public URL to a file. |
||
172 | * Either fully qualified URL or relative to PATH_site (rawurlencoded). |
||
173 | * |
||
174 | * @param string $identifier |
||
175 | * @return string |
||
176 | */ |
||
177 | public function getPublicUrl($identifier) |
||
181 | |||
182 | /** |
||
183 | * Renames a folder in this storage. |
||
184 | * |
||
185 | * @param string $folderIdentifier |
||
186 | * @param string $newName |
||
187 | * @return array A map of old to new file identifiers of all affected resources |
||
188 | */ |
||
189 | 3 | public function renameFolder($folderIdentifier, $newName) |
|
199 | |||
200 | /** |
||
201 | * Removes a folder in filesystem. |
||
202 | * |
||
203 | * @param string $folderIdentifier |
||
204 | * @param bool $deleteRecursively |
||
205 | * @return bool |
||
206 | * @throws FileOperationErrorException |
||
207 | */ |
||
208 | public function deleteFolder($folderIdentifier, $deleteRecursively = false) |
||
220 | |||
221 | /** |
||
222 | * Checks if a file exists. |
||
223 | * |
||
224 | * @param string $fileIdentifier |
||
225 | * @return bool |
||
226 | */ |
||
227 | 18 | public function fileExists($fileIdentifier) |
|
234 | |||
235 | /** |
||
236 | * Checks if a folder contains files and (if supported) other folders. |
||
237 | * |
||
238 | * @param string $folderIdentifier |
||
239 | * @return bool TRUE if there are no files and folders within $folder |
||
240 | */ |
||
241 | 3 | public function isFolderEmpty($folderIdentifier) |
|
245 | |||
246 | /** |
||
247 | * Adds a file from the local server hard disk to a given path in TYPO3s |
||
248 | * virtual file system. This assumes that the local file exists, so no |
||
249 | * further check is done here! After a successful the original file must |
||
250 | * not exist anymore. |
||
251 | * |
||
252 | * @param string $localFilePath (within PATH_site) |
||
253 | * @param string $targetFolderIdentifier |
||
254 | * @param string $newFileName optional, if not given original name is used |
||
255 | * @param bool $removeOriginal if set the original file will be removed |
||
256 | * after successful operation |
||
257 | * @return string the identifier of the new file |
||
258 | */ |
||
259 | 3 | public function addFile($localFilePath, $targetFolderIdentifier, $newFileName = '', $removeOriginal = true) |
|
281 | |||
282 | /** |
||
283 | * Creates a new (empty) file and returns the identifier. |
||
284 | * |
||
285 | * @param string $fileName |
||
286 | * @param string $parentFolderIdentifier |
||
287 | * @return string |
||
288 | * @throws InvalidFileNameException |
||
289 | */ |
||
290 | 3 | public function createFile($fileName, $parentFolderIdentifier) |
|
313 | |||
314 | /** |
||
315 | * Copies a file *within* the current storage. |
||
316 | * Note that this is only about an inner storage copy action, |
||
317 | * where a file is just copied to another folder in the same storage. |
||
318 | * |
||
319 | * @param string $fileIdentifier |
||
320 | * @param string $targetFolderIdentifier |
||
321 | * @param string $fileName |
||
322 | * @return string the Identifier of the new file |
||
323 | */ |
||
324 | public function copyFileWithinStorage($fileIdentifier, $targetFolderIdentifier, $fileName) |
||
335 | |||
336 | /** |
||
337 | * Renames a file in this storage. |
||
338 | * |
||
339 | * @param string $fileIdentifier |
||
340 | * @param string $newName The target path (including the file name!) |
||
341 | * @return string The identifier of the file after renaming |
||
342 | * @throws ExistingTargetFileNameException |
||
343 | */ |
||
344 | 6 | public function renameFile($fileIdentifier, $newName) |
|
366 | |||
367 | /** |
||
368 | * Replaces a file with file in local file system. |
||
369 | * |
||
370 | * @param string $fileIdentifier |
||
371 | * @param string $localFilePath |
||
372 | * @return bool TRUE if the operation succeeded |
||
373 | */ |
||
374 | public function replaceFile($fileIdentifier, $localFilePath) |
||
382 | |||
383 | /** |
||
384 | * Removes a file from the filesystem. This does not check if the file is |
||
385 | * still used or if it is a bad idea to delete it for some other reason |
||
386 | * this has to be taken care of in the upper layers (e.g. the Storage)! |
||
387 | * |
||
388 | * @param string $fileIdentifier |
||
389 | * @return bool TRUE if deleting the file succeeded |
||
390 | */ |
||
391 | 3 | public function deleteFile($fileIdentifier) |
|
395 | |||
396 | /** |
||
397 | * Creates a hash for a file. |
||
398 | * |
||
399 | * @param string $fileIdentifier |
||
400 | * @param string $hashAlgorithm The hash algorithm to use |
||
401 | * @return string |
||
402 | */ |
||
403 | 9 | public function hash($fileIdentifier, $hashAlgorithm) |
|
424 | |||
425 | /** |
||
426 | * Moves a file *within* the current storage. |
||
427 | * Note that this is only about an inner-storage move action, |
||
428 | * where a file is just moved to another folder in the same storage. |
||
429 | * |
||
430 | * @param string $fileIdentifier |
||
431 | * @param string $targetFolderIdentifier |
||
432 | * @param string $newFileName |
||
433 | * @return string |
||
434 | */ |
||
435 | public function moveFileWithinStorage($fileIdentifier, $targetFolderIdentifier, $newFileName) |
||
439 | |||
440 | /** |
||
441 | * Folder equivalent to moveFileWithinStorage(). |
||
442 | * |
||
443 | * @param string $sourceFolderIdentifier |
||
444 | * @param string $targetFolderIdentifier |
||
445 | * @param string $newFolderName |
||
446 | * @return array All files which are affected, map of old => new file identifiers |
||
447 | * @throws FolderDoesNotExistException |
||
448 | */ |
||
449 | public function moveFolderWithinStorage($sourceFolderIdentifier, $targetFolderIdentifier, $newFolderName) |
||
471 | |||
472 | /** |
||
473 | * Folder equivalent to copyFileWithinStorage(). |
||
474 | * |
||
475 | * @param string $sourceFolderIdentifier |
||
476 | * @param string $targetFolderIdentifier |
||
477 | * @param string $newFolderName |
||
478 | * @return bool |
||
479 | */ |
||
480 | public function copyFolderWithinStorage($sourceFolderIdentifier, $targetFolderIdentifier, $newFolderName) |
||
491 | |||
492 | /** |
||
493 | * Returns the contents of a file. Beware that this requires to load the |
||
494 | * complete file into memory and also may require fetching the file from an |
||
495 | * external location. So this might be an expensive operation (both in terms |
||
496 | * of processing resources and money) for large files. |
||
497 | * |
||
498 | * @param string $fileIdentifier |
||
499 | * @return string The file contents |
||
500 | */ |
||
501 | 3 | public function getFileContents($fileIdentifier) |
|
505 | |||
506 | /** |
||
507 | * Sets the contents of a file to the specified value. |
||
508 | * |
||
509 | * @param string $fileIdentifier |
||
510 | * @param string $contents |
||
511 | * @return int The number of bytes written to the file |
||
512 | */ |
||
513 | 3 | public function setFileContents($fileIdentifier, $contents) |
|
519 | |||
520 | /** |
||
521 | * Checks if a file inside a folder exists |
||
522 | * |
||
523 | * @param string $fileName |
||
524 | * @param string $folderIdentifier |
||
525 | * @return bool |
||
526 | */ |
||
527 | 3 | public function fileExistsInFolder($fileName, $folderIdentifier) |
|
533 | |||
534 | /** |
||
535 | * Checks if a folder inside a folder exists. |
||
536 | * |
||
537 | * @param string $folderName |
||
538 | * @param string $folderIdentifier |
||
539 | * @return bool |
||
540 | */ |
||
541 | 3 | public function folderExistsInFolder($folderName, $folderIdentifier) |
|
547 | |||
548 | /** |
||
549 | * Returns a path to a local copy of a file for processing it. When changing the |
||
550 | * file, you have to take care of replacing the current version yourself! |
||
551 | * |
||
552 | * @param string $fileIdentifier |
||
553 | * @param bool $writable Set this to FALSE if you only need the file for read |
||
554 | * operations. This might speed up things, e.g. by using |
||
555 | * a cached local version. Never modify the file if you |
||
556 | * have set this flag! |
||
557 | * @return string The path to the file on the local disk |
||
558 | */ |
||
559 | public function getFileForLocalProcessing($fileIdentifier, $writable = true) |
||
563 | |||
564 | /** |
||
565 | * Returns the permissions of a file/folder as an array |
||
566 | * (keys r, w) of boolean flags |
||
567 | * |
||
568 | * @param string $identifier |
||
569 | * @return array |
||
570 | */ |
||
571 | public function getPermissions($identifier) |
||
578 | |||
579 | /** |
||
580 | * Directly output the contents of the file to the output |
||
581 | * buffer. Should not take care of header files or flushing |
||
582 | * buffer before. Will be taken care of by the Storage. |
||
583 | * |
||
584 | * @param string $identifier |
||
585 | * @return void |
||
586 | */ |
||
587 | public function dumpFileContents($identifier) |
||
594 | |||
595 | /** |
||
596 | * Checks if a given identifier is within a container, e.g. if |
||
597 | * a file or folder is within another folder. |
||
598 | * This can e.g. be used to check for web-mounts. |
||
599 | * |
||
600 | * Hint: this also needs to return TRUE if the given identifier |
||
601 | * matches the container identifier to allow access to the root |
||
602 | * folder of a filemount. |
||
603 | * |
||
604 | * @param string $folderIdentifier |
||
605 | * @param string $identifier identifier to be checked against $folderIdentifier |
||
606 | * @return bool TRUE if $content is within or matches $folderIdentifier |
||
607 | */ |
||
608 | 3 | public function isWithin($folderIdentifier, $identifier) |
|
625 | |||
626 | /** |
||
627 | * Returns information about a file. |
||
628 | * |
||
629 | * @param string $fileIdentifier |
||
630 | * @param array $propertiesToExtract Array of properties which are be extracted |
||
631 | * If empty all will be extracted |
||
632 | * @return array |
||
633 | * @throws FileDoesNotExistException |
||
634 | */ |
||
635 | 12 | public function getFileInfoByIdentifier($fileIdentifier, array $propertiesToExtract = []) |
|
645 | |||
646 | /** |
||
647 | * Returns information about a file. |
||
648 | * |
||
649 | * @param string $folderIdentifier |
||
650 | * @return array |
||
651 | * @throws FolderDoesNotExistException |
||
652 | */ |
||
653 | 6 | public function getFolderInfoByIdentifier($folderIdentifier) |
|
669 | |||
670 | /** |
||
671 | * Returns the identifier of a file inside the folder |
||
672 | * |
||
673 | * @param string $fileName |
||
674 | * @param string $folderIdentifier |
||
675 | * @return string file identifier |
||
676 | */ |
||
677 | 3 | public function getFileInFolder($fileName, $folderIdentifier) |
|
681 | |||
682 | /** |
||
683 | * Returns a list of files inside the specified path |
||
684 | * |
||
685 | * @param string $folderIdentifier |
||
686 | * @param int $start |
||
687 | * @param int $numberOfItems |
||
688 | * @param bool $recursive |
||
689 | * @param array $filenameFilterCallbacks callbacks for filtering the items |
||
690 | * @param string $sort Property name used to sort the items. |
||
691 | * Among them may be: '' (empty, no sorting), name, |
||
692 | * fileext, size, tstamp and rw. |
||
693 | * If a driver does not support the given property, it |
||
694 | * should fall back to "name". |
||
695 | * @param bool $sortRev TRUE to indicate reverse sorting (last to first) |
||
696 | * @return array of FileIdentifiers |
||
697 | */ |
||
698 | 3 | public function getFilesInFolder( |
|
722 | |||
723 | /** |
||
724 | * Returns the identifier of a folder inside the folder |
||
725 | * |
||
726 | * @param string $folderName The name of the target folder |
||
727 | * @param string $folderIdentifier |
||
728 | * @return string folder identifier |
||
729 | */ |
||
730 | 3 | public function getFolderInFolder($folderName, $folderIdentifier) |
|
735 | |||
736 | /** |
||
737 | * Returns a list of folders inside the specified path |
||
738 | * |
||
739 | * @param string $folderIdentifier |
||
740 | * @param int $start |
||
741 | * @param int $numberOfItems |
||
742 | * @param bool $recursive |
||
743 | * @param array $folderNameFilterCallbacks callbacks for filtering the items |
||
744 | * @param string $sort Property name used to sort the items. |
||
745 | * Among them may be: '' (empty, no sorting), name, |
||
746 | * fileext, size, tstamp and rw. |
||
747 | * If a driver does not support the given property, it |
||
748 | * should fall back to "name". |
||
749 | * @param bool $sortRev TRUE to indicate reverse sorting (last to first) |
||
750 | * @return array of Folder Identifier |
||
751 | * @TODO: Implement pagination with $start and $numberOfItems |
||
752 | * @TODO: Implement directory filter callbacks |
||
753 | * @TODO: Implement sorting |
||
754 | */ |
||
755 | 3 | public function getFoldersInFolder( |
|
780 | |||
781 | /** |
||
782 | * Returns the number of files inside the specified path |
||
783 | * |
||
784 | * @param string $folderIdentifier |
||
785 | * @param bool $recursive |
||
786 | * @param array $filenameFilterCallbacks callbacks for filtering the items |
||
787 | * @return int Number of files in folder |
||
788 | * @TODO: Implement recursive count |
||
789 | * @TODO: Implement filename filtering |
||
790 | */ |
||
791 | public function countFilesInFolder($folderIdentifier, $recursive = false, array $filenameFilterCallbacks = []) |
||
796 | |||
797 | /** |
||
798 | * Returns the number of folders inside the specified path |
||
799 | * |
||
800 | * @param string $folderIdentifier |
||
801 | * @param bool $recursive |
||
802 | * @param array $folderNameFilterCallbacks callbacks for filtering the items |
||
803 | * @return int Number of folders in folder |
||
804 | */ |
||
805 | 3 | public function countFoldersInFolder($folderIdentifier, $recursive = false, array $folderNameFilterCallbacks = []) |
|
818 | |||
819 | /** |
||
820 | * Extracts information about a file from the filesystem. |
||
821 | * |
||
822 | * @param string $filePath The absolute path to the file |
||
823 | * @param string $containerPath The relative path to the file's container |
||
824 | * @param array $propertiesToExtract array of properties which should be returned, if empty all will be extracted |
||
825 | * @return array |
||
826 | */ |
||
827 | 9 | protected function extractFileInformation($filePath, $containerPath, array $propertiesToExtract = array()) |
|
850 | |||
851 | /** |
||
852 | * Extracts a specific FileInformation from the FileSystems. |
||
853 | * |
||
854 | * @param string $fileIdentifier |
||
855 | * @param string $containerPath |
||
856 | * @param string $property |
||
857 | * |
||
858 | * @return bool|int|string |
||
859 | * @throws \InvalidArgumentException |
||
860 | */ |
||
861 | 9 | public function getSpecificFileInformation($fileIdentifier, $containerPath, $property) |
|
900 | |||
901 | /** |
||
902 | * Copies a file to a temporary path and returns that path. |
||
903 | * |
||
904 | * @param string $fileIdentifier |
||
905 | * @return string The temporary path |
||
906 | * @throws \RuntimeException |
||
907 | */ |
||
908 | protected function copyFileToTemporaryPath($fileIdentifier) |
||
925 | |||
926 | /** |
||
927 | * @param string $trimmedSourcePath |
||
928 | * @param string $trimmedTargetPath |
||
929 | * @return bool |
||
930 | */ |
||
931 | protected function copyFolderRecursively($trimmedSourcePath, $trimmedTargetPath) |
||
953 | } |
||
954 |