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()) |
||
64 | |||
65 | /** |
||
66 | * Gets the pre-authorized URL of an Amazon S3 object |
||
67 | * |
||
68 | * @param string $key Object key |
||
69 | * @param int|string|\DateTime $expires The time at which the URL should |
||
70 | * expire. This can be a Unix timestamp, a PHP DateTime object, or a |
||
71 | * string that can be evaluated by strtotime. |
||
72 | * @param array $parameters Optional parameters. |
||
73 | * Exempli gratia: |
||
74 | * - ['ResponseContentType' => 'application/pdf'] |
||
75 | * to force Response Content-Type header to "application/pdf" |
||
76 | * - ['ResponseContentDisposition' => 'attachment; filename="file.pdf"'] |
||
77 | * to force resource download using a particular file-name |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | public function getAuthUrl($key, $expires = '+15 minutes', $parameters = []) |
||
98 | |||
99 | /** |
||
100 | * {@inheritdoc} |
||
101 | */ |
||
102 | public function setMetadata($key, $metadata) |
||
112 | |||
113 | /** |
||
114 | * {@inheritdoc} |
||
115 | */ |
||
116 | public function getMetadata($key) |
||
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | public function read($key) |
||
135 | |||
136 | /** |
||
137 | * {@inheritdoc} |
||
138 | */ |
||
139 | public function rename($sourceKey, $targetKey) |
||
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | public function write($key, $content) |
||
191 | |||
192 | /** |
||
193 | * {@inheritdoc} |
||
194 | */ |
||
195 | public function exists($key) |
||
199 | |||
200 | /** |
||
201 | * {@inheritdoc} |
||
202 | */ |
||
203 | View Code Duplication | public function mtime($key) |
|
213 | |||
214 | /** |
||
215 | * {@inheritdoc} |
||
216 | */ |
||
217 | View Code Duplication | public function size($key) |
|
227 | |||
228 | /** |
||
229 | * {@inheritdoc} |
||
230 | */ |
||
231 | public function keys() |
||
235 | |||
236 | /** |
||
237 | * {@inheritdoc} |
||
238 | */ |
||
239 | public function listKeys($prefix = '') |
||
256 | |||
257 | /** |
||
258 | * {@inheritdoc} |
||
259 | */ |
||
260 | public function delete($key) |
||
270 | |||
271 | /** |
||
272 | * {@inheritdoc} |
||
273 | */ |
||
274 | public function isDirectory($key) |
||
284 | |||
285 | /** |
||
286 | * Ensures the specified bucket exists. If the bucket does not exists |
||
287 | * and the create option is set to true, it will try to create the |
||
288 | * bucket. The bucket is created using the same region as the supplied |
||
289 | * client object. |
||
290 | * |
||
291 | * @throws \RuntimeException if the bucket does not exists or could not be |
||
292 | * created |
||
293 | */ |
||
294 | protected function ensureBucketExists() |
||
321 | |||
322 | protected function getOptions($key, array $options = array()) |
||
336 | |||
337 | View Code Duplication | protected function computePath($key) |
|
345 | |||
346 | /** |
||
347 | * Computes the key from the specified path. |
||
348 | * |
||
349 | * @param string $path |
||
350 | * |
||
351 | * return string |
||
352 | */ |
||
353 | protected function computeKey($path) |
||
357 | } |
||
358 |