Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like AwsS3 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 AwsS3, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class AwsS3 implements Adapter, |
||
|
|
|||
| 16 | MetadataSupporter, |
||
| 17 | ListKeysAware, |
||
| 18 | SizeCalculator, |
||
| 19 | MimeTypeProvider |
||
| 20 | { |
||
| 21 | /* |
||
| 22 | * Amazon S3 does not not allow uploads > 5Go |
||
| 23 | */ |
||
| 24 | const MAX_CONTENT_SIZE = 5368709120; |
||
| 25 | const DEFAULT_PART_SIZE = 5242880; |
||
| 26 | |||
| 27 | /** @var S3Client */ |
||
| 28 | protected $service; |
||
| 29 | /** @var string */ |
||
| 30 | protected $bucket; |
||
| 31 | /** @var array */ |
||
| 32 | protected $options; |
||
| 33 | /** @var bool */ |
||
| 34 | protected $bucketExists; |
||
| 35 | /** @var array */ |
||
| 36 | protected $metadata = []; |
||
| 37 | /** @var bool */ |
||
| 38 | protected $detectContentType; |
||
| 39 | /** @var int */ |
||
| 40 | protected $sizeLimit = self::MAX_CONTENT_SIZE; |
||
| 41 | /** @var int */ |
||
| 42 | protected $partSize = self::DEFAULT_PART_SIZE; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @param S3Client $service |
||
| 46 | * @param string $bucket |
||
| 47 | * @param array $options |
||
| 48 | * @param bool $detectContentType |
||
| 49 | */ |
||
| 50 | public function __construct(S3Client $service, $bucket, array $options = [], $detectContentType = false) |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Gets the publicly accessible URL of an Amazon S3 object. |
||
| 78 | * |
||
| 79 | * @param string $key Object key |
||
| 80 | * @param array $options Associative array of options used to buld the URL |
||
| 81 | * - expires: The time at which the URL should expire |
||
| 82 | * represented as a UNIX timestamp |
||
| 83 | * - Any options available in the Amazon S3 GetObject |
||
| 84 | * operation may be specified. |
||
| 85 | * |
||
| 86 | * @return string |
||
| 87 | * |
||
| 88 | * @deprecated 1.0 Resolving object path into URLs is out of the scope of this repository since v0.4. gaufrette/extras |
||
| 89 | * provides a Filesystem decorator with a regular resolve() method. You should use it instead. |
||
| 90 | * |
||
| 91 | * @see https://github.com/Gaufrette/extras |
||
| 92 | */ |
||
| 93 | public function getUrl($key, array $options = []) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * {@inheritdoc} |
||
| 110 | */ |
||
| 111 | public function setMetadata($key, $metadata) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * {@inheritdoc} |
||
| 124 | */ |
||
| 125 | public function getMetadata($key) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * {@inheritdoc} |
||
| 132 | */ |
||
| 133 | public function read($key) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * {@inheritdoc} |
||
| 156 | */ |
||
| 157 | public function rename($sourceKey, $targetKey) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * {@inheritdoc} |
||
| 176 | */ |
||
| 177 | public function write($key, $content) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * {@inheritdoc} |
||
| 219 | */ |
||
| 220 | public function exists($key) |
||
| 224 | |||
| 225 | /** |
||
| 226 | * {@inheritdoc} |
||
| 227 | */ |
||
| 228 | View Code Duplication | public function mtime($key) |
|
| 238 | |||
| 239 | /** |
||
| 240 | * {@inheritdoc} |
||
| 241 | */ |
||
| 242 | View Code Duplication | public function size($key) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * {@inheritdoc} |
||
| 255 | */ |
||
| 256 | public function keys() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * {@inheritdoc} |
||
| 263 | */ |
||
| 264 | public function listKeys($prefix = '') |
||
| 281 | |||
| 282 | /** |
||
| 283 | * {@inheritdoc} |
||
| 284 | */ |
||
| 285 | public function delete($key) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * {@inheritdoc} |
||
| 298 | */ |
||
| 299 | public function isDirectory($key) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Ensures the specified bucket exists. If the bucket does not exists |
||
| 312 | * and the create option is set to true, it will try to create the |
||
| 313 | * bucket. The bucket is created using the same region as the supplied |
||
| 314 | * client object. |
||
| 315 | * |
||
| 316 | * @throws \RuntimeException if the bucket does not exists or could not be |
||
| 317 | * created |
||
| 318 | */ |
||
| 319 | protected function ensureBucketExists() |
||
| 344 | |||
| 345 | protected function getOptions($key, array $options = []) |
||
| 359 | |||
| 360 | View Code Duplication | protected function computePath($key) |
|
| 368 | |||
| 369 | |||
| 370 | /** |
||
| 371 | * MultiPart upload for big files (exceeding size_limit) |
||
| 372 | * |
||
| 373 | * @param $key |
||
| 374 | * @param $content |
||
| 375 | * |
||
| 376 | * @return bool |
||
| 377 | */ |
||
| 378 | protected function multipartUpload($key, $content) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @param $key |
||
| 408 | * |
||
| 409 | * @return mixed |
||
| 410 | */ |
||
| 411 | protected function initiateMultipartUpload($key) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @param $key |
||
| 421 | * @param $content |
||
| 422 | * @param $uploadId |
||
| 423 | * @param $partNumber |
||
| 424 | * |
||
| 425 | * @return \Aws\Result |
||
| 426 | */ |
||
| 427 | protected function uploadNextPart($key, $content, $uploadId, $partNumber) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * @param $key |
||
| 445 | * @param $uploadId |
||
| 446 | * @param $parts |
||
| 447 | */ |
||
| 448 | protected function completeMultipartUpload($key, $uploadId, $parts) |
||
| 459 | |||
| 460 | protected function abortMultipartUpload($key, $uploadId) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Computes the key from the specified path. |
||
| 468 | * |
||
| 469 | * @param string $path |
||
| 470 | * |
||
| 471 | * @return string |
||
| 472 | */ |
||
| 473 | protected function computeKey($path) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @param string $content |
||
| 480 | * |
||
| 481 | * @return string |
||
| 482 | */ |
||
| 483 | View Code Duplication | private function guessContentType($content) |
|
| 493 | |||
| 494 | View Code Duplication | public function mimeType($key) |
|
| 503 | } |
||
| 504 |