Complex classes like FlysystemAssetStore 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 FlysystemAssetStore, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class FlysystemAssetStore implements AssetStore, AssetStoreRouter, Flushable { |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Session key to use for user grants |
||
| 32 | */ |
||
| 33 | const GRANTS_SESSION = 'AssetStore_Grants'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var Filesystem |
||
| 37 | */ |
||
| 38 | private $publicFilesystem = null; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Filesystem to use for protected files |
||
| 42 | * |
||
| 43 | * @var Filesystem |
||
| 44 | */ |
||
| 45 | private $protectedFilesystem = null; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Enable to use legacy filename behaviour (omits hash) |
||
| 49 | * |
||
| 50 | * Note that if using legacy filenames then duplicate files will not work. |
||
| 51 | * |
||
| 52 | * @config |
||
| 53 | * @var bool |
||
| 54 | */ |
||
| 55 | private static $legacy_filenames = false; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Flag if empty folders are allowed. |
||
| 59 | * If false, empty folders are cleared up when their contents are deleted. |
||
| 60 | * |
||
| 61 | * @config |
||
| 62 | * @var bool |
||
| 63 | */ |
||
| 64 | private static $keep_empty_dirs = false; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Set HTTP error code for requests to secure denied assets. |
||
| 68 | * Note that this defaults to 404 to prevent information disclosure |
||
| 69 | * of secure files |
||
| 70 | * |
||
| 71 | * @config |
||
| 72 | * @var int |
||
| 73 | */ |
||
| 74 | private static $denied_response_code = 404; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Set HTTP error code to use for missing secure assets |
||
| 78 | * |
||
| 79 | * @config |
||
| 80 | * @var int |
||
| 81 | */ |
||
| 82 | private static $missing_response_code = 404; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Custom headers to add to all custom file responses |
||
| 86 | * |
||
| 87 | * @config |
||
| 88 | * @var array |
||
| 89 | */ |
||
| 90 | private static $file_response_headers = array( |
||
| 91 | 'Cache-Control' => 'private' |
||
| 92 | ); |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Assign new flysystem backend |
||
| 96 | * |
||
| 97 | * @param Filesystem $filesystem |
||
| 98 | * @return $this |
||
| 99 | */ |
||
| 100 | public function setPublicFilesystem(Filesystem $filesystem) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Get the currently assigned flysystem backend |
||
| 110 | * |
||
| 111 | * @return Filesystem |
||
| 112 | * @throws LogicException |
||
| 113 | */ |
||
| 114 | public function getPublicFilesystem() { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Assign filesystem to use for non-public files |
||
| 123 | * |
||
| 124 | * @param Filesystem $filesystem |
||
| 125 | * @return $this |
||
| 126 | */ |
||
| 127 | public function setProtectedFilesystem(Filesystem $filesystem) { |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Get filesystem to use for non-public files |
||
| 137 | * |
||
| 138 | * @return Filesystem |
||
| 139 | */ |
||
| 140 | public function getProtectedFilesystem() { |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Return the store that contains the given fileID |
||
| 149 | * |
||
| 150 | * @param string $fileID Internal file identifier |
||
| 151 | * @return Filesystem |
||
| 152 | */ |
||
| 153 | protected function getFilesystemFor($fileID) { |
||
| 164 | |||
| 165 | public function getCapabilities() { |
||
| 179 | |||
| 180 | public function getVisibility($filename, $hash) { |
||
| 192 | |||
| 193 | |||
| 194 | public function getAsStream($filename, $hash, $variant = null) { |
||
| 200 | |||
| 201 | public function getAsString($filename, $hash, $variant = null) { |
||
| 207 | |||
| 208 | public function getAsURL($filename, $hash, $variant = null, $grant = true) { |
||
| 227 | |||
| 228 | public function setFromLocalFile($path, $filename = null, $hash = null, $variant = null, $config = array()) { |
||
| 259 | |||
| 260 | public function setFromString($data, $filename, $hash = null, $variant = null, $config = array()) { |
||
| 274 | |||
| 275 | public function setFromStream($stream, $filename, $hash = null, $variant = null, $config = array()) { |
||
| 297 | |||
| 298 | public function delete($filename, $hash) { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Delete the given file (and any variants) in the given {@see Filesystem} |
||
| 307 | * |
||
| 308 | * @param string $fileID |
||
| 309 | * @param Filesystem $filesystem |
||
| 310 | * @return bool True if a file was deleted |
||
| 311 | */ |
||
| 312 | protected function deleteFromFilesystem($fileID, Filesystem $filesystem) { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Clear directory if it's empty |
||
| 327 | * |
||
| 328 | * @param string $dirname Name of directory |
||
| 329 | * @param Filesystem $filesystem |
||
| 330 | */ |
||
| 331 | protected function truncateDirectory($dirname, Filesystem $filesystem) { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Returns an iterable {@see Generator} of all files / variants for the given $fileID in the given $filesystem |
||
| 343 | * This includes the empty (no) variant. |
||
| 344 | * |
||
| 345 | * @param string $fileID ID of original file to compare with. |
||
| 346 | * @param Filesystem $filesystem |
||
| 347 | * @return Generator |
||
| 348 | */ |
||
| 349 | protected function findVariants($fileID, Filesystem $filesystem) { |
||
| 362 | |||
| 363 | public function publish($filename, $hash) { |
||
| 369 | |||
| 370 | public function protect($filename, $hash) { |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Move a file (and its associative variants) between filesystems |
||
| 379 | * |
||
| 380 | * @param string $fileID |
||
| 381 | * @param Filesystem $from |
||
| 382 | * @param Filesystem $to |
||
| 383 | */ |
||
| 384 | protected function moveBetweenFilesystems($fileID, Filesystem $from, Filesystem $to) { |
||
| 396 | |||
| 397 | public function grant($filename, $hash) { |
||
| 403 | |||
| 404 | public function revoke($filename, $hash) { |
||
| 414 | |||
| 415 | public function canView($filename, $hash) { |
||
| 422 | |||
| 423 | /** |
||
| 424 | * Determine if a grant exists for the given FileID |
||
| 425 | * |
||
| 426 | * @param string $fileID |
||
| 427 | * @return bool |
||
| 428 | */ |
||
| 429 | protected function isGranted($fileID) { |
||
| 436 | |||
| 437 | /** |
||
| 438 | * get sha1 hash from stream |
||
| 439 | * |
||
| 440 | * @param resource $stream |
||
| 441 | * @return string str1 hash |
||
| 442 | */ |
||
| 443 | protected function getStreamSHA1($stream) { |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Get stream as a file |
||
| 452 | * |
||
| 453 | * @param resource $stream |
||
| 454 | * @return string Filename of resulting stream content |
||
| 455 | * @throws Exception |
||
| 456 | */ |
||
| 457 | protected function getStreamAsFile($stream) { |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Determine if this stream is seekable |
||
| 477 | * |
||
| 478 | * @param resource $stream |
||
| 479 | * @return bool True if this stream is seekable |
||
| 480 | */ |
||
| 481 | protected function isSeekableStream($stream) { |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Invokes the conflict resolution scheme on the given content, and invokes a callback if |
||
| 487 | * the storage request is approved. |
||
| 488 | * |
||
| 489 | * @param callable $callback Will be invoked and passed a fileID if the file should be stored |
||
| 490 | * @param string $filename Name for the resulting file |
||
| 491 | * @param string $hash SHA1 of the original file content |
||
| 492 | * @param string $variant Variant to write |
||
| 493 | * @param array $config Write options. {@see AssetStore} |
||
| 494 | * @return array Tuple associative array (Filename, Hash, Variant) |
||
| 495 | * @throws Exception |
||
| 496 | */ |
||
| 497 | protected function writeWithCallback($callback, $filename, $hash, $variant = null, $config = array()) { |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Choose a default conflict resolution |
||
| 564 | * |
||
| 565 | * @param string $variant |
||
| 566 | * @return string |
||
| 567 | */ |
||
| 568 | protected function getDefaultConflictResolution($variant) { |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Determine if legacy filenames should be used. These do not have hash path parts. |
||
| 582 | * |
||
| 583 | * @return bool |
||
| 584 | */ |
||
| 585 | protected function useLegacyFilenames() { |
||
| 588 | |||
| 589 | public function getMetadata($filename, $hash, $variant = null) { |
||
| 597 | |||
| 598 | public function getMimeType($filename, $hash, $variant = null) { |
||
| 606 | |||
| 607 | public function exists($filename, $hash, $variant = null) { |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Determine the path that should be written to, given the conflict resolution scheme |
||
| 615 | * |
||
| 616 | * @param string $conflictResolution |
||
| 617 | * @param string $fileID |
||
| 618 | * @return string|false Safe filename to write to. If false, then don't write, and use existing file. |
||
| 619 | * @throws Exception |
||
| 620 | */ |
||
| 621 | protected function resolveConflicts($conflictResolution, $fileID) { |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Get an asset renamer for the given filename. |
||
| 661 | * |
||
| 662 | * @param string $fileID Adapter specific identifier for this file/version |
||
| 663 | * @return AssetNameGenerator |
||
| 664 | */ |
||
| 665 | protected function fileGeneratorFor($fileID){ |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Performs filename cleanup before sending it back. |
||
| 671 | * |
||
| 672 | * This name should not contain hash or variants. |
||
| 673 | * |
||
| 674 | * @param string $filename |
||
| 675 | * @return string |
||
| 676 | */ |
||
| 677 | protected function cleanFilename($filename) { |
||
| 681 | |||
| 682 | /** |
||
| 683 | * Given a FileID, map this back to the original filename, trimming variant and hash |
||
| 684 | * |
||
| 685 | * @param string $fileID Adapter specific identifier for this file/version |
||
| 686 | * @return string Filename for this file, omitting hash and variant |
||
| 687 | */ |
||
| 688 | protected function getOriginalFilename($fileID) { |
||
| 703 | |||
| 704 | /** |
||
| 705 | * Remove variant from a fileID |
||
| 706 | * |
||
| 707 | * @param string $fileID |
||
| 708 | * @return string FileID without variant |
||
| 709 | */ |
||
| 710 | protected function removeVariant($fileID) { |
||
| 718 | |||
| 719 | /** |
||
| 720 | * Map file tuple (hash, name, variant) to a filename to be used by flysystem |
||
| 721 | * |
||
| 722 | * The resulting file will look something like my/directory/EA775CB4D4/filename__variant.jpg |
||
| 723 | * |
||
| 724 | * @param string $filename Name of file |
||
| 725 | * @param string $hash Hash of original file |
||
| 726 | * @param string $variant (if given) |
||
| 727 | * @return string Adapter specific identifier for this file/version |
||
| 728 | */ |
||
| 729 | protected function getFileID($filename, $hash, $variant = null) { |
||
| 766 | |||
| 767 | /** |
||
| 768 | * Ensure each adapter re-generates its own server configuration files |
||
| 769 | */ |
||
| 770 | public static function flush() { |
||
| 785 | |||
| 786 | public function getResponseFor($asset) { |
||
| 806 | |||
| 807 | /** |
||
| 808 | * Generate an {@see SS_HTTPResponse} for the given file from the source filesystem |
||
| 809 | * @param FilesystemInterface $flysystem |
||
| 810 | * @param string $fileID |
||
| 811 | * @return SS_HTTPResponse |
||
| 812 | */ |
||
| 813 | protected function createResponseFor(FilesystemInterface $flysystem, $fileID) { |
||
| 828 | |||
| 829 | /** |
||
| 830 | * Generate a response for requests to a denied protected file |
||
| 831 | * |
||
| 832 | * @return SS_HTTPResponse |
||
| 833 | */ |
||
| 834 | protected function createDeniedResponse() { |
||
| 838 | |||
| 839 | /** |
||
| 840 | * Generate a response for missing file requests |
||
| 841 | * |
||
| 842 | * @return SS_HTTPResponse |
||
| 843 | */ |
||
| 844 | protected function createMissingResponse() { |
||
| 848 | |||
| 849 | /** |
||
| 850 | * Create a response with the given error code |
||
| 851 | * |
||
| 852 | * @param int $code |
||
| 853 | * @return SS_HTTPResponse |
||
| 854 | */ |
||
| 855 | protected function createErrorResponse($code) { |
||
| 865 | } |
||
| 866 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: