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 |
||
| 14 | class AwsS3Adapter extends AbstractAdapter |
||
| 15 | { |
||
| 16 | const PUBLIC_GRANT_URI = 'http://acs.amazonaws.com/groups/global/AllUsers'; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | protected static $resultMap = [ |
||
| 22 | 'Body' => 'contents', |
||
| 23 | 'ContentLength' => 'size', |
||
| 24 | 'ContentType' => 'mimetype', |
||
| 25 | 'Size' => 'size', |
||
| 26 | ]; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected static $metaOptions = [ |
||
| 32 | 'CacheControl', |
||
| 33 | 'Expires', |
||
| 34 | 'StorageClass', |
||
| 35 | 'ServerSideEncryption', |
||
| 36 | 'Metadata', |
||
| 37 | 'ACL', |
||
| 38 | 'ContentType', |
||
| 39 | 'ContentEncoding', |
||
| 40 | 'ContentDisposition', |
||
| 41 | 'ContentLength', |
||
| 42 | ]; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var S3Client |
||
| 46 | */ |
||
| 47 | protected $s3Client; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | protected $bucket; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $options = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Constructor. |
||
| 61 | * |
||
| 62 | * @param S3Client $client |
||
| 63 | * @param string $bucket |
||
| 64 | * @param string $prefix |
||
| 65 | * @param array $options |
||
| 66 | */ |
||
| 67 | 68 | public function __construct(S3Client $client, $bucket, $prefix = '', array $options = []) |
|
| 68 | { |
||
| 69 | 68 | $this->s3Client = $client; |
|
| 70 | 68 | $this->bucket = $bucket; |
|
| 71 | 68 | $this->setPathPrefix($prefix); |
|
| 72 | 68 | $this->options = $options; |
|
| 73 | 68 | } |
|
| 74 | |||
| 75 | /** |
||
| 76 | * Get the S3Client bucket. |
||
| 77 | * |
||
| 78 | * @return string |
||
| 79 | */ |
||
| 80 | 2 | public function getBucket() |
|
| 81 | { |
||
| 82 | 2 | return $this->bucket; |
|
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Get the S3Client instance. |
||
| 87 | * |
||
| 88 | * @return S3Client |
||
| 89 | */ |
||
| 90 | 2 | public function getClient() |
|
| 91 | { |
||
| 92 | 2 | return $this->s3Client; |
|
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Write a new file. |
||
| 97 | * |
||
| 98 | * @param string $path |
||
| 99 | * @param string $contents |
||
| 100 | * @param Config $config Config object |
||
| 101 | * |
||
| 102 | * @return false|array false on failure file meta data on success |
||
| 103 | */ |
||
| 104 | 2 | public function write($path, $contents, Config $config) |
|
| 105 | { |
||
| 106 | 2 | return $this->upload($path, $contents, $config); |
|
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Update a 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 update($path, $contents, Config $config) |
|
| 119 | { |
||
| 120 | 2 | return $this->upload($path, $contents, $config); |
|
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Rename a file. |
||
| 125 | * |
||
| 126 | * @param string $path |
||
| 127 | * @param string $newpath |
||
| 128 | * |
||
| 129 | * @return bool |
||
| 130 | */ |
||
| 131 | 4 | public function rename($path, $newpath) |
|
| 132 | { |
||
| 133 | 4 | if ( ! $this->copy($path, $newpath)) { |
|
| 134 | 2 | return false; |
|
| 135 | } |
||
| 136 | |||
| 137 | 2 | return $this->delete($path); |
|
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Delete a file. |
||
| 142 | * |
||
| 143 | * @param string $path |
||
| 144 | * |
||
| 145 | * @return bool |
||
| 146 | */ |
||
| 147 | 6 | public function delete($path) |
|
| 148 | { |
||
| 149 | 4 | $location = $this->applyPathPrefix($path); |
|
| 150 | |||
| 151 | 4 | $command = $this->s3Client->getCommand( |
|
| 152 | 4 | 'deleteObject', |
|
| 153 | [ |
||
| 154 | 4 | 'Bucket' => $this->bucket, |
|
| 155 | 4 | 'Key' => $location, |
|
| 156 | ] |
||
| 157 | 6 | ); |
|
| 158 | |||
| 159 | 4 | $this->s3Client->execute($command); |
|
| 160 | |||
| 161 | 4 | return ! $this->has($path); |
|
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Delete a directory. |
||
| 166 | * |
||
| 167 | * @param string $dirname |
||
| 168 | * |
||
| 169 | * @return bool |
||
| 170 | */ |
||
| 171 | 4 | public function deleteDir($dirname) |
|
| 172 | { |
||
| 173 | try { |
||
| 174 | 4 | $prefix = $this->applyPathPrefix($dirname) . '/'; |
|
| 175 | 4 | $this->s3Client->deleteMatchingObjects($this->bucket, $prefix); |
|
| 176 | 4 | } catch (DeleteMultipleObjectsException $exception) { |
|
| 177 | 2 | return false; |
|
| 178 | } |
||
| 179 | |||
| 180 | 2 | return true; |
|
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Create a directory. |
||
| 185 | * |
||
| 186 | * @param string $dirname directory name |
||
| 187 | * @param Config $config |
||
| 188 | * |
||
| 189 | * @return bool|array |
||
| 190 | */ |
||
| 191 | 2 | public function createDir($dirname, Config $config) |
|
| 192 | { |
||
| 193 | 2 | return $this->upload($dirname . '/', '', $config); |
|
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Check whether a file exists. |
||
| 198 | * |
||
| 199 | * @param string $path |
||
| 200 | * |
||
| 201 | * @return bool |
||
| 202 | */ |
||
| 203 | 12 | public function has($path) |
|
| 204 | { |
||
| 205 | 12 | $location = $this->applyPathPrefix($path); |
|
| 206 | |||
| 207 | 12 | if ($this->s3Client->doesObjectExist($this->bucket, $location)) { |
|
| 208 | 2 | return true; |
|
| 209 | } |
||
| 210 | |||
| 211 | 10 | return $this->doesDirectoryExist($location); |
|
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Read a file. |
||
| 216 | * |
||
| 217 | * @param string $path |
||
| 218 | * |
||
| 219 | * @return false|array |
||
| 220 | */ |
||
| 221 | 4 | public function read($path) |
|
| 222 | { |
||
| 223 | 4 | $response = $this->readObject($path); |
|
| 224 | |||
| 225 | 4 | if ($response !== false) { |
|
| 226 | 2 | $response['contents'] = $response['contents']->getContents(); |
|
| 227 | 2 | } |
|
| 228 | |||
| 229 | 4 | return $response; |
|
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * List contents of a directory. |
||
| 234 | * |
||
| 235 | * @param string $directory |
||
| 236 | * @param bool $recursive |
||
| 237 | * |
||
| 238 | * @return array |
||
| 239 | */ |
||
| 240 | 2 | public function listContents($directory = '', $recursive = false) |
|
| 255 | |||
| 256 | /** |
||
| 257 | * @param array $options |
||
| 258 | * |
||
| 259 | * @return array |
||
| 260 | */ |
||
| 261 | 2 | protected function retrievePaginatedListing(array $options) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * Get all the meta data of a file or directory. |
||
| 275 | * |
||
| 276 | * @param string $path |
||
| 277 | * |
||
| 278 | * @return false|array |
||
| 279 | */ |
||
| 280 | 12 | public function getMetadata($path) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Get all the meta data of a file or directory. |
||
| 308 | * |
||
| 309 | * @param string $path |
||
| 310 | * |
||
| 311 | * @return false|array |
||
| 312 | */ |
||
| 313 | 2 | public function getSize($path) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Get the mimetype of a file. |
||
| 320 | * |
||
| 321 | * @param string $path |
||
| 322 | * |
||
| 323 | * @return false|array |
||
| 324 | */ |
||
| 325 | 2 | public function getMimetype($path) |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Get the timestamp of a file. |
||
| 332 | * |
||
| 333 | * @param string $path |
||
| 334 | * |
||
| 335 | * @return false|array |
||
| 336 | */ |
||
| 337 | 2 | public function getTimestamp($path) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Write a new file using a stream. |
||
| 344 | * |
||
| 345 | * @param string $path |
||
| 346 | * @param resource $resource |
||
| 347 | * @param Config $config Config object |
||
| 348 | * |
||
| 349 | * @return array|false false on failure file meta data on success |
||
| 350 | */ |
||
| 351 | 2 | public function writeStream($path, $resource, Config $config) |
|
| 355 | |||
| 356 | /** |
||
| 357 | * Update a 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 updateStream($path, $resource, Config $config) |
|
| 369 | |||
| 370 | /** |
||
| 371 | * Copy a file. |
||
| 372 | * |
||
| 373 | * @param string $path |
||
| 374 | * @param string $newpath |
||
| 375 | * |
||
| 376 | * @return bool |
||
| 377 | */ |
||
| 378 | 8 | public function copy($path, $newpath) |
|
| 379 | { |
||
| 380 | 8 | $command = $this->s3Client->getCommand( |
|
| 399 | |||
| 400 | /** |
||
| 401 | * Read a file as a stream. |
||
| 402 | * |
||
| 403 | * @param string $path |
||
| 404 | * |
||
| 405 | * @return array|false |
||
| 406 | */ |
||
| 407 | 2 | public function readStream($path) |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Read an object and normalize the response. |
||
| 421 | * |
||
| 422 | * @param $path |
||
| 423 | * |
||
| 424 | * @return array|bool |
||
| 425 | */ |
||
| 426 | 6 | protected function readObject($path) |
|
| 448 | |||
| 449 | /** |
||
| 450 | * Set the visibility for a file. |
||
| 451 | * |
||
| 452 | * @param string $path |
||
| 453 | * @param string $visibility |
||
| 454 | * |
||
| 455 | * @return array|false file meta data |
||
| 456 | */ |
||
| 457 | 6 | public function setVisibility($path, $visibility) |
|
| 476 | |||
| 477 | /** |
||
| 478 | * Get the visibility of a file. |
||
| 479 | * |
||
| 480 | * @param string $path |
||
| 481 | * |
||
| 482 | * @return array|false |
||
| 483 | */ |
||
| 484 | 4 | public function getVisibility($path) |
|
| 488 | |||
| 489 | /** |
||
| 490 | * {@inheritdoc} |
||
| 491 | */ |
||
| 492 | 62 | public function applyPathPrefix($prefix) |
|
| 496 | |||
| 497 | /** |
||
| 498 | * {@inheritdoc} |
||
| 499 | */ |
||
| 500 | 68 | public function setPathPrefix($prefix) |
|
| 506 | |||
| 507 | /** |
||
| 508 | * Get the object acl presented as a visibility. |
||
| 509 | * |
||
| 510 | * @param string $path |
||
| 511 | * |
||
| 512 | * @return string |
||
| 513 | */ |
||
| 514 | 12 | protected function getRawVisibility($path) |
|
| 540 | |||
| 541 | /** |
||
| 542 | * Upload an object. |
||
| 543 | * |
||
| 544 | * @param $path |
||
| 545 | * @param $body |
||
| 546 | * @param Config $config |
||
| 547 | * |
||
| 548 | * @return array |
||
| 549 | */ |
||
| 550 | 10 | protected function upload($path, $body, Config $config) |
|
| 568 | |||
| 569 | /** |
||
| 570 | * Get options from the config. |
||
| 571 | * |
||
| 572 | * @param Config $config |
||
| 573 | * |
||
| 574 | * @return array |
||
| 575 | */ |
||
| 576 | 10 | protected function getOptionsFromConfig(Config $config) |
|
| 603 | |||
| 604 | /** |
||
| 605 | * Normalize the object result array. |
||
| 606 | * |
||
| 607 | * @param array $response |
||
| 608 | * @param string $path |
||
| 609 | * |
||
| 610 | * @return array |
||
| 611 | */ |
||
| 612 | 22 | protected function normalizeResponse(array $response, $path = null) |
|
| 634 | |||
| 635 | /** |
||
| 636 | * @param $location |
||
| 637 | * |
||
| 638 | * @return bool |
||
| 639 | */ |
||
| 640 | 10 | protected function doesDirectoryExist($location) |
|
| 665 | } |
||
| 666 |