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 |
||
20 | abstract class FlysystemDriver extends AbstractHierarchicalFilesystemDriver |
||
21 | { |
||
22 | /** |
||
23 | * @var FilesystemInterface |
||
24 | */ |
||
25 | protected $filesystem; |
||
26 | |||
27 | /** |
||
28 | * @var AdapterInterface |
||
29 | */ |
||
30 | protected $adapter; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $entryPath; |
||
36 | |||
37 | /** |
||
38 | * FlysystemDriver constructor. |
||
39 | * @param array $configuration |
||
40 | */ |
||
41 | 48 | public function __construct(array $configuration = []) |
|
50 | |||
51 | /** |
||
52 | * Processes the configuration for this driver. |
||
53 | * @return void |
||
54 | */ |
||
55 | public function processConfiguration() |
||
59 | |||
60 | /** |
||
61 | * Merges the capabilities merged by the user at the storage |
||
62 | * configuration into the actual capabilities of the driver |
||
63 | * and returns the result. |
||
64 | * |
||
65 | * @param int $capabilities |
||
66 | * @return int |
||
67 | */ |
||
68 | public function mergeConfigurationCapabilities($capabilities) |
||
72 | |||
73 | /** |
||
74 | * Returns the identifier of the root level folder of the storage. |
||
75 | * |
||
76 | * @return string |
||
77 | */ |
||
78 | public function getRootLevelFolder() |
||
82 | |||
83 | /** |
||
84 | * Returns the identifier of the default folder new files should be put into. |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | 3 | public function getDefaultFolder() |
|
97 | |||
98 | /** |
||
99 | * Checks if a folder exists. |
||
100 | * |
||
101 | * @param string $folderIdentifier |
||
102 | * @return bool |
||
103 | */ |
||
104 | 6 | public function folderExists($folderIdentifier) |
|
105 | { |
||
106 | 6 | if ('/' === $folderIdentifier) { |
|
107 | return true; |
||
108 | } else { |
||
109 | 6 | return ($this->filesystem->has('/' . $folderIdentifier) && $this->filesystem->get('/' . $folderIdentifier)->isDir()); |
|
110 | } |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Creates a folder, within a parent folder. |
||
115 | * If no parent folder is given, a root level folder will be created |
||
116 | * |
||
117 | * @param string $newFolderName |
||
118 | * @param string $parentFolderIdentifier |
||
119 | * @param bool $recursive |
||
120 | * @return string the Identifier of the new folder |
||
121 | */ |
||
122 | 9 | public function createFolder($newFolderName, $parentFolderIdentifier = '', $recursive = false) |
|
138 | |||
139 | /** |
||
140 | * Returns the public URL to a file. |
||
141 | * Either fully qualified URL or relative to PATH_site (rawurlencoded). |
||
142 | * |
||
143 | * @param string $identifier |
||
144 | * @return string |
||
145 | */ |
||
146 | public function getPublicUrl($identifier) |
||
147 | { |
||
148 | return '/'; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Renames a folder in this storage. |
||
153 | * |
||
154 | * @param string $folderIdentifier |
||
155 | * @param string $newName |
||
156 | * @return array A map of old to new file identifiers of all affected resources |
||
157 | */ |
||
158 | 3 | public function renameFolder($folderIdentifier, $newName) |
|
159 | { |
||
160 | 3 | $renameResult = $this->filesystem->rename($folderIdentifier, $newName); |
|
161 | |||
162 | 3 | if (true === $renameResult) { |
|
163 | 3 | return [$folderIdentifier => $newName]; |
|
164 | } else { |
||
165 | return [$folderIdentifier => $folderIdentifier]; |
||
166 | } |
||
167 | } |
||
168 | |||
169 | /** |
||
170 | * Removes a folder in filesystem. |
||
171 | * |
||
172 | * @param string $folderIdentifier |
||
173 | * @param bool $deleteRecursively |
||
174 | * @return bool |
||
175 | */ |
||
176 | public function deleteFolder($folderIdentifier, $deleteRecursively = false) |
||
177 | { |
||
178 | return $this->filesystem->deleteDir($folderIdentifier); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * Checks if a file exists. |
||
183 | * |
||
184 | * @param string $fileIdentifier |
||
185 | * @return bool |
||
186 | */ |
||
187 | 9 | public function fileExists($fileIdentifier) |
|
188 | { |
||
189 | 9 | if ($this->filesystem->has($fileIdentifier) && !$this->filesystem->get($fileIdentifier)->isDir()) { |
|
190 | 9 | return true; |
|
191 | } |
||
192 | 6 | return false; |
|
193 | } |
||
194 | |||
195 | /** |
||
196 | * Checks if a folder contains files and (if supported) other folders. |
||
197 | * |
||
198 | * @param string $folderIdentifier |
||
199 | * @return bool TRUE if there are no files and folders within $folder |
||
200 | */ |
||
201 | 3 | public function isFolderEmpty($folderIdentifier) |
|
205 | |||
206 | /** |
||
207 | * Adds a file from the local server hard disk to a given path in TYPO3s |
||
208 | * virtual file system. This assumes that the local file exists, so no |
||
209 | * further check is done here! After a successful the original file must |
||
210 | * not exist anymore. |
||
211 | * |
||
212 | * @param string $localFilePath (within PATH_site) |
||
213 | * @param string $targetFolderIdentifier |
||
214 | * @param string $newFileName optional, if not given original name is used |
||
215 | * @param bool $removeOriginal if set the original file will be removed |
||
216 | * after successful operation |
||
217 | * @return string the identifier of the new file |
||
218 | */ |
||
219 | 3 | public function addFile($localFilePath, $targetFolderIdentifier, $newFileName = '', $removeOriginal = true) |
|
245 | |||
246 | /** |
||
247 | * Creates a new (empty) file and returns the identifier. |
||
248 | * |
||
249 | * @param string $fileName |
||
250 | * @param string $parentFolderIdentifier |
||
251 | * @return string |
||
252 | */ |
||
253 | public function createFile($fileName, $parentFolderIdentifier) |
||
261 | |||
262 | /** |
||
263 | * Copies a file *within* the current storage. |
||
264 | * Note that this is only about an inner storage copy action, |
||
265 | * where a file is just copied to another folder in the same storage. |
||
266 | * |
||
267 | * @param string $fileIdentifier |
||
268 | * @param string $targetFolderIdentifier |
||
269 | * @param string $fileName |
||
270 | * @return string the Identifier of the new file |
||
271 | */ |
||
272 | public function copyFileWithinStorage($fileIdentifier, $targetFolderIdentifier, $fileName) |
||
281 | |||
282 | /** |
||
283 | * Renames a file in this storage. |
||
284 | * |
||
285 | * @param string $fileIdentifier |
||
286 | * @param string $newName The target path (including the file name!) |
||
287 | * @return string The identifier of the file after renaming |
||
288 | */ |
||
289 | public function renameFile($fileIdentifier, $newName) |
||
297 | |||
298 | /** |
||
299 | * Replaces a file with file in local file system. |
||
300 | * |
||
301 | * @param string $fileIdentifier |
||
302 | * @param string $localFilePath |
||
303 | * @return bool TRUE if the operation succeeded |
||
304 | */ |
||
305 | public function replaceFile($fileIdentifier, $localFilePath) |
||
313 | |||
314 | /** |
||
315 | * Removes a file from the filesystem. This does not check if the file is |
||
316 | * still used or if it is a bad idea to delete it for some other reason |
||
317 | * this has to be taken care of in the upper layers (e.g. the Storage)! |
||
318 | * |
||
319 | * @param string $fileIdentifier |
||
320 | * @return bool TRUE if deleting the file succeeded |
||
321 | */ |
||
322 | 3 | public function deleteFile($fileIdentifier) |
|
323 | { |
||
324 | 3 | return $this->filesystem->delete($fileIdentifier); |
|
325 | } |
||
326 | |||
327 | /** |
||
328 | * Creates a hash for a file. |
||
329 | * |
||
330 | * @param string $fileIdentifier |
||
331 | * @param string $hashAlgorithm The hash algorithm to use |
||
332 | * @return string |
||
333 | */ |
||
334 | public function hash($fileIdentifier, $hashAlgorithm) |
||
338 | |||
339 | /** |
||
340 | * Moves a file *within* the current storage. |
||
341 | * Note that this is only about an inner-storage move action, |
||
342 | * where a file is just moved to another folder in the same storage. |
||
343 | * |
||
344 | * @param string $fileIdentifier |
||
345 | * @param string $targetFolderIdentifier |
||
346 | * @param string $newFileName |
||
347 | * @return string |
||
348 | */ |
||
349 | public function moveFileWithinStorage($fileIdentifier, $targetFolderIdentifier, $newFileName) |
||
353 | |||
354 | /** |
||
355 | * Folder equivalent to moveFileWithinStorage(). |
||
356 | * |
||
357 | * @param string $sourceFolderIdentifier |
||
358 | * @param string $targetFolderIdentifier |
||
359 | * @param string $newFolderName |
||
360 | * @return array All files which are affected, map of old => new file identifiers |
||
361 | */ |
||
362 | public function moveFolderWithinStorage($sourceFolderIdentifier, $targetFolderIdentifier, $newFolderName) |
||
366 | |||
367 | /** |
||
368 | * Folder equivalent to copyFileWithinStorage(). |
||
369 | * |
||
370 | * @param string $sourceFolderIdentifier |
||
371 | * @param string $targetFolderIdentifier |
||
372 | * @param string $newFolderName |
||
373 | * @return bool |
||
374 | */ |
||
375 | public function copyFolderWithinStorage($sourceFolderIdentifier, $targetFolderIdentifier, $newFolderName) |
||
379 | |||
380 | /** |
||
381 | * Returns the contents of a file. Beware that this requires to load the |
||
382 | * complete file into memory and also may require fetching the file from an |
||
383 | * external location. So this might be an expensive operation (both in terms |
||
384 | * of processing resources and money) for large files. |
||
385 | * |
||
386 | * @param string $fileIdentifier |
||
387 | * @return string The file contents |
||
388 | */ |
||
389 | 3 | public function getFileContents($fileIdentifier) |
|
393 | |||
394 | /** |
||
395 | * Sets the contents of a file to the specified value. |
||
396 | * |
||
397 | * @param string $fileIdentifier |
||
398 | * @param string $contents |
||
399 | * @return int The number of bytes written to the file |
||
400 | */ |
||
401 | 3 | public function setFileContents($fileIdentifier, $contents) |
|
407 | |||
408 | /** |
||
409 | * Checks if a file inside a folder exists |
||
410 | * |
||
411 | * @param string $fileName |
||
412 | * @param string $folderIdentifier |
||
413 | * @return bool |
||
414 | */ |
||
415 | 3 | public function fileExistsInFolder($fileName, $folderIdentifier) |
|
421 | |||
422 | /** |
||
423 | * Checks if a folder inside a folder exists. |
||
424 | * |
||
425 | * @param string $folderName |
||
426 | * @param string $folderIdentifier |
||
427 | * @return bool |
||
428 | */ |
||
429 | 3 | public function folderExistsInFolder($folderName, $folderIdentifier) |
|
435 | |||
436 | /** |
||
437 | * Returns a path to a local copy of a file for processing it. When changing the |
||
438 | * file, you have to take care of replacing the current version yourself! |
||
439 | * |
||
440 | * @param string $fileIdentifier |
||
441 | * @param bool $writable Set this to FALSE if you only need the file for read |
||
442 | * operations. This might speed up things, e.g. by using |
||
443 | * a cached local version. Never modify the file if you |
||
444 | * have set this flag! |
||
445 | * @return string The path to the file on the local disk |
||
446 | */ |
||
447 | public function getFileForLocalProcessing($fileIdentifier, $writable = true) |
||
455 | |||
456 | /** |
||
457 | * Returns the permissions of a file/folder as an array |
||
458 | * (keys r, w) of boolean flags |
||
459 | * |
||
460 | * @param string $identifier |
||
461 | * @return array |
||
462 | */ |
||
463 | public function getPermissions($identifier) |
||
470 | |||
471 | /** |
||
472 | * Directly output the contents of the file to the output |
||
473 | * buffer. Should not take care of header files or flushing |
||
474 | * buffer before. Will be taken care of by the Storage. |
||
475 | * |
||
476 | * @param string $identifier |
||
477 | * @return void |
||
478 | */ |
||
479 | public function dumpFileContents($identifier) |
||
486 | |||
487 | /** |
||
488 | * Checks if a given identifier is within a container, e.g. if |
||
489 | * a file or folder is within another folder. |
||
490 | * This can e.g. be used to check for web-mounts. |
||
491 | * |
||
492 | * Hint: this also needs to return TRUE if the given identifier |
||
493 | * matches the container identifier to allow access to the root |
||
494 | * folder of a filemount. |
||
495 | * |
||
496 | * @param string $folderIdentifier |
||
497 | * @param string $identifier identifier to be checked against $folderIdentifier |
||
498 | * @return bool TRUE if $content is within or matches $folderIdentifier |
||
499 | */ |
||
500 | public function isWithin($folderIdentifier, $identifier) |
||
514 | |||
515 | /** |
||
516 | * Returns information about a file. |
||
517 | * |
||
518 | * @param string $fileIdentifier |
||
519 | * @param array $propertiesToExtract Array of properties which are be extracted |
||
520 | * If empty all will be extracted |
||
521 | * @return array |
||
522 | */ |
||
523 | public function getFileInfoByIdentifier($fileIdentifier, array $propertiesToExtract = []) |
||
533 | |||
534 | /** |
||
535 | * Returns information about a file. |
||
536 | * |
||
537 | * @param string $folderIdentifier |
||
538 | * @return array |
||
539 | */ |
||
540 | 3 | public function getFolderInfoByIdentifier($folderIdentifier) |
|
550 | |||
551 | /** |
||
552 | * Returns the identifier of a file inside the folder |
||
553 | * |
||
554 | * @param string $fileName |
||
555 | * @param string $folderIdentifier |
||
556 | * @return string file identifier |
||
557 | */ |
||
558 | public function getFileInFolder($fileName, $folderIdentifier) |
||
562 | |||
563 | /** |
||
564 | * Returns a list of files inside the specified path |
||
565 | * |
||
566 | * @param string $folderIdentifier |
||
567 | * @param int $start |
||
568 | * @param int $numberOfItems |
||
569 | * @param bool $recursive |
||
570 | * @param array $filenameFilterCallbacks callbacks for filtering the items |
||
571 | * @param string $sort Property name used to sort the items. |
||
572 | * Among them may be: '' (empty, no sorting), name, |
||
573 | * fileext, size, tstamp and rw. |
||
574 | * If a driver does not support the given property, it |
||
575 | * should fall back to "name". |
||
576 | * @param bool $sortRev TRUE to indicate reverse sorting (last to first) |
||
577 | * @return array of FileIdentifiers |
||
578 | */ |
||
579 | 3 | public function getFilesInFolder( |
|
603 | |||
604 | /** |
||
605 | * Returns the identifier of a folder inside the folder |
||
606 | * |
||
607 | * @param string $folderName The name of the target folder |
||
608 | * @param string $folderIdentifier |
||
609 | * @return string folder identifier |
||
610 | */ |
||
611 | 3 | public function getFolderInFolder($folderName, $folderIdentifier) |
|
612 | { |
||
613 | 3 | $folderIdentifier = $this->canonicalizeAndCheckFolderIdentifier($folderIdentifier . '/' . $folderName); |
|
614 | 3 | return $folderIdentifier; |
|
615 | } |
||
616 | |||
617 | /** |
||
618 | * Returns a list of folders inside the specified path |
||
619 | * |
||
620 | * @param string $folderIdentifier |
||
621 | * @param int $start |
||
622 | * @param int $numberOfItems |
||
623 | * @param bool $recursive |
||
624 | * @param array $folderNameFilterCallbacks callbacks for filtering the items |
||
625 | * @param string $sort Property name used to sort the items. |
||
626 | * Among them may be: '' (empty, no sorting), name, |
||
627 | * fileext, size, tstamp and rw. |
||
628 | * If a driver does not support the given property, it |
||
629 | * should fall back to "name". |
||
630 | * @param bool $sortRev TRUE to indicate reverse sorting (last to first) |
||
631 | * @return array of Folder Identifier |
||
632 | * @TODO: Implement pagination with $start and $numberOfItems |
||
633 | * @TODO: Implement directory filter callbacks |
||
634 | * @TODO: Implement sorting |
||
635 | */ |
||
636 | 3 | public function getFoldersInFolder( |
|
661 | |||
662 | /** |
||
663 | * Returns the number of files inside the specified path |
||
664 | * |
||
665 | * @param string $folderIdentifier |
||
666 | * @param bool $recursive |
||
667 | * @param array $filenameFilterCallbacks callbacks for filtering the items |
||
668 | * @return int Number of files in folder |
||
669 | * @TODO: Implement recursive count |
||
670 | * @TODO: Implement filename filtering |
||
671 | */ |
||
672 | public function countFilesInFolder($folderIdentifier, $recursive = false, array $filenameFilterCallbacks = []) |
||
677 | |||
678 | /** |
||
679 | * Returns the number of folders inside the specified path |
||
680 | * |
||
681 | * @param string $folderIdentifier |
||
682 | * @param bool $recursive |
||
683 | * @param array $folderNameFilterCallbacks callbacks for filtering the items |
||
684 | * @return int Number of folders in folder |
||
685 | */ |
||
686 | 3 | public function countFoldersInFolder($folderIdentifier, $recursive = false, array $folderNameFilterCallbacks = []) |
|
699 | |||
700 | /** |
||
701 | * Extracts information about a file from the filesystem. |
||
702 | * |
||
703 | * @param string $filePath The absolute path to the file |
||
704 | * @param string $containerPath The relative path to the file's container |
||
705 | * @param array $propertiesToExtract array of properties which should be returned, if empty all will be extracted |
||
706 | * @return array |
||
707 | */ |
||
708 | protected function extractFileInformation($filePath, $containerPath, array $propertiesToExtract = array()) |
||
731 | |||
732 | /** |
||
733 | * Extracts a specific FileInformation from the FileSystems. |
||
734 | * |
||
735 | * @param string $fileIdentifier |
||
736 | * @param string $containerPath |
||
737 | * @param string $property |
||
738 | * |
||
739 | * @return bool|int|string |
||
740 | * @throws \InvalidArgumentException |
||
741 | */ |
||
742 | public function getSpecificFileInformation($fileIdentifier, $containerPath, $property) |
||
772 | } |
||
773 |