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 | ]; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | protected static $metaOptions = [ |
||
| 34 | 'CacheControl', |
||
| 35 | 'Expires', |
||
| 36 | 'StorageClass', |
||
| 37 | 'ServerSideEncryption', |
||
| 38 | 'Metadata', |
||
| 39 | 'ACL', |
||
| 40 | 'ContentType', |
||
| 41 | 'ContentEncoding', |
||
| 42 | 'ContentDisposition', |
||
| 43 | 'ContentLength', |
||
| 44 | 'Tagging', |
||
| 45 | 'WebsiteRedirectLocation', |
||
| 46 | 'SSEKMSKeyId' |
||
| 47 | ]; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var S3Client |
||
| 51 | */ |
||
| 52 | protected $s3Client; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | protected $bucket; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $options = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Constructor. |
||
| 66 | * |
||
| 67 | * @param S3Client $client |
||
| 68 | * @param string $bucket |
||
| 69 | * @param string $prefix |
||
| 70 | * @param array $options |
||
| 71 | */ |
||
| 72 | 72 | public function __construct(S3Client $client, $bucket, $prefix = '', array $options = []) |
|
| 79 | |||
| 80 | /** |
||
| 81 | * Get the S3Client bucket. |
||
| 82 | * |
||
| 83 | * @return string |
||
| 84 | */ |
||
| 85 | 4 | public function getBucket() |
|
| 89 | |||
| 90 | /** |
||
| 91 | * Set the S3Client bucket. |
||
| 92 | * |
||
| 93 | * @return string |
||
| 94 | */ |
||
| 95 | 2 | public function setBucket($bucket) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * Get the S3Client instance. |
||
| 102 | * |
||
| 103 | * @return S3Client |
||
| 104 | */ |
||
| 105 | 2 | public function getClient() |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Write a new file. |
||
| 112 | * |
||
| 113 | * @param string $path |
||
| 114 | * @param string $contents |
||
| 115 | * @param Config $config Config object |
||
| 116 | * |
||
| 117 | * @return false|array false on failure file meta data on success |
||
| 118 | */ |
||
| 119 | 2 | public function write($path, $contents, Config $config) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Update a file. |
||
| 126 | * |
||
| 127 | * @param string $path |
||
| 128 | * @param string $contents |
||
| 129 | * @param Config $config Config object |
||
| 130 | * |
||
| 131 | * @return false|array false on failure file meta data on success |
||
| 132 | */ |
||
| 133 | 2 | public function update($path, $contents, Config $config) |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Rename a file. |
||
| 140 | * |
||
| 141 | * @param string $path |
||
| 142 | * @param string $newpath |
||
| 143 | * |
||
| 144 | * @return bool |
||
| 145 | */ |
||
| 146 | 4 | public function rename($path, $newpath) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * Delete a file. |
||
| 157 | * |
||
| 158 | * @param string $path |
||
| 159 | * |
||
| 160 | * @return bool |
||
| 161 | */ |
||
| 162 | 4 | public function delete($path) |
|
| 163 | { |
||
| 164 | 4 | $location = $this->applyPathPrefix($path); |
|
| 165 | |||
| 166 | 4 | $command = $this->s3Client->getCommand( |
|
| 167 | 4 | 'deleteObject', |
|
| 168 | [ |
||
| 169 | 4 | 'Bucket' => $this->bucket, |
|
| 170 | 4 | 'Key' => $location, |
|
| 171 | ] |
||
| 172 | ); |
||
| 173 | |||
| 174 | 4 | $this->s3Client->execute($command); |
|
| 175 | |||
| 176 | 4 | return ! $this->has($path); |
|
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Delete a directory. |
||
| 181 | * |
||
| 182 | * @param string $dirname |
||
| 183 | * |
||
| 184 | * @return bool |
||
| 185 | */ |
||
| 186 | 4 | public function deleteDir($dirname) |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Create a directory. |
||
| 200 | * |
||
| 201 | * @param string $dirname directory name |
||
| 202 | * @param Config $config |
||
| 203 | * |
||
| 204 | * @return bool|array |
||
| 205 | */ |
||
| 206 | 2 | public function createDir($dirname, Config $config) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Check whether a file exists. |
||
| 213 | * |
||
| 214 | * @param string $path |
||
| 215 | * |
||
| 216 | * @return bool |
||
| 217 | */ |
||
| 218 | 12 | public function has($path) |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Read a file. |
||
| 231 | * |
||
| 232 | * @param string $path |
||
| 233 | * |
||
| 234 | * @return false|array |
||
| 235 | */ |
||
| 236 | 4 | public function read($path) |
|
| 237 | { |
||
| 238 | 4 | $response = $this->readObject($path); |
|
| 239 | |||
| 240 | 4 | if ($response !== false) { |
|
| 241 | 2 | $response['contents'] = $response['contents']->getContents(); |
|
| 242 | } |
||
| 243 | |||
| 244 | 4 | return $response; |
|
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * List contents of a directory. |
||
| 249 | * |
||
| 250 | * @param string $directory |
||
| 251 | * @param bool $recursive |
||
| 252 | * |
||
| 253 | * @return array |
||
| 254 | */ |
||
| 255 | 2 | public function listContents($directory = '', $recursive = false) |
|
| 256 | { |
||
| 257 | 2 | $prefix = $this->applyPathPrefix(rtrim($directory, '/') . '/'); |
|
| 258 | 2 | $options = ['Bucket' => $this->bucket, 'Prefix' => ltrim($prefix, '/')]; |
|
| 259 | |||
| 260 | 2 | if ($recursive === false) { |
|
| 261 | 2 | $options['Delimiter'] = '/'; |
|
| 262 | } |
||
| 263 | |||
| 264 | 2 | $listing = $this->retrievePaginatedListing($options); |
|
| 265 | 2 | $normalizer = [$this, 'normalizeResponse']; |
|
| 266 | 2 | $normalized = array_map($normalizer, $listing); |
|
| 267 | |||
| 268 | 2 | return Util::emulateDirectories($normalized); |
|
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * @param array $options |
||
| 273 | * |
||
| 274 | * @return array |
||
| 275 | */ |
||
| 276 | 2 | protected function retrievePaginatedListing(array $options) |
|
| 277 | { |
||
| 278 | 2 | $resultPaginator = $this->s3Client->getPaginator('ListObjects', $options); |
|
| 279 | 2 | $listing = []; |
|
| 280 | |||
| 281 | 2 | foreach ($resultPaginator as $result) { |
|
| 282 | $listing = array_merge($listing, $result->get('Contents') ?: [], $result->get('CommonPrefixes') ?: []); |
||
| 283 | } |
||
| 284 | |||
| 285 | 2 | return $listing; |
|
| 286 | } |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Get all the meta data of a file or directory. |
||
| 290 | * |
||
| 291 | * @param string $path |
||
| 292 | * |
||
| 293 | * @return false|array |
||
| 294 | */ |
||
| 295 | 12 | public function getMetadata($path) |
|
| 296 | { |
||
| 297 | 12 | $command = $this->s3Client->getCommand( |
|
| 298 | 12 | 'headObject', |
|
| 299 | [ |
||
| 300 | 12 | 'Bucket' => $this->bucket, |
|
| 301 | 12 | 'Key' => $this->applyPathPrefix($path), |
|
| 302 | 12 | ] + $this->options |
|
| 303 | ); |
||
| 304 | |||
| 305 | /* @var Result $result */ |
||
| 306 | try { |
||
| 307 | 12 | $result = $this->s3Client->execute($command); |
|
| 308 | 4 | } catch (S3Exception $exception) { |
|
| 309 | 4 | $response = $exception->getResponse(); |
|
| 310 | |||
| 311 | 4 | if ($response !== null && $response->getStatusCode() === 404) { |
|
| 312 | 2 | return false; |
|
| 313 | } |
||
| 314 | |||
| 315 | 2 | throw $exception; |
|
| 316 | } |
||
| 317 | |||
| 318 | 8 | return $this->normalizeResponse($result->toArray(), $path); |
|
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Get all the meta data of a file or directory. |
||
| 323 | * |
||
| 324 | * @param string $path |
||
| 325 | * |
||
| 326 | * @return false|array |
||
| 327 | */ |
||
| 328 | 2 | public function getSize($path) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Get the mimetype of a file. |
||
| 335 | * |
||
| 336 | * @param string $path |
||
| 337 | * |
||
| 338 | * @return false|array |
||
| 339 | */ |
||
| 340 | 2 | public function getMimetype($path) |
|
| 344 | |||
| 345 | /** |
||
| 346 | * Get the timestamp of a file. |
||
| 347 | * |
||
| 348 | * @param string $path |
||
| 349 | * |
||
| 350 | * @return false|array |
||
| 351 | */ |
||
| 352 | 2 | public function getTimestamp($path) |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Write a new file using a stream. |
||
| 359 | * |
||
| 360 | * @param string $path |
||
| 361 | * @param resource $resource |
||
| 362 | * @param Config $config Config object |
||
| 363 | * |
||
| 364 | * @return array|false false on failure file meta data on success |
||
| 365 | */ |
||
| 366 | 2 | public function writeStream($path, $resource, Config $config) |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Update a file using a stream. |
||
| 373 | * |
||
| 374 | * @param string $path |
||
| 375 | * @param resource $resource |
||
| 376 | * @param Config $config Config object |
||
| 377 | * |
||
| 378 | * @return array|false false on failure file meta data on success |
||
| 379 | */ |
||
| 380 | 2 | public function updateStream($path, $resource, Config $config) |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Copy a file. |
||
| 387 | * |
||
| 388 | * @param string $path |
||
| 389 | * @param string $newpath |
||
| 390 | * |
||
| 391 | * @return bool |
||
| 392 | */ |
||
| 393 | 8 | public function copy($path, $newpath) |
|
| 394 | { |
||
| 395 | 8 | $command = $this->s3Client->getCommand( |
|
| 396 | 8 | 'copyObject', |
|
| 397 | [ |
||
| 398 | 8 | 'Bucket' => $this->bucket, |
|
| 399 | 8 | 'Key' => $this->applyPathPrefix($newpath), |
|
| 400 | 8 | 'CopySource' => urlencode($this->bucket . '/' . $this->applyPathPrefix($path)), |
|
| 401 | 8 | 'ACL' => $this->getRawVisibility($path) === AdapterInterface::VISIBILITY_PUBLIC |
|
| 402 | 8 | ? 'public-read' : 'private', |
|
| 403 | 8 | ] + $this->options |
|
| 404 | ); |
||
| 405 | |||
| 406 | try { |
||
| 407 | 8 | $this->s3Client->execute($command); |
|
| 408 | 4 | } catch (S3Exception $e) { |
|
| 409 | 4 | return false; |
|
| 410 | } |
||
| 411 | |||
| 412 | 4 | return true; |
|
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Read a file as a stream. |
||
| 417 | * |
||
| 418 | * @param string $path |
||
| 419 | * |
||
| 420 | * @return array|false |
||
| 421 | */ |
||
| 422 | 4 | public function readStream($path) |
|
| 423 | { |
||
| 424 | 4 | $response = $this->readObject($path); |
|
| 425 | |||
| 426 | 4 | if ($response !== false) { |
|
| 427 | 4 | $response['stream'] = $response['contents']->detach(); |
|
| 428 | 4 | unset($response['contents']); |
|
| 429 | } |
||
| 430 | |||
| 431 | 4 | return $response; |
|
| 432 | } |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Read an object and normalize the response. |
||
| 436 | * |
||
| 437 | * @param $path |
||
| 438 | * |
||
| 439 | * @return array|bool |
||
| 440 | */ |
||
| 441 | 8 | protected function readObject($path) |
|
| 442 | { |
||
| 443 | $options = [ |
||
| 444 | 8 | 'Bucket' => $this->bucket, |
|
| 445 | 8 | 'Key' => $this->applyPathPrefix($path), |
|
| 446 | ]; |
||
| 447 | |||
| 448 | 8 | if (isset($this->options['@http'])) { |
|
| 449 | 2 | $options['@http'] = $this->options['@http']; |
|
| 450 | } |
||
| 451 | |||
| 452 | 8 | $command = $this->s3Client->getCommand('getObject', $options + $this->options); |
|
| 453 | |||
| 454 | try { |
||
| 455 | /** @var Result $response */ |
||
| 456 | 8 | $response = $this->s3Client->execute($command); |
|
| 457 | 2 | } catch (S3Exception $e) { |
|
| 458 | 2 | return false; |
|
| 459 | } |
||
| 460 | |||
| 461 | 6 | return $this->normalizeResponse($response->toArray(), $path); |
|
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Set the visibility for a file. |
||
| 466 | * |
||
| 467 | * @param string $path |
||
| 468 | * @param string $visibility |
||
| 469 | * |
||
| 470 | * @return array|false file meta data |
||
| 471 | */ |
||
| 472 | 6 | public function setVisibility($path, $visibility) |
|
| 473 | { |
||
| 474 | 6 | $command = $this->s3Client->getCommand( |
|
| 475 | 6 | 'putObjectAcl', |
|
| 476 | [ |
||
| 477 | 6 | 'Bucket' => $this->bucket, |
|
| 478 | 6 | 'Key' => $this->applyPathPrefix($path), |
|
| 479 | 6 | 'ACL' => $visibility === AdapterInterface::VISIBILITY_PUBLIC ? 'public-read' : 'private', |
|
| 480 | ] |
||
| 481 | ); |
||
| 482 | |||
| 483 | try { |
||
| 484 | 6 | $this->s3Client->execute($command); |
|
| 485 | 2 | } catch (S3Exception $exception) { |
|
| 486 | 2 | return false; |
|
| 487 | } |
||
| 488 | |||
| 489 | 4 | return compact('path', 'visibility'); |
|
| 490 | } |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Get the visibility of a file. |
||
| 494 | * |
||
| 495 | * @param string $path |
||
| 496 | * |
||
| 497 | * @return array|false |
||
| 498 | */ |
||
| 499 | 4 | public function getVisibility($path) |
|
| 503 | |||
| 504 | /** |
||
| 505 | * {@inheritdoc} |
||
| 506 | */ |
||
| 507 | 64 | public function applyPathPrefix($path) |
|
| 511 | |||
| 512 | /** |
||
| 513 | * {@inheritdoc} |
||
| 514 | */ |
||
| 515 | 72 | public function setPathPrefix($prefix) |
|
| 521 | |||
| 522 | /** |
||
| 523 | * Get the object acl presented as a visibility. |
||
| 524 | * |
||
| 525 | * @param string $path |
||
| 526 | * |
||
| 527 | * @return string |
||
| 528 | */ |
||
| 529 | 12 | protected function getRawVisibility($path) |
|
| 530 | { |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Upload an object. |
||
| 558 | * |
||
| 559 | * @param $path |
||
| 560 | * @param $body |
||
| 561 | * @param Config $config |
||
| 562 | * |
||
| 563 | * @return array |
||
| 564 | */ |
||
| 565 | 10 | protected function upload($path, $body, Config $config) |
|
| 587 | |||
| 588 | /** |
||
| 589 | * Get options from the config. |
||
| 590 | * |
||
| 591 | * @param Config $config |
||
| 592 | * |
||
| 593 | * @return array |
||
| 594 | */ |
||
| 595 | 10 | protected function getOptionsFromConfig(Config $config) |
|
| 622 | |||
| 623 | /** |
||
| 624 | * Normalize the object result array. |
||
| 625 | * |
||
| 626 | * @param array $response |
||
| 627 | * @param string $path |
||
| 628 | * |
||
| 629 | * @return array |
||
| 630 | */ |
||
| 631 | 24 | protected function normalizeResponse(array $response, $path = null) |
|
| 653 | |||
| 654 | /** |
||
| 655 | * @param $location |
||
| 656 | * |
||
| 657 | * @return bool |
||
| 658 | */ |
||
| 659 | 10 | protected function doesDirectoryExist($location) |
|
| 684 | } |
||
| 685 |