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) |
|
| 229 | { |
||
| 230 | try { |
||
| 231 | $result = $this->service->headObject($this->getOptions($key)); |
||
| 232 | |||
| 233 | return strtotime($result['LastModified']); |
||
| 234 | } catch (\Exception $e) { |
||
| 235 | return false; |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * {@inheritdoc} |
||
| 241 | */ |
||
| 242 | View Code Duplication | public function size($key) |
|
| 243 | { |
||
| 244 | try { |
||
| 245 | $result = $this->service->headObject($this->getOptions($key)); |
||
| 246 | |||
| 247 | return $result['ContentLength']; |
||
| 248 | } catch (\Exception $e) { |
||
| 249 | return false; |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * {@inheritdoc} |
||
| 255 | */ |
||
| 256 | public function keys() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * {@inheritdoc} |
||
| 263 | */ |
||
| 264 | public function listKeys($prefix = '') |
||
| 283 | |||
| 284 | /** |
||
| 285 | * {@inheritdoc} |
||
| 286 | */ |
||
| 287 | public function delete($key) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * {@inheritdoc} |
||
| 300 | */ |
||
| 301 | public function isDirectory($key) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Ensures the specified bucket exists. If the bucket does not exists |
||
| 319 | * and the create option is set to true, it will try to create the |
||
| 320 | * bucket. The bucket is created using the same region as the supplied |
||
| 321 | * client object. |
||
| 322 | * |
||
| 323 | * @throws \RuntimeException if the bucket does not exists or could not be |
||
| 324 | * created |
||
| 325 | */ |
||
| 326 | protected function ensureBucketExists() |
||
| 351 | |||
| 352 | protected function getOptions($key, array $options = []) |
||
| 366 | |||
| 367 | View Code Duplication | protected function computePath($key) |
|
| 375 | |||
| 376 | |||
| 377 | /** |
||
| 378 | * MultiPart upload for big files (exceeding size_limit) |
||
| 379 | * |
||
| 380 | * @param $key |
||
| 381 | * @param $content |
||
| 382 | * |
||
| 383 | * @return bool |
||
| 384 | */ |
||
| 385 | protected function multipartUpload($key, $content) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @param $key |
||
| 415 | * |
||
| 416 | * @return mixed |
||
| 417 | */ |
||
| 418 | protected function initiateMultipartUpload($key) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @param $key |
||
| 428 | * @param $content |
||
| 429 | * @param $uploadId |
||
| 430 | * @param $partNumber |
||
| 431 | * |
||
| 432 | * @return \Aws\Result |
||
| 433 | */ |
||
| 434 | protected function uploadNextPart($key, $content, $uploadId, $partNumber) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @param $key |
||
| 452 | * @param $uploadId |
||
| 453 | * @param $parts |
||
| 454 | */ |
||
| 455 | protected function completeMultipartUpload($key, $uploadId, $parts) |
||
| 466 | |||
| 467 | protected function abortMultipartUpload($key, $uploadId) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Computes the key from the specified path. |
||
| 475 | * |
||
| 476 | * @param string $path |
||
| 477 | * |
||
| 478 | * @return string |
||
| 479 | */ |
||
| 480 | protected function computeKey($path) |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @param string $content |
||
| 487 | * |
||
| 488 | * @return string |
||
| 489 | */ |
||
| 490 | View Code Duplication | private function guessContentType($content) |
|
| 500 | |||
| 501 | View Code Duplication | public function mimeType($key) |
|
| 510 | } |
||
| 511 |