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 | { |
||
| 20 | protected $service; |
||
| 21 | protected $bucket; |
||
| 22 | protected $options; |
||
| 23 | protected $bucketExists; |
||
| 24 | protected $metadata = array(); |
||
| 25 | protected $detectContentType; |
||
| 26 | |||
| 27 | View Code Duplication | public function __construct(S3Client $service, $bucket, array $options = array(), $detectContentType = false) |
|
| 28 | { |
||
| 29 | $this->service = $service; |
||
| 30 | $this->bucket = $bucket; |
||
| 31 | $this->options = array_replace( |
||
| 32 | array( |
||
| 33 | 'create' => false, |
||
| 34 | 'directory' => '', |
||
| 35 | 'acl' => 'private', |
||
| 36 | ), |
||
| 37 | $options |
||
| 38 | ); |
||
| 39 | |||
| 40 | $this->detectContentType = $detectContentType; |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Gets the publicly accessible URL of an Amazon S3 object. |
||
| 45 | * |
||
| 46 | * @param string $key Object key |
||
| 47 | * @param array $options Associative array of options used to buld the URL |
||
| 48 | * - expires: The time at which the URL should expire |
||
| 49 | * represented as a UNIX timestamp |
||
| 50 | * - Any options available in the Amazon S3 GetObject |
||
| 51 | * operation may be specified. |
||
| 52 | * |
||
| 53 | * @return string |
||
| 54 | */ |
||
| 55 | public function getUrl($key, array $options = array()) |
||
| 56 | { |
||
| 57 | return $this->service->getObjectUrl( |
||
| 58 | $this->bucket, |
||
| 59 | $this->computePath($key), |
||
| 60 | isset($options['expires']) ? $options['expires'] : null, |
||
| 61 | $options |
||
| 62 | ); |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * {@inheritdoc} |
||
| 67 | */ |
||
| 68 | public function setMetadata($key, $metadata) |
||
| 69 | { |
||
| 70 | // BC with AmazonS3 adapter |
||
| 71 | if (isset($metadata['contentType'])) { |
||
| 72 | $metadata['ContentType'] = $metadata['contentType']; |
||
| 73 | unset($metadata['contentType']); |
||
| 74 | } |
||
| 75 | |||
| 76 | $this->metadata[$key] = $metadata; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * {@inheritdoc} |
||
| 81 | */ |
||
| 82 | public function getMetadata($key) |
||
| 83 | { |
||
| 84 | return isset($this->metadata[$key]) ? $this->metadata[$key] : array(); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * {@inheritdoc} |
||
| 89 | */ |
||
| 90 | public function read($key) |
||
| 91 | { |
||
| 92 | $this->ensureBucketExists(); |
||
| 93 | $options = $this->getOptions($key); |
||
| 94 | |||
| 95 | try { |
||
| 96 | return (string) $this->service->getObject($options)->get('Body'); |
||
| 97 | } catch (\Exception $e) { |
||
| 98 | return false; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * {@inheritdoc} |
||
| 104 | */ |
||
| 105 | public function rename($sourceKey, $targetKey) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * {@inheritdoc} |
||
| 126 | */ |
||
| 127 | public function write($key, $content) |
||
| 128 | { |
||
| 129 | $this->ensureBucketExists(); |
||
| 130 | $options = $this->getOptions($key, array('Body' => $content)); |
||
| 131 | |||
| 132 | /* |
||
| 133 | * If the ContentType was not already set in the metadata, then we autodetect |
||
| 134 | * it to prevent everything being served up as binary/octet-stream. |
||
| 135 | */ |
||
| 136 | if (!isset($options['ContentType']) && $this->detectContentType) { |
||
| 137 | $fileInfo = new \finfo(FILEINFO_MIME_TYPE); |
||
| 138 | if (is_resource($content)) { |
||
| 139 | $contentType = $fileInfo->file(stream_get_meta_data($content)['uri']); |
||
| 140 | } else { |
||
| 141 | $contentType = $fileInfo->buffer($content); |
||
| 142 | } |
||
| 143 | $options['ContentType'] = $contentType; |
||
| 144 | } |
||
| 145 | |||
| 146 | try { |
||
| 147 | $this->service->putObject($options); |
||
| 148 | if (is_resource($content)) { |
||
| 149 | return Util\Size::fromResource($content); |
||
| 150 | } else { |
||
| 151 | return Util\Size::fromContent($content); |
||
| 152 | } |
||
| 153 | } catch (\Exception $e) { |
||
| 154 | return false; |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * {@inheritdoc} |
||
| 160 | */ |
||
| 161 | public function exists($key) |
||
| 162 | { |
||
| 163 | return $this->service->doesObjectExist($this->bucket, $this->computePath($key)); |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * {@inheritdoc} |
||
| 168 | */ |
||
| 169 | View Code Duplication | public function mtime($key) |
|
| 170 | { |
||
| 171 | try { |
||
| 172 | $result = $this->service->headObject($this->getOptions($key)); |
||
| 173 | |||
| 174 | return strtotime($result['LastModified']); |
||
| 175 | } catch (\Exception $e) { |
||
| 176 | return false; |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * {@inheritdoc} |
||
| 182 | */ |
||
| 183 | View Code Duplication | public function size($key) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * {@inheritdoc} |
||
| 196 | */ |
||
| 197 | public function keys() |
||
| 198 | { |
||
| 199 | return $this->listKeys(); |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * {@inheritdoc} |
||
| 204 | */ |
||
| 205 | public function listKeys($prefix = '') |
||
| 222 | |||
| 223 | /** |
||
| 224 | * List files beginning with given prefix. |
||
| 225 | * Similar to listKeys but this will also return file mtime, size, and etag. |
||
| 226 | * @param string $prefix |
||
| 227 | * @return mixed |
||
| 228 | */ |
||
| 229 | public function listFiles($prefix = '') |
||
| 230 | { |
||
| 231 | $options = array('Bucket' => $this->bucket); |
||
| 257 | |||
| 258 | /** |
||
| 259 | * {@inheritdoc} |
||
| 260 | */ |
||
| 261 | public function delete($key) |
||
| 271 | |||
| 272 | /** |
||
| 273 | * {@inheritdoc} |
||
| 274 | */ |
||
| 275 | public function isDirectory($key) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Ensures the specified bucket exists. If the bucket does not exists |
||
| 288 | * and the create option is set to true, it will try to create the |
||
| 289 | * bucket. The bucket is created using the same region as the supplied |
||
| 290 | * client object. |
||
| 291 | * |
||
| 292 | * @throws \RuntimeException if the bucket does not exists or could not be |
||
| 293 | * created |
||
| 294 | */ |
||
| 295 | protected function ensureBucketExists() |
||
| 322 | |||
| 323 | protected function getOptions($key, array $options = array()) |
||
| 337 | |||
| 338 | View Code Duplication | protected function computePath($key) |
|
| 346 | |||
| 347 | /** |
||
| 348 | * Computes the key from the specified path. |
||
| 349 | * |
||
| 350 | * @param string $path |
||
| 351 | * |
||
| 352 | * return string |
||
| 353 | */ |
||
| 354 | protected function computeKey($path) |
||
| 358 | } |
||
| 359 |