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 = []) |
|
72 | { |
||
73 | 78 | parent::__construct($configuration); |
|
74 | // The capabilities default of this driver. See CAPABILITY_* constants for possible values |
||
75 | 78 | $this->capabilities = |
|
76 | 26 | ResourceStorage::CAPABILITY_BROWSABLE |
|
77 | 78 | | ResourceStorage::CAPABILITY_PUBLIC |
|
78 | 78 | | ResourceStorage::CAPABILITY_WRITABLE; |
|
79 | 78 | } |
|
80 | |||
81 | /** |
||
82 | * Processes the configuration for this driver. |
||
83 | * @return void |
||
84 | */ |
||
85 | public function processConfiguration() |
||
86 | { |
||
87 | } |
||
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) |
||
98 | { |
||
99 | $this->capabilities &= $capabilities; |
||
100 | return $this->capabilities; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Returns the identifier of the root level folder of the storage. |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | public function getRootLevelFolder() |
||
109 | { |
||
110 | return '/'; |
||
111 | } |
||
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() |
|
119 | { |
||
120 | 3 | $identifier = '/user_upload/'; |
|
121 | 3 | $createFolder = !$this->folderExists($identifier); |
|
122 | 3 | if (true === $createFolder) { |
|
123 | 3 | $identifier = $this->createFolder('user_upload'); |
|
124 | 2 | } |
|
125 | 3 | return $identifier; |
|
126 | } |
||
127 | |||
128 | /** |
||
129 | * Checks if a folder exists. |
||
130 | * |
||
131 | * @param string $folderIdentifier |
||
132 | * @return bool |
||
133 | */ |
||
134 | 12 | public function folderExists($folderIdentifier) |
|
135 | { |
||
136 | 12 | $normalizedIdentifier = $this->canonicalizeAndCheckFolderIdentifier($folderIdentifier); |
|
137 | 12 | $normalizedIdentifier = ltrim(rtrim($normalizedIdentifier, '/'), '/'); |
|
138 | |||
139 | 12 | if ('/' === $folderIdentifier) { |
|
140 | return true; |
||
141 | } else { |
||
142 | return ( |
||
143 | 12 | $this->filesystem->has($normalizedIdentifier) |
|
144 | 12 | && $this->filesystem->get($normalizedIdentifier)->isDir() |
|
145 | 8 | ); |
|
146 | } |
||
147 | } |
||
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) |
|
159 | { |
||
160 | 9 | $parentFolderIdentifier = $this->canonicalizeAndCheckFolderIdentifier($parentFolderIdentifier); |
|
161 | 9 | $newFolderName = trim($newFolderName, '/'); |
|
162 | |||
163 | 9 | $newFolderName = $this->sanitizeFileName($newFolderName); |
|
164 | 9 | $newIdentifier = $parentFolderIdentifier . $newFolderName . '/'; |
|
165 | 9 | $this->filesystem->createDir($newIdentifier); |
|
166 | |||
167 | 9 | return $newIdentifier; |
|
168 | } |
||
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) |
||
178 | { |
||
179 | return '/'; |
||
180 | } |
||
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) |
|
190 | { |
||
191 | 3 | $renameResult = $this->filesystem->rename($folderIdentifier, $newName); |
|
192 | |||
193 | 3 | if (true === $renameResult) { |
|
194 | 3 | return [$folderIdentifier => $newName]; |
|
195 | } else { |
||
196 | return [$folderIdentifier => $folderIdentifier]; |
||
197 | } |
||
198 | } |
||
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) |
||
209 | { |
||
210 | $folderIdentifier = ltrim($folderIdentifier, '/'); |
||
211 | $result = $this->filesystem->deleteDir($folderIdentifier); |
||
212 | |||
213 | if (false === $result) { |
||
214 | throw new FileOperationErrorException( |
||
215 | 'Deleting folder "' . $folderIdentifier . '" failed.', |
||
216 | 1330119451 |
||
217 | ); |
||
218 | } |
||
219 | return $result; |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * Checks if a file exists. |
||
224 | * |
||
225 | * @param string $fileIdentifier |
||
226 | * @return bool |
||
227 | */ |
||
228 | 18 | public function fileExists($fileIdentifier) |
|
229 | { |
||
230 | 18 | if ($this->filesystem->has($fileIdentifier) && !$this->filesystem->get($fileIdentifier)->isDir()) { |
|
231 | 18 | return true; |
|
232 | } |
||
233 | 12 | return false; |
|
234 | } |
||
235 | |||
236 | /** |
||
237 | * Checks if a folder contains files and (if supported) other folders. |
||
238 | * |
||
239 | * @param string $folderIdentifier |
||
240 | * @return bool TRUE if there are no files and folders within $folder |
||
241 | */ |
||
242 | 3 | public function isFolderEmpty($folderIdentifier) |
|
243 | { |
||
244 | 3 | return 0 === count($this->filesystem->listContents($folderIdentifier)); |
|
245 | } |
||
246 | |||
247 | /** |
||
248 | * Adds a file from the local server hard disk to a given path in TYPO3s |
||
249 | * virtual file system. This assumes that the local file exists, so no |
||
250 | * further check is done here! After a successful the original file must |
||
251 | * not exist anymore. |
||
252 | * |
||
253 | * @param string $localFilePath (within PATH_site) |
||
254 | * @param string $targetFolderIdentifier |
||
255 | * @param string $newFileName optional, if not given original name is used |
||
256 | * @param bool $removeOriginal if set the original file will be removed |
||
257 | * after successful operation |
||
258 | * @return string the identifier of the new file |
||
259 | */ |
||
260 | 5 | public function addFile($localFilePath, $targetFolderIdentifier, $newFileName = '', $removeOriginal = true) |
|
261 | { |
||
262 | 3 | $localFilePath = $this->canonicalizeAndCheckFilePath($localFilePath); |
|
263 | 3 | $newFileName = $this->sanitizeFileName($newFileName !== '' ? $newFileName : PathUtility::basename($localFilePath)); |
|
264 | 3 | $newFileIdentifier = $this->canonicalizeAndCheckFolderIdentifier($targetFolderIdentifier) . $newFileName; |
|
265 | |||
266 | 3 | $targetPath = ltrim($newFileIdentifier, '/'); |
|
267 | |||
268 | 3 | $content = file_get_contents($localFilePath); |
|
269 | |||
270 | 5 | if ($removeOriginal) { |
|
271 | $result = $this->filesystem->put($targetPath, $content); |
||
272 | unlink($localFilePath); |
||
273 | } else { |
||
274 | 3 | $result = $this->filesystem->put($targetPath, $content); |
|
275 | } |
||
276 | 3 | if ($result === false || !$this->filesystem->has($targetPath)) { |
|
277 | throw new \RuntimeException('Adding file ' . $localFilePath . ' at ' . $newFileIdentifier . ' failed.'); |
||
278 | } |
||
279 | 3 | clearstatcache(); |
|
280 | 3 | return $newFileIdentifier; |
|
281 | } |
||
282 | |||
283 | /** |
||
284 | * Creates a new (empty) file and returns the identifier. |
||
285 | * |
||
286 | * @param string $fileName |
||
287 | * @param string $parentFolderIdentifier |
||
288 | * @return string |
||
289 | * @throws InvalidFileNameException |
||
290 | */ |
||
291 | 3 | public function createFile($fileName, $parentFolderIdentifier) |
|
314 | |||
315 | /** |
||
316 | * Copies a file *within* the current storage. |
||
317 | * Note that this is only about an inner storage copy action, |
||
318 | * where a file is just copied to another folder in the same storage. |
||
319 | * |
||
320 | * @param string $fileIdentifier |
||
321 | * @param string $targetFolderIdentifier |
||
322 | * @param string $fileName |
||
323 | * @return string the Identifier of the new file |
||
324 | */ |
||
325 | public function copyFileWithinStorage($fileIdentifier, $targetFolderIdentifier, $fileName) |
||
326 | { |
||
327 | $newFileIdentifier = $this->canonicalizeAndCheckFileIdentifier($targetFolderIdentifier . '/' . $fileName); |
||
328 | |||
329 | $trimmedSourceFile = ltrim($fileIdentifier, '/'); |
||
330 | $trimmedTargetFile = ltrim($newFileIdentifier, '/'); |
||
331 | |||
332 | $this->filesystem->copy($trimmedSourceFile, $trimmedTargetFile); |
||
333 | |||
334 | return $newFileIdentifier; |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * Renames a file in this storage. |
||
339 | * |
||
340 | * @param string $fileIdentifier |
||
341 | * @param string $newName The target path (including the file name!) |
||
342 | * @return string The identifier of the file after renaming |
||
343 | * @throws ExistingTargetFileNameException |
||
344 | */ |
||
345 | 6 | public function renameFile($fileIdentifier, $newName) |
|
367 | |||
368 | /** |
||
369 | * Replaces a file with file in local file system. |
||
370 | * |
||
371 | * @param string $fileIdentifier |
||
372 | * @param string $localFilePath |
||
373 | * @return bool TRUE if the operation succeeded |
||
374 | */ |
||
375 | public function replaceFile($fileIdentifier, $localFilePath) |
||
383 | |||
384 | /** |
||
385 | * Removes a file from the filesystem. This does not check if the file is |
||
386 | * still used or if it is a bad idea to delete it for some other reason |
||
387 | * this has to be taken care of in the upper layers (e.g. the Storage)! |
||
388 | * |
||
389 | * @param string $fileIdentifier |
||
390 | * @return bool TRUE if deleting the file succeeded |
||
391 | */ |
||
392 | 3 | public function deleteFile($fileIdentifier) |
|
396 | |||
397 | /** |
||
398 | * Creates a hash for a file. |
||
399 | * |
||
400 | * @param string $fileIdentifier |
||
401 | * @param string $hashAlgorithm The hash algorithm to use |
||
402 | * @return string |
||
403 | */ |
||
404 | 9 | public function hash($fileIdentifier, $hashAlgorithm) |
|
425 | |||
426 | /** |
||
427 | * Moves a file *within* the current storage. |
||
428 | * Note that this is only about an inner-storage move action, |
||
429 | * where a file is just moved to another folder in the same storage. |
||
430 | * |
||
431 | * @param string $fileIdentifier |
||
432 | * @param string $targetFolderIdentifier |
||
433 | * @param string $newFileName |
||
434 | * @return string |
||
435 | */ |
||
436 | public function moveFileWithinStorage($fileIdentifier, $targetFolderIdentifier, $newFileName) |
||
440 | |||
441 | /** |
||
442 | * Folder equivalent to moveFileWithinStorage(). |
||
443 | * |
||
444 | * @param string $sourceFolderIdentifier |
||
445 | * @param string $targetFolderIdentifier |
||
446 | * @param string $newFolderName |
||
447 | * @return array All files which are affected, map of old => new file identifiers |
||
448 | * @throws FolderDoesNotExistException |
||
449 | */ |
||
450 | public function moveFolderWithinStorage($sourceFolderIdentifier, $targetFolderIdentifier, $newFolderName) |
||
451 | { |
||
452 | $trimmedSourceIdentifier = ltrim($sourceFolderIdentifier, '/'); |
||
453 | $trimmedTargetFolderIdentifier = ltrim($targetFolderIdentifier, '/'); |
||
454 | |||
472 | |||
473 | /** |
||
474 | * Folder equivalent to copyFileWithinStorage(). |
||
475 | * |
||
476 | * @param string $sourceFolderIdentifier |
||
477 | * @param string $targetFolderIdentifier |
||
478 | * @param string $newFolderName |
||
479 | * @return bool |
||
480 | */ |
||
481 | public function copyFolderWithinStorage($sourceFolderIdentifier, $targetFolderIdentifier, $newFolderName) |
||
492 | |||
493 | /** |
||
494 | * Returns the contents of a file. Beware that this requires to load the |
||
495 | * complete file into memory and also may require fetching the file from an |
||
496 | * external location. So this might be an expensive operation (both in terms |
||
497 | * of processing resources and money) for large files. |
||
498 | * |
||
499 | * @param string $fileIdentifier |
||
500 | * @return string The file contents |
||
501 | */ |
||
502 | 3 | public function getFileContents($fileIdentifier) |
|
506 | |||
507 | /** |
||
508 | * Sets the contents of a file to the specified value. |
||
509 | * |
||
510 | * @param string $fileIdentifier |
||
511 | * @param string $contents |
||
512 | * @return int The number of bytes written to the file |
||
513 | */ |
||
514 | 3 | public function setFileContents($fileIdentifier, $contents) |
|
520 | |||
521 | /** |
||
522 | * Checks if a file inside a folder exists |
||
523 | * |
||
524 | * @param string $fileName |
||
525 | * @param string $folderIdentifier |
||
526 | * @return bool |
||
527 | */ |
||
528 | 3 | public function fileExistsInFolder($fileName, $folderIdentifier) |
|
534 | |||
535 | /** |
||
536 | * Checks if a folder inside a folder exists. |
||
537 | * |
||
538 | * @param string $folderName |
||
539 | * @param string $folderIdentifier |
||
540 | * @return bool |
||
541 | */ |
||
542 | 3 | public function folderExistsInFolder($folderName, $folderIdentifier) |
|
548 | |||
549 | /** |
||
550 | * Returns a path to a local copy of a file for processing it. When changing the |
||
551 | * file, you have to take care of replacing the current version yourself! |
||
552 | * |
||
553 | * @param string $fileIdentifier |
||
554 | * @param bool $writable Set this to FALSE if you only need the file for read |
||
555 | * operations. This might speed up things, e.g. by using |
||
556 | * a cached local version. Never modify the file if you |
||
557 | * have set this flag! |
||
558 | * @return string The path to the file on the local disk |
||
559 | */ |
||
560 | public function getFileForLocalProcessing($fileIdentifier, $writable = true) |
||
564 | |||
565 | /** |
||
566 | * Returns the permissions of a file/folder as an array |
||
567 | * (keys r, w) of boolean flags |
||
568 | * |
||
569 | * @param string $identifier |
||
570 | * @return array |
||
571 | */ |
||
572 | public function getPermissions($identifier) |
||
579 | |||
580 | /** |
||
581 | * Directly output the contents of the file to the output |
||
582 | * buffer. Should not take care of header files or flushing |
||
583 | * buffer before. Will be taken care of by the Storage. |
||
584 | * |
||
585 | * @param string $identifier |
||
586 | * @return void |
||
587 | */ |
||
588 | public function dumpFileContents($identifier) |
||
595 | |||
596 | /** |
||
597 | * Checks if a given identifier is within a container, e.g. if |
||
598 | * a file or folder is within another folder. |
||
599 | * This can e.g. be used to check for web-mounts. |
||
600 | * |
||
601 | * Hint: this also needs to return TRUE if the given identifier |
||
602 | * matches the container identifier to allow access to the root |
||
603 | * folder of a filemount. |
||
604 | * |
||
605 | * @param string $folderIdentifier |
||
606 | * @param string $identifier identifier to be checked against $folderIdentifier |
||
607 | * @return bool TRUE if $content is within or matches $folderIdentifier |
||
608 | */ |
||
609 | 3 | public function isWithin($folderIdentifier, $identifier) |
|
626 | |||
627 | /** |
||
628 | * Returns information about a file. |
||
629 | * |
||
630 | * @param string $fileIdentifier |
||
631 | * @param array $propertiesToExtract Array of properties which are be extracted |
||
632 | * If empty all will be extracted |
||
633 | * @return array |
||
634 | * @throws FileDoesNotExistException |
||
635 | */ |
||
636 | 12 | public function getFileInfoByIdentifier($fileIdentifier, array $propertiesToExtract = []) |
|
646 | |||
647 | /** |
||
648 | * Returns information about a file. |
||
649 | * |
||
650 | * @param string $folderIdentifier |
||
651 | * @return array |
||
652 | * @throws FolderDoesNotExistException |
||
653 | */ |
||
654 | 6 | public function getFolderInfoByIdentifier($folderIdentifier) |
|
670 | |||
671 | /** |
||
672 | * Returns the identifier of a file inside the folder |
||
673 | * |
||
674 | * @param string $fileName |
||
675 | * @param string $folderIdentifier |
||
676 | * @return string file identifier |
||
677 | */ |
||
678 | 3 | public function getFileInFolder($fileName, $folderIdentifier) |
|
682 | |||
683 | /** |
||
684 | * Returns a list of files inside the specified path |
||
685 | * |
||
686 | * @param string $folderIdentifier |
||
687 | * @param int $start |
||
688 | * @param int $numberOfItems |
||
689 | * @param bool $recursive |
||
690 | * @param array $filenameFilterCallbacks callbacks for filtering the items |
||
691 | * @param string $sort Property name used to sort the items. |
||
692 | * Among them may be: '' (empty, no sorting), name, |
||
693 | * fileext, size, tstamp and rw. |
||
694 | * If a driver does not support the given property, it |
||
695 | * should fall back to "name". |
||
696 | * @param bool $sortRev TRUE to indicate reverse sorting (last to first) |
||
697 | * @return array of FileIdentifiers |
||
698 | */ |
||
699 | 3 | public function getFilesInFolder( |
|
723 | |||
724 | /** |
||
725 | * Returns the identifier of a folder inside the folder |
||
726 | * |
||
727 | * @param string $folderName The name of the target folder |
||
728 | * @param string $folderIdentifier |
||
729 | * @return string folder identifier |
||
730 | */ |
||
731 | 3 | public function getFolderInFolder($folderName, $folderIdentifier) |
|
736 | |||
737 | /** |
||
738 | * Returns a list of folders inside the specified path |
||
739 | * |
||
740 | * @param string $folderIdentifier |
||
741 | * @param int $start |
||
742 | * @param int $numberOfItems |
||
743 | * @param bool $recursive |
||
744 | * @param array $folderNameFilterCallbacks callbacks for filtering the items |
||
745 | * @param string $sort Property name used to sort the items. |
||
746 | * Among them may be: '' (empty, no sorting), name, |
||
747 | * fileext, size, tstamp and rw. |
||
748 | * If a driver does not support the given property, it |
||
749 | * should fall back to "name". |
||
750 | * @param bool $sortRev TRUE to indicate reverse sorting (last to first) |
||
751 | * @return array of Folder Identifier |
||
752 | * @TODO: Implement pagination with $start and $numberOfItems |
||
753 | * @TODO: Implement directory filter callbacks |
||
754 | * @TODO: Implement sorting |
||
755 | */ |
||
756 | 3 | public function getFoldersInFolder( |
|
781 | |||
782 | /** |
||
783 | * Returns the number of files inside the specified path |
||
784 | * |
||
785 | * @param string $folderIdentifier |
||
786 | * @param bool $recursive |
||
787 | * @param array $filenameFilterCallbacks callbacks for filtering the items |
||
788 | * @return int Number of files in folder |
||
789 | * @TODO: Implement recursive count |
||
790 | * @TODO: Implement filename filtering |
||
791 | */ |
||
792 | public function countFilesInFolder($folderIdentifier, $recursive = false, array $filenameFilterCallbacks = []) |
||
797 | |||
798 | /** |
||
799 | * Returns the number of folders inside the specified path |
||
800 | * |
||
801 | * @param string $folderIdentifier |
||
802 | * @param bool $recursive |
||
803 | * @param array $folderNameFilterCallbacks callbacks for filtering the items |
||
804 | * @return int Number of folders in folder |
||
805 | */ |
||
806 | 3 | public function countFoldersInFolder($folderIdentifier, $recursive = false, array $folderNameFilterCallbacks = []) |
|
819 | |||
820 | /** |
||
821 | * Extracts information about a file from the filesystem. |
||
822 | * |
||
823 | * @param string $filePath The absolute path to the file |
||
824 | * @param string $containerPath The relative path to the file's container |
||
825 | * @param array $propertiesToExtract array of properties which should be returned, if empty all will be extracted |
||
826 | * @return array |
||
827 | */ |
||
828 | 9 | protected function extractFileInformation($filePath, $containerPath, array $propertiesToExtract = array()) |
|
851 | |||
852 | /** |
||
853 | * Extracts a specific FileInformation from the FileSystems. |
||
854 | * |
||
855 | * @param string $fileIdentifier |
||
856 | * @param string $containerPath |
||
857 | * @param string $property |
||
858 | * |
||
859 | * @return bool|int|string |
||
860 | * @throws \InvalidArgumentException |
||
861 | */ |
||
862 | 9 | public function getSpecificFileInformation($fileIdentifier, $containerPath, $property) |
|
901 | |||
902 | /** |
||
903 | * Copies a file to a temporary path and returns that path. |
||
904 | * |
||
905 | * @param string $fileIdentifier |
||
906 | * @return string The temporary path |
||
907 | * @throws \RuntimeException |
||
908 | */ |
||
909 | protected function copyFileToTemporaryPath($fileIdentifier) |
||
926 | |||
927 | /** |
||
928 | * @param string $trimmedSourcePath |
||
929 | * @param string $trimmedTargetPath |
||
930 | * @return bool |
||
931 | */ |
||
932 | protected function copyFolderRecursively($trimmedSourcePath, $trimmedTargetPath) |
||
954 | } |
||
955 |