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 | 75 | 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 | */ |
||
| 448 | public function moveFolderWithinStorage($sourceFolderIdentifier, $targetFolderIdentifier, $newFolderName) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Folder equivalent to copyFileWithinStorage(). |
||
| 455 | * |
||
| 456 | * @param string $sourceFolderIdentifier |
||
| 457 | * @param string $targetFolderIdentifier |
||
| 458 | * @param string $newFolderName |
||
| 459 | * @return bool |
||
| 460 | */ |
||
| 461 | public function copyFolderWithinStorage($sourceFolderIdentifier, $targetFolderIdentifier, $newFolderName) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Returns the contents of a file. Beware that this requires to load the |
||
| 475 | * complete file into memory and also may require fetching the file from an |
||
| 476 | * external location. So this might be an expensive operation (both in terms |
||
| 477 | * of processing resources and money) for large files. |
||
| 478 | * |
||
| 479 | * @param string $fileIdentifier |
||
| 480 | * @return string The file contents |
||
| 481 | */ |
||
| 482 | 3 | public function getFileContents($fileIdentifier) |
|
| 486 | |||
| 487 | /** |
||
| 488 | * Sets the contents of a file to the specified value. |
||
| 489 | * |
||
| 490 | * @param string $fileIdentifier |
||
| 491 | * @param string $contents |
||
| 492 | * @return int The number of bytes written to the file |
||
| 493 | */ |
||
| 494 | 3 | public function setFileContents($fileIdentifier, $contents) |
|
| 500 | |||
| 501 | /** |
||
| 502 | * Checks if a file inside a folder exists |
||
| 503 | * |
||
| 504 | * @param string $fileName |
||
| 505 | * @param string $folderIdentifier |
||
| 506 | * @return bool |
||
| 507 | */ |
||
| 508 | 3 | public function fileExistsInFolder($fileName, $folderIdentifier) |
|
| 514 | |||
| 515 | /** |
||
| 516 | * Checks if a folder inside a folder exists. |
||
| 517 | * |
||
| 518 | * @param string $folderName |
||
| 519 | * @param string $folderIdentifier |
||
| 520 | * @return bool |
||
| 521 | */ |
||
| 522 | 3 | public function folderExistsInFolder($folderName, $folderIdentifier) |
|
| 528 | |||
| 529 | /** |
||
| 530 | * Returns a path to a local copy of a file for processing it. When changing the |
||
| 531 | * file, you have to take care of replacing the current version yourself! |
||
| 532 | * |
||
| 533 | * @param string $fileIdentifier |
||
| 534 | * @param bool $writable Set this to FALSE if you only need the file for read |
||
| 535 | * operations. This might speed up things, e.g. by using |
||
| 536 | * a cached local version. Never modify the file if you |
||
| 537 | * have set this flag! |
||
| 538 | * @return string The path to the file on the local disk |
||
| 539 | */ |
||
| 540 | public function getFileForLocalProcessing($fileIdentifier, $writable = true) |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Returns the permissions of a file/folder as an array |
||
| 547 | * (keys r, w) of boolean flags |
||
| 548 | * |
||
| 549 | * @param string $identifier |
||
| 550 | * @return array |
||
| 551 | */ |
||
| 552 | public function getPermissions($identifier) |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Directly output the contents of the file to the output |
||
| 562 | * buffer. Should not take care of header files or flushing |
||
| 563 | * buffer before. Will be taken care of by the Storage. |
||
| 564 | * |
||
| 565 | * @param string $identifier |
||
| 566 | * @return void |
||
| 567 | */ |
||
| 568 | public function dumpFileContents($identifier) |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Checks if a given identifier is within a container, e.g. if |
||
| 578 | * a file or folder is within another folder. |
||
| 579 | * This can e.g. be used to check for web-mounts. |
||
| 580 | * |
||
| 581 | * Hint: this also needs to return TRUE if the given identifier |
||
| 582 | * matches the container identifier to allow access to the root |
||
| 583 | * folder of a filemount. |
||
| 584 | * |
||
| 585 | * @param string $folderIdentifier |
||
| 586 | * @param string $identifier identifier to be checked against $folderIdentifier |
||
| 587 | * @return bool TRUE if $content is within or matches $folderIdentifier |
||
| 588 | */ |
||
| 589 | public function isWithin($folderIdentifier, $identifier) |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Returns information about a file. |
||
| 606 | * |
||
| 607 | * @param string $fileIdentifier |
||
| 608 | * @param array $propertiesToExtract Array of properties which are be extracted |
||
| 609 | * If empty all will be extracted |
||
| 610 | * @return array |
||
| 611 | * @throws FileDoesNotExistException |
||
| 612 | */ |
||
| 613 | 12 | public function getFileInfoByIdentifier($fileIdentifier, array $propertiesToExtract = []) |
|
| 623 | |||
| 624 | /** |
||
| 625 | * Returns information about a file. |
||
| 626 | * |
||
| 627 | * @param string $folderIdentifier |
||
| 628 | * @return array |
||
| 629 | * @throws FolderDoesNotExistException |
||
| 630 | */ |
||
| 631 | 6 | public function getFolderInfoByIdentifier($folderIdentifier) |
|
| 647 | |||
| 648 | /** |
||
| 649 | * Returns the identifier of a file inside the folder |
||
| 650 | * |
||
| 651 | * @param string $fileName |
||
| 652 | * @param string $folderIdentifier |
||
| 653 | * @return string file identifier |
||
| 654 | */ |
||
| 655 | 3 | public function getFileInFolder($fileName, $folderIdentifier) |
|
| 659 | |||
| 660 | /** |
||
| 661 | * Returns a list of files inside the specified path |
||
| 662 | * |
||
| 663 | * @param string $folderIdentifier |
||
| 664 | * @param int $start |
||
| 665 | * @param int $numberOfItems |
||
| 666 | * @param bool $recursive |
||
| 667 | * @param array $filenameFilterCallbacks callbacks for filtering the items |
||
| 668 | * @param string $sort Property name used to sort the items. |
||
| 669 | * Among them may be: '' (empty, no sorting), name, |
||
| 670 | * fileext, size, tstamp and rw. |
||
| 671 | * If a driver does not support the given property, it |
||
| 672 | * should fall back to "name". |
||
| 673 | * @param bool $sortRev TRUE to indicate reverse sorting (last to first) |
||
| 674 | * @return array of FileIdentifiers |
||
| 675 | */ |
||
| 676 | 3 | public function getFilesInFolder( |
|
| 700 | |||
| 701 | /** |
||
| 702 | * Returns the identifier of a folder inside the folder |
||
| 703 | * |
||
| 704 | * @param string $folderName The name of the target folder |
||
| 705 | * @param string $folderIdentifier |
||
| 706 | * @return string folder identifier |
||
| 707 | */ |
||
| 708 | 3 | public function getFolderInFolder($folderName, $folderIdentifier) |
|
| 713 | |||
| 714 | /** |
||
| 715 | * Returns a list of folders inside the specified path |
||
| 716 | * |
||
| 717 | * @param string $folderIdentifier |
||
| 718 | * @param int $start |
||
| 719 | * @param int $numberOfItems |
||
| 720 | * @param bool $recursive |
||
| 721 | * @param array $folderNameFilterCallbacks callbacks for filtering the items |
||
| 722 | * @param string $sort Property name used to sort the items. |
||
| 723 | * Among them may be: '' (empty, no sorting), name, |
||
| 724 | * fileext, size, tstamp and rw. |
||
| 725 | * If a driver does not support the given property, it |
||
| 726 | * should fall back to "name". |
||
| 727 | * @param bool $sortRev TRUE to indicate reverse sorting (last to first) |
||
| 728 | * @return array of Folder Identifier |
||
| 729 | * @TODO: Implement pagination with $start and $numberOfItems |
||
| 730 | * @TODO: Implement directory filter callbacks |
||
| 731 | * @TODO: Implement sorting |
||
| 732 | */ |
||
| 733 | 3 | public function getFoldersInFolder( |
|
| 758 | |||
| 759 | /** |
||
| 760 | * Returns the number of files inside the specified path |
||
| 761 | * |
||
| 762 | * @param string $folderIdentifier |
||
| 763 | * @param bool $recursive |
||
| 764 | * @param array $filenameFilterCallbacks callbacks for filtering the items |
||
| 765 | * @return int Number of files in folder |
||
| 766 | * @TODO: Implement recursive count |
||
| 767 | * @TODO: Implement filename filtering |
||
| 768 | */ |
||
| 769 | public function countFilesInFolder($folderIdentifier, $recursive = false, array $filenameFilterCallbacks = []) |
||
| 774 | |||
| 775 | /** |
||
| 776 | * Returns the number of folders inside the specified path |
||
| 777 | * |
||
| 778 | * @param string $folderIdentifier |
||
| 779 | * @param bool $recursive |
||
| 780 | * @param array $folderNameFilterCallbacks callbacks for filtering the items |
||
| 781 | * @return int Number of folders in folder |
||
| 782 | */ |
||
| 783 | 3 | public function countFoldersInFolder($folderIdentifier, $recursive = false, array $folderNameFilterCallbacks = []) |
|
| 796 | |||
| 797 | /** |
||
| 798 | * Extracts information about a file from the filesystem. |
||
| 799 | * |
||
| 800 | * @param string $filePath The absolute path to the file |
||
| 801 | * @param string $containerPath The relative path to the file's container |
||
| 802 | * @param array $propertiesToExtract array of properties which should be returned, if empty all will be extracted |
||
| 803 | * @return array |
||
| 804 | */ |
||
| 805 | 9 | protected function extractFileInformation($filePath, $containerPath, array $propertiesToExtract = array()) |
|
| 828 | |||
| 829 | /** |
||
| 830 | * Extracts a specific FileInformation from the FileSystems. |
||
| 831 | * |
||
| 832 | * @param string $fileIdentifier |
||
| 833 | * @param string $containerPath |
||
| 834 | * @param string $property |
||
| 835 | * |
||
| 836 | * @return bool|int|string |
||
| 837 | * @throws \InvalidArgumentException |
||
| 838 | */ |
||
| 839 | 9 | public function getSpecificFileInformation($fileIdentifier, $containerPath, $property) |
|
| 878 | |||
| 879 | /** |
||
| 880 | * Copies a file to a temporary path and returns that path. |
||
| 881 | * |
||
| 882 | * @param string $fileIdentifier |
||
| 883 | * @return string The temporary path |
||
| 884 | * @throws \RuntimeException |
||
| 885 | */ |
||
| 886 | protected function copyFileToTemporaryPath($fileIdentifier) |
||
| 903 | |||
| 904 | /** |
||
| 905 | * @param string $trimmedSourcePath |
||
| 906 | * @param string $trimmedTargetPath |
||
| 907 | * @return bool |
||
| 908 | */ |
||
| 909 | protected function copyFolderRecursively($trimmedSourcePath, $trimmedTargetPath) |
||
| 931 | } |
||
| 932 |