Complex classes like AwsS3Adapter 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 AwsS3Adapter, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class AwsS3Adapter extends AbstractAdapter implements CanOverwriteFiles |
||
| 16 | { |
||
| 17 | const PUBLIC_GRANT_URI = 'http://acs.amazonaws.com/groups/global/AllUsers'; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | protected static $resultMap = [ |
||
| 23 | 'Body' => 'contents', |
||
| 24 | 'ContentLength' => 'size', |
||
| 25 | 'ContentType' => 'mimetype', |
||
| 26 | 'Size' => 'size', |
||
| 27 | 'Metadata' => 'metadata', |
||
| 28 | 'StorageClass' => 'storageclass', |
||
| 29 | 'ETag' => 'etag', |
||
| 30 | 'VersionId' => 'versionid' |
||
| 31 | ]; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected static $metaOptions = [ |
||
| 37 | 'ACL', |
||
| 38 | 'CacheControl', |
||
| 39 | 'ContentDisposition', |
||
| 40 | 'ContentEncoding', |
||
| 41 | 'ContentLength', |
||
| 42 | 'ContentType', |
||
| 43 | 'Expires', |
||
| 44 | 'GrantFullControl', |
||
| 45 | 'GrantRead', |
||
| 46 | 'GrantReadACP', |
||
| 47 | 'GrantWriteACP', |
||
| 48 | 'Metadata', |
||
| 49 | 'RequestPayer', |
||
| 50 | 'SSECustomerAlgorithm', |
||
| 51 | 'SSECustomerKey', |
||
| 52 | 'SSECustomerKeyMD5', |
||
| 53 | 'SSEKMSKeyId', |
||
| 54 | 'ServerSideEncryption', |
||
| 55 | 'StorageClass', |
||
| 56 | 'Tagging', |
||
| 57 | 'WebsiteRedirectLocation', |
||
| 58 | ]; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var S3Client |
||
| 62 | */ |
||
| 63 | protected $s3Client; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | protected $bucket; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | protected $options = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Constructor. |
||
| 77 | * |
||
| 78 | * @param S3Client $client |
||
| 79 | * @param string $bucket |
||
| 80 | * @param string $prefix |
||
| 81 | * @param array $options |
||
| 82 | */ |
||
| 83 | 72 | public function __construct(S3Client $client, $bucket, $prefix = '', array $options = []) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * Get the S3Client bucket. |
||
| 93 | * |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | 4 | public function getBucket() |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Set the S3Client bucket. |
||
| 103 | * |
||
| 104 | * @return string |
||
| 105 | */ |
||
| 106 | 2 | public function setBucket($bucket) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Get the S3Client instance. |
||
| 113 | * |
||
| 114 | * @return S3Client |
||
| 115 | */ |
||
| 116 | 2 | public function getClient() |
|
| 120 | |||
| 121 | /** |
||
| 122 | * Write a new file. |
||
| 123 | * |
||
| 124 | * @param string $path |
||
| 125 | * @param string $contents |
||
| 126 | * @param Config $config Config object |
||
| 127 | * |
||
| 128 | * @return false|array false on failure file meta data on success |
||
| 129 | */ |
||
| 130 | 2 | public function write($path, $contents, Config $config) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Update a file. |
||
| 137 | * |
||
| 138 | * @param string $path |
||
| 139 | * @param string $contents |
||
| 140 | * @param Config $config Config object |
||
| 141 | * |
||
| 142 | * @return false|array false on failure file meta data on success |
||
| 143 | */ |
||
| 144 | 2 | public function update($path, $contents, Config $config) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Rename a file. |
||
| 151 | * |
||
| 152 | * @param string $path |
||
| 153 | * @param string $newpath |
||
| 154 | * |
||
| 155 | * @return bool |
||
| 156 | */ |
||
| 157 | 4 | public function rename($path, $newpath) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Delete a file. |
||
| 168 | * |
||
| 169 | * @param string $path |
||
| 170 | * |
||
| 171 | * @return bool |
||
| 172 | */ |
||
| 173 | 4 | public function delete($path) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Delete a directory. |
||
| 192 | * |
||
| 193 | * @param string $dirname |
||
| 194 | * |
||
| 195 | * @return bool |
||
| 196 | */ |
||
| 197 | 4 | public function deleteDir($dirname) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Create a directory. |
||
| 211 | * |
||
| 212 | * @param string $dirname directory name |
||
| 213 | * @param Config $config |
||
| 214 | * |
||
| 215 | * @return bool|array |
||
| 216 | */ |
||
| 217 | 2 | public function createDir($dirname, Config $config) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Check whether a file exists. |
||
| 224 | * |
||
| 225 | * @param string $path |
||
| 226 | * |
||
| 227 | * @return bool |
||
| 228 | */ |
||
| 229 | 12 | public function has($path) |
|
| 239 | |||
| 240 | /** |
||
| 241 | * Read a file. |
||
| 242 | * |
||
| 243 | * @param string $path |
||
| 244 | * |
||
| 245 | * @return false|array |
||
| 246 | */ |
||
| 247 | 4 | public function read($path) |
|
| 257 | |||
| 258 | /** |
||
| 259 | * List contents of a directory. |
||
| 260 | * |
||
| 261 | * @param string $directory |
||
| 262 | * @param bool $recursive |
||
| 263 | * |
||
| 264 | * @return array |
||
| 265 | */ |
||
| 266 | 2 | public function listContents($directory = '', $recursive = false) |
|
| 281 | |||
| 282 | /** |
||
| 283 | * @param array $options |
||
| 284 | * |
||
| 285 | * @return array |
||
| 286 | */ |
||
| 287 | 2 | protected function retrievePaginatedListing(array $options) |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Get all the meta data of a file or directory. |
||
| 301 | * |
||
| 302 | * @param string $path |
||
| 303 | * |
||
| 304 | * @return false|array |
||
| 305 | */ |
||
| 306 | 12 | public function getMetadata($path) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Get all the meta data of a file or directory. |
||
| 334 | * |
||
| 335 | * @param string $path |
||
| 336 | * |
||
| 337 | * @return false|array |
||
| 338 | */ |
||
| 339 | 2 | public function getSize($path) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Get the mimetype of a file. |
||
| 346 | * |
||
| 347 | * @param string $path |
||
| 348 | * |
||
| 349 | * @return false|array |
||
| 350 | */ |
||
| 351 | 2 | public function getMimetype($path) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Get the timestamp of a file. |
||
| 358 | * |
||
| 359 | * @param string $path |
||
| 360 | * |
||
| 361 | * @return false|array |
||
| 362 | */ |
||
| 363 | 2 | public function getTimestamp($path) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Write a new file using a stream. |
||
| 370 | * |
||
| 371 | * @param string $path |
||
| 372 | * @param resource $resource |
||
| 373 | * @param Config $config Config object |
||
| 374 | * |
||
| 375 | * @return array|false false on failure file meta data on success |
||
| 376 | */ |
||
| 377 | 2 | public function writeStream($path, $resource, Config $config) |
|
| 381 | |||
| 382 | /** |
||
| 383 | * Update a file using a stream. |
||
| 384 | * |
||
| 385 | * @param string $path |
||
| 386 | * @param resource $resource |
||
| 387 | * @param Config $config Config object |
||
| 388 | * |
||
| 389 | * @return array|false false on failure file meta data on success |
||
| 390 | */ |
||
| 391 | 2 | public function updateStream($path, $resource, Config $config) |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Copy a file. |
||
| 398 | * |
||
| 399 | * @param string $path |
||
| 400 | * @param string $newpath |
||
| 401 | * |
||
| 402 | * @return bool |
||
| 403 | */ |
||
| 404 | 8 | public function copy($path, $newpath) |
|
| 425 | |||
| 426 | /** |
||
| 427 | * Read a file as a stream. |
||
| 428 | * |
||
| 429 | * @param string $path |
||
| 430 | * |
||
| 431 | * @return array|false |
||
| 432 | */ |
||
| 433 | 4 | public function readStream($path) |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Read an object and normalize the response. |
||
| 447 | * |
||
| 448 | * @param $path |
||
| 449 | * |
||
| 450 | * @return array|bool |
||
| 451 | */ |
||
| 452 | 8 | protected function readObject($path) |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Set the visibility for a file. |
||
| 477 | * |
||
| 478 | * @param string $path |
||
| 479 | * @param string $visibility |
||
| 480 | * |
||
| 481 | * @return array|false file meta data |
||
| 482 | */ |
||
| 483 | 6 | public function setVisibility($path, $visibility) |
|
| 502 | |||
| 503 | /** |
||
| 504 | * Get the visibility of a file. |
||
| 505 | * |
||
| 506 | * @param string $path |
||
| 507 | * |
||
| 508 | * @return array|false |
||
| 509 | */ |
||
| 510 | 4 | public function getVisibility($path) |
|
| 514 | |||
| 515 | /** |
||
| 516 | * {@inheritdoc} |
||
| 517 | */ |
||
| 518 | 64 | public function applyPathPrefix($path) |
|
| 522 | |||
| 523 | /** |
||
| 524 | * {@inheritdoc} |
||
| 525 | */ |
||
| 526 | 72 | public function setPathPrefix($prefix) |
|
| 532 | |||
| 533 | /** |
||
| 534 | * Get the object acl presented as a visibility. |
||
| 535 | * |
||
| 536 | * @param string $path |
||
| 537 | * |
||
| 538 | * @return string |
||
| 539 | */ |
||
| 540 | 12 | protected function getRawVisibility($path) |
|
| 566 | |||
| 567 | /** |
||
| 568 | * Upload an object. |
||
| 569 | * |
||
| 570 | * @param $path |
||
| 571 | * @param $body |
||
| 572 | * @param Config $config |
||
| 573 | * |
||
| 574 | * @return array |
||
| 575 | */ |
||
| 576 | 10 | protected function upload($path, $body, Config $config) |
|
| 598 | |||
| 599 | /** |
||
| 600 | * Get options from the config. |
||
| 601 | * |
||
| 602 | * @param Config $config |
||
| 603 | * |
||
| 604 | * @return array |
||
| 605 | */ |
||
| 606 | 10 | protected function getOptionsFromConfig(Config $config) |
|
| 633 | |||
| 634 | /** |
||
| 635 | * Normalize the object result array. |
||
| 636 | * |
||
| 637 | * @param array $response |
||
| 638 | * @param string $path |
||
| 639 | * |
||
| 640 | * @return array |
||
| 641 | */ |
||
| 642 | 24 | protected function normalizeResponse(array $response, $path = null) |
|
| 664 | |||
| 665 | /** |
||
| 666 | * @param $location |
||
| 667 | * |
||
| 668 | * @return bool |
||
| 669 | */ |
||
| 670 | 10 | protected function doesDirectoryExist($location) |
|
| 695 | } |
||
| 696 |