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 |
||
| 14 | class AwsS3 implements Adapter, |
||
|
|
|||
| 15 | MetadataSupporter, |
||
| 16 | ListKeysAware, |
||
| 17 | SizeCalculator |
||
| 18 | { |
||
| 19 | protected $service; |
||
| 20 | protected $bucket; |
||
| 21 | protected $options; |
||
| 22 | protected $bucketExists; |
||
| 23 | protected $metadata = array(); |
||
| 24 | protected $detectContentType; |
||
| 25 | |||
| 26 | View Code Duplication | public function __construct(S3Client $service, $bucket, array $options = array(), $detectContentType = false) |
|
| 41 | |||
| 42 | /** |
||
| 43 | * Gets the publicly accessible URL of an Amazon S3 object. |
||
| 44 | * |
||
| 45 | * @param string $key Object key |
||
| 46 | * @param array $options Associative array of options used to buld the URL |
||
| 47 | * - expires: The time at which the URL should expire |
||
| 48 | * represented as a UNIX timestamp |
||
| 49 | * - Any options available in the Amazon S3 GetObject |
||
| 50 | * operation may be specified. |
||
| 51 | * |
||
| 52 | * @return string |
||
| 53 | */ |
||
| 54 | public function getUrl($key, array $options = array()) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * {@inheritdoc} |
||
| 66 | */ |
||
| 67 | public function setMetadata($key, $metadata) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * {@inheritdoc} |
||
| 80 | */ |
||
| 81 | public function getMetadata($key) |
||
| 85 | |||
| 86 | /** |
||
| 87 | * {@inheritdoc} |
||
| 88 | */ |
||
| 89 | public function read($key) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * {@inheritdoc} |
||
| 103 | */ |
||
| 104 | public function rename($sourceKey, $targetKey) |
||
| 105 | { |
||
| 106 | $this->ensureBucketExists(); |
||
| 107 | $options = $this->getOptions( |
||
| 108 | $targetKey, |
||
| 109 | array( |
||
| 110 | 'CopySource' => $this->bucket.'/'.$this->computePath($sourceKey), |
||
| 111 | ) |
||
| 112 | ); |
||
| 113 | |||
| 114 | try { |
||
| 115 | $this->service->copyObject(array_merge($options, $this->getMetadata($targetKey))); |
||
| 116 | |||
| 117 | return $this->delete($sourceKey); |
||
| 118 | } catch (\Exception $e) { |
||
| 119 | return false; |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * {@inheritdoc} |
||
| 125 | */ |
||
| 126 | public function write($key, $content) |
||
| 127 | { |
||
| 128 | $this->ensureBucketExists(); |
||
| 129 | $options = $this->getOptions($key, array('Body' => $content)); |
||
| 130 | |||
| 131 | /* |
||
| 132 | * If the ContentType was not already set in the metadata, then we autodetect |
||
| 133 | * it to prevent everything being served up as binary/octet-stream. |
||
| 134 | */ |
||
| 135 | if (!isset($options['ContentType']) && $this->detectContentType) { |
||
| 136 | $options['ContentType'] = $this->guessContentType($content); |
||
| 137 | } |
||
| 138 | |||
| 139 | try { |
||
| 140 | $this->service->putObject($options); |
||
| 141 | |||
| 142 | if (is_resource($content)) { |
||
| 143 | return Util\Size::fromResource($content); |
||
| 144 | } |
||
| 145 | |||
| 146 | return Util\Size::fromContent($content); |
||
| 147 | } catch (\Exception $e) { |
||
| 148 | return false; |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * {@inheritdoc} |
||
| 154 | */ |
||
| 155 | public function exists($key) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * {@inheritdoc} |
||
| 162 | */ |
||
| 163 | View Code Duplication | public function mtime($key) |
|
| 173 | |||
| 174 | /** |
||
| 175 | * {@inheritdoc} |
||
| 176 | */ |
||
| 177 | View Code Duplication | public function size($key) |
|
| 178 | { |
||
| 179 | try { |
||
| 180 | $result = $this->service->headObject($this->getOptions($key)); |
||
| 181 | |||
| 182 | return $result['ContentLength']; |
||
| 183 | } catch (\Exception $e) { |
||
| 184 | return false; |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * {@inheritdoc} |
||
| 190 | */ |
||
| 191 | public function keys() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * {@inheritdoc} |
||
| 198 | */ |
||
| 199 | public function listKeys($prefix = '') |
||
| 200 | { |
||
| 201 | $options = array('Bucket' => $this->bucket); |
||
| 202 | View Code Duplication | if ((string) $prefix != '') { |
|
| 203 | $options['Prefix'] = $this->computePath($prefix); |
||
| 204 | } elseif (!empty($this->options['directory'])) { |
||
| 205 | $options['Prefix'] = $this->options['directory']; |
||
| 206 | } |
||
| 207 | |||
| 208 | $keys = array(); |
||
| 209 | $iter = $this->service->getIterator('ListObjects', $options); |
||
| 210 | foreach ($iter as $file) { |
||
| 211 | $keys[] = $this->computeKey($file['Key']); |
||
| 212 | } |
||
| 213 | |||
| 214 | return $keys; |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * {@inheritdoc} |
||
| 219 | */ |
||
| 220 | public function delete($key) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * {@inheritdoc} |
||
| 233 | */ |
||
| 234 | public function isDirectory($key) |
||
| 235 | { |
||
| 236 | $result = $this->service->listObjects(array( |
||
| 237 | 'Bucket' => $this->bucket, |
||
| 238 | 'Prefix' => rtrim($this->computePath($key), '/').'/', |
||
| 239 | 'MaxKeys' => 1, |
||
| 240 | )); |
||
| 241 | |||
| 242 | return count($result['Contents']) > 0; |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Ensures the specified bucket exists. If the bucket does not exists |
||
| 247 | * and the create option is set to true, it will try to create the |
||
| 248 | * bucket. The bucket is created using the same region as the supplied |
||
| 249 | * client object. |
||
| 250 | * |
||
| 251 | * @throws \RuntimeException if the bucket does not exists or could not be |
||
| 252 | * created |
||
| 253 | */ |
||
| 254 | protected function ensureBucketExists() |
||
| 281 | |||
| 282 | protected function getOptions($key, array $options = array()) |
||
| 296 | |||
| 297 | View Code Duplication | protected function computePath($key) |
|
| 298 | { |
||
| 299 | if (empty($this->options['directory'])) { |
||
| 300 | return $key; |
||
| 301 | } |
||
| 302 | |||
| 303 | return sprintf('%s/%s', $this->options['directory'], $key); |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Computes the key from the specified path. |
||
| 308 | * |
||
| 309 | * @param string $path |
||
| 310 | * |
||
| 311 | * return string |
||
| 312 | */ |
||
| 313 | protected function computeKey($path) |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @param string $content |
||
| 320 | * |
||
| 321 | * @return string |
||
| 322 | */ |
||
| 323 | View Code Duplication | private function guessContentType($content) |
|
| 333 | } |
||
| 334 |