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 | ]; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected static $metaOptions = [ |
||
| 33 | 'CacheControl', |
||
| 34 | 'Expires', |
||
| 35 | 'StorageClass', |
||
| 36 | 'ServerSideEncryption', |
||
| 37 | 'Metadata', |
||
| 38 | 'ACL', |
||
| 39 | 'ContentType', |
||
| 40 | 'ContentEncoding', |
||
| 41 | 'ContentDisposition', |
||
| 42 | 'ContentLength', |
||
| 43 | 'Tagging', |
||
| 44 | 'WebsiteRedirectLocation', |
||
| 45 | 'SSEKMSKeyId' |
||
| 46 | ]; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var S3Client |
||
| 50 | */ |
||
| 51 | protected $s3Client; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $bucket; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | protected $options = []; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Constructor. |
||
| 65 | * |
||
| 66 | * @param S3Client $client |
||
| 67 | * @param string $bucket |
||
| 68 | * @param string $prefix |
||
| 69 | * @param array $options |
||
| 70 | */ |
||
| 71 | 72 | public function __construct(S3Client $client, $bucket, $prefix = '', array $options = []) |
|
| 78 | |||
| 79 | /** |
||
| 80 | * Get the S3Client bucket. |
||
| 81 | * |
||
| 82 | * @return string |
||
| 83 | */ |
||
| 84 | 4 | public function getBucket() |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Set the S3Client bucket. |
||
| 91 | * |
||
| 92 | * @return string |
||
| 93 | */ |
||
| 94 | 2 | public function setBucket($bucket) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Get the S3Client instance. |
||
| 101 | * |
||
| 102 | * @return S3Client |
||
| 103 | */ |
||
| 104 | 2 | public function getClient() |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Write a new file. |
||
| 111 | * |
||
| 112 | * @param string $path |
||
| 113 | * @param string $contents |
||
| 114 | * @param Config $config Config object |
||
| 115 | * |
||
| 116 | * @return false|array false on failure file meta data on success |
||
| 117 | */ |
||
| 118 | 2 | public function write($path, $contents, Config $config) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * Update a file. |
||
| 125 | * |
||
| 126 | * @param string $path |
||
| 127 | * @param string $contents |
||
| 128 | * @param Config $config Config object |
||
| 129 | * |
||
| 130 | * @return false|array false on failure file meta data on success |
||
| 131 | */ |
||
| 132 | 2 | public function update($path, $contents, Config $config) |
|
| 136 | |||
| 137 | /** |
||
| 138 | * Rename a file. |
||
| 139 | * |
||
| 140 | * @param string $path |
||
| 141 | * @param string $newpath |
||
| 142 | * |
||
| 143 | * @return bool |
||
| 144 | */ |
||
| 145 | 4 | public function rename($path, $newpath) |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Delete a file. |
||
| 156 | * |
||
| 157 | * @param string $path |
||
| 158 | * |
||
| 159 | * @return bool |
||
| 160 | */ |
||
| 161 | 4 | public function delete($path) |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Delete a directory. |
||
| 180 | * |
||
| 181 | * @param string $dirname |
||
| 182 | * |
||
| 183 | * @return bool |
||
| 184 | */ |
||
| 185 | 4 | public function deleteDir($dirname) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Create a directory. |
||
| 199 | * |
||
| 200 | * @param string $dirname directory name |
||
| 201 | * @param Config $config |
||
| 202 | * |
||
| 203 | * @return bool|array |
||
| 204 | */ |
||
| 205 | 2 | public function createDir($dirname, Config $config) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Check whether a file exists. |
||
| 212 | * |
||
| 213 | * @param string $path |
||
| 214 | * |
||
| 215 | * @return bool |
||
| 216 | */ |
||
| 217 | 12 | public function has($path) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Read a file. |
||
| 230 | * |
||
| 231 | * @param string $path |
||
| 232 | * |
||
| 233 | * @return false|array |
||
| 234 | */ |
||
| 235 | 4 | public function read($path) |
|
| 245 | |||
| 246 | /** |
||
| 247 | * List contents of a directory. |
||
| 248 | * |
||
| 249 | * @param string $directory |
||
| 250 | * @param bool $recursive |
||
| 251 | * |
||
| 252 | * @return array |
||
| 253 | */ |
||
| 254 | 2 | public function listContents($directory = '', $recursive = false) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * @param array $options |
||
| 272 | * |
||
| 273 | * @return array |
||
| 274 | */ |
||
| 275 | 2 | protected function retrievePaginatedListing(array $options) |
|
| 286 | |||
| 287 | /** |
||
| 288 | * Get all the meta data of a file or directory. |
||
| 289 | * |
||
| 290 | * @param string $path |
||
| 291 | * |
||
| 292 | * @return false|array |
||
| 293 | */ |
||
| 294 | 12 | public function getMetadata($path) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Get all the meta data of a file or directory. |
||
| 322 | * |
||
| 323 | * @param string $path |
||
| 324 | * |
||
| 325 | * @return false|array |
||
| 326 | */ |
||
| 327 | 2 | public function getSize($path) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * Get the mimetype of a file. |
||
| 334 | * |
||
| 335 | * @param string $path |
||
| 336 | * |
||
| 337 | * @return false|array |
||
| 338 | */ |
||
| 339 | 2 | public function getMimetype($path) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * Get the timestamp of a file. |
||
| 346 | * |
||
| 347 | * @param string $path |
||
| 348 | * |
||
| 349 | * @return false|array |
||
| 350 | */ |
||
| 351 | 2 | public function getTimestamp($path) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Write a new file using a stream. |
||
| 358 | * |
||
| 359 | * @param string $path |
||
| 360 | * @param resource $resource |
||
| 361 | * @param Config $config Config object |
||
| 362 | * |
||
| 363 | * @return array|false false on failure file meta data on success |
||
| 364 | */ |
||
| 365 | 2 | public function writeStream($path, $resource, Config $config) |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Update a file using a stream. |
||
| 372 | * |
||
| 373 | * @param string $path |
||
| 374 | * @param resource $resource |
||
| 375 | * @param Config $config Config object |
||
| 376 | * |
||
| 377 | * @return array|false false on failure file meta data on success |
||
| 378 | */ |
||
| 379 | 2 | public function updateStream($path, $resource, Config $config) |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Copy a file. |
||
| 386 | * |
||
| 387 | * @param string $path |
||
| 388 | * @param string $newpath |
||
| 389 | * |
||
| 390 | * @return bool |
||
| 391 | */ |
||
| 392 | 8 | public function copy($path, $newpath) |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Read a file as a stream. |
||
| 416 | * |
||
| 417 | * @param string $path |
||
| 418 | * |
||
| 419 | * @return array|false |
||
| 420 | */ |
||
| 421 | 4 | public function readStream($path) |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Read an object and normalize the response. |
||
| 435 | * |
||
| 436 | * @param $path |
||
| 437 | * |
||
| 438 | * @return array|bool |
||
| 439 | */ |
||
| 440 | 8 | protected function readObject($path) |
|
| 462 | |||
| 463 | /** |
||
| 464 | * Set the visibility for a file. |
||
| 465 | * |
||
| 466 | * @param string $path |
||
| 467 | * @param string $visibility |
||
| 468 | * |
||
| 469 | * @return array|false file meta data |
||
| 470 | */ |
||
| 471 | 6 | public function setVisibility($path, $visibility) |
|
| 490 | |||
| 491 | /** |
||
| 492 | * Get the visibility of a file. |
||
| 493 | * |
||
| 494 | * @param string $path |
||
| 495 | * |
||
| 496 | * @return array|false |
||
| 497 | */ |
||
| 498 | 4 | public function getVisibility($path) |
|
| 502 | |||
| 503 | /** |
||
| 504 | * {@inheritdoc} |
||
| 505 | */ |
||
| 506 | 64 | public function applyPathPrefix($path) |
|
| 510 | |||
| 511 | /** |
||
| 512 | * {@inheritdoc} |
||
| 513 | */ |
||
| 514 | 72 | public function setPathPrefix($prefix) |
|
| 520 | |||
| 521 | /** |
||
| 522 | * Get the object acl presented as a visibility. |
||
| 523 | * |
||
| 524 | * @param string $path |
||
| 525 | * |
||
| 526 | * @return string |
||
| 527 | */ |
||
| 528 | 12 | protected function getRawVisibility($path) |
|
| 554 | |||
| 555 | /** |
||
| 556 | * Upload an object. |
||
| 557 | * |
||
| 558 | * @param $path |
||
| 559 | * @param $body |
||
| 560 | * @param Config $config |
||
| 561 | * |
||
| 562 | * @return array |
||
| 563 | */ |
||
| 564 | 10 | protected function upload($path, $body, Config $config) |
|
| 586 | |||
| 587 | /** |
||
| 588 | * Get options from the config. |
||
| 589 | * |
||
| 590 | * @param Config $config |
||
| 591 | * |
||
| 592 | * @return array |
||
| 593 | */ |
||
| 594 | 10 | protected function getOptionsFromConfig(Config $config) |
|
| 621 | |||
| 622 | /** |
||
| 623 | * Normalize the object result array. |
||
| 624 | * |
||
| 625 | * @param array $response |
||
| 626 | * @param string $path |
||
| 627 | * |
||
| 628 | * @return array |
||
| 629 | */ |
||
| 630 | 24 | protected function normalizeResponse(array $response, $path = null) |
|
| 652 | |||
| 653 | /** |
||
| 654 | * @param $location |
||
| 655 | * |
||
| 656 | * @return bool |
||
| 657 | */ |
||
| 658 | 10 | protected function doesDirectoryExist($location) |
|
| 683 | } |
||
| 684 |