Complex classes like AwsS3Adapter 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 AwsS3Adapter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class AwsS3Adapter extends AbstractAdapter implements CanOverwriteFiles |
||
17 | { |
||
18 | const PUBLIC_GRANT_URI = 'http://acs.amazonaws.com/groups/global/AllUsers'; |
||
19 | |||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | protected static $resultMap = [ |
||
24 | 'Body' => 'contents', |
||
25 | 'ContentLength' => 'size', |
||
26 | 'ContentType' => 'mimetype', |
||
27 | 'Size' => 'size', |
||
28 | 'Metadata' => 'metadata', |
||
29 | 'StorageClass' => 'storageclass', |
||
30 | 'ETag' => 'etag', |
||
31 | 'VersionId' => 'versionid' |
||
32 | ]; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | protected static $metaOptions = [ |
||
38 | 'ACL', |
||
39 | 'CacheControl', |
||
40 | 'ContentDisposition', |
||
41 | 'ContentEncoding', |
||
42 | 'ContentLength', |
||
43 | 'ContentType', |
||
44 | 'Expires', |
||
45 | 'GrantFullControl', |
||
46 | 'GrantRead', |
||
47 | 'GrantReadACP', |
||
48 | 'GrantWriteACP', |
||
49 | 'Metadata', |
||
50 | 'RequestPayer', |
||
51 | 'SSECustomerAlgorithm', |
||
52 | 'SSECustomerKey', |
||
53 | 'SSECustomerKeyMD5', |
||
54 | 'SSEKMSKeyId', |
||
55 | 'ServerSideEncryption', |
||
56 | 'StorageClass', |
||
57 | 'Tagging', |
||
58 | 'WebsiteRedirectLocation', |
||
59 | ]; |
||
60 | |||
61 | /** |
||
62 | * @var S3Client |
||
63 | */ |
||
64 | protected $s3Client; |
||
65 | |||
66 | /** |
||
67 | * @var string |
||
68 | */ |
||
69 | protected $bucket; |
||
70 | |||
71 | /** |
||
72 | * @var array |
||
73 | */ |
||
74 | protected $options = []; |
||
75 | |||
76 | /** |
||
77 | * Constructor. |
||
78 | * |
||
79 | * @param S3Client $client |
||
80 | * @param string $bucket |
||
81 | * @param string $prefix |
||
82 | * @param array $options |
||
83 | */ |
||
84 | 76 | public function __construct(S3Client $client, $bucket, $prefix = '', array $options = []) |
|
91 | |||
92 | /** |
||
93 | * Get the S3Client bucket. |
||
94 | * |
||
95 | * @return string |
||
96 | */ |
||
97 | 4 | public function getBucket() |
|
101 | |||
102 | /** |
||
103 | * Set the S3Client bucket. |
||
104 | * |
||
105 | * @return string |
||
106 | */ |
||
107 | 2 | public function setBucket($bucket) |
|
111 | |||
112 | /** |
||
113 | * Get the S3Client instance. |
||
114 | * |
||
115 | * @return S3Client |
||
116 | */ |
||
117 | 2 | public function getClient() |
|
121 | |||
122 | /** |
||
123 | * Write a new file. |
||
124 | * |
||
125 | * @param string $path |
||
126 | * @param string $contents |
||
127 | * @param Config $config Config object |
||
128 | * |
||
129 | * @return false|array false on failure file meta data on success |
||
130 | */ |
||
131 | 6 | public function write($path, $contents, Config $config) |
|
135 | |||
136 | /** |
||
137 | * Update a file. |
||
138 | * |
||
139 | * @param string $path |
||
140 | * @param string $contents |
||
141 | * @param Config $config Config object |
||
142 | * |
||
143 | * @return false|array false on failure file meta data on success |
||
144 | */ |
||
145 | 4 | public function update($path, $contents, Config $config) |
|
149 | |||
150 | /** |
||
151 | * Rename a file. |
||
152 | * |
||
153 | * @param string $path |
||
154 | * @param string $newpath |
||
155 | * |
||
156 | * @return bool |
||
157 | */ |
||
158 | 4 | public function rename($path, $newpath) |
|
166 | |||
167 | /** |
||
168 | * Delete a file. |
||
169 | * |
||
170 | * @param string $path |
||
171 | * |
||
172 | * @return bool |
||
173 | */ |
||
174 | 4 | public function delete($path) |
|
175 | { |
||
176 | 4 | $location = $this->applyPathPrefix($path); |
|
177 | |||
178 | 4 | $command = $this->s3Client->getCommand( |
|
179 | 4 | 'deleteObject', |
|
180 | [ |
||
181 | 4 | 'Bucket' => $this->bucket, |
|
182 | 4 | 'Key' => $location, |
|
183 | ] |
||
184 | ); |
||
185 | |||
186 | 4 | $this->s3Client->execute($command); |
|
187 | |||
188 | 4 | return ! $this->has($path); |
|
189 | } |
||
190 | |||
191 | /** |
||
192 | * Delete a directory. |
||
193 | * |
||
194 | * @param string $dirname |
||
195 | * |
||
196 | * @return bool |
||
197 | */ |
||
198 | 4 | public function deleteDir($dirname) |
|
209 | |||
210 | /** |
||
211 | * Create a directory. |
||
212 | * |
||
213 | * @param string $dirname directory name |
||
214 | * @param Config $config |
||
215 | * |
||
216 | * @return bool|array |
||
217 | */ |
||
218 | 4 | public function createDir($dirname, Config $config) |
|
222 | |||
223 | /** |
||
224 | * Check whether a file exists. |
||
225 | * |
||
226 | * @param string $path |
||
227 | * |
||
228 | * @return bool |
||
229 | */ |
||
230 | 12 | public function has($path) |
|
240 | |||
241 | /** |
||
242 | * Read a file. |
||
243 | * |
||
244 | * @param string $path |
||
245 | * |
||
246 | * @return false|array |
||
247 | */ |
||
248 | 6 | public function read($path) |
|
258 | |||
259 | /** |
||
260 | * List contents of a directory. |
||
261 | * |
||
262 | * @param string $directory |
||
263 | * @param bool $recursive |
||
264 | * |
||
265 | * @return array |
||
266 | */ |
||
267 | 4 | public function listContents($directory = '', $recursive = false) |
|
282 | |||
283 | /** |
||
284 | * @param array $options |
||
285 | * |
||
286 | * @return array |
||
287 | */ |
||
288 | 4 | protected function retrievePaginatedListing(array $options) |
|
299 | |||
300 | /** |
||
301 | * Get all the meta data of a file or directory. |
||
302 | * |
||
303 | * @param string $path |
||
304 | * |
||
305 | * @return false|array |
||
306 | */ |
||
307 | 14 | public function getMetadata($path) |
|
330 | |||
331 | /** |
||
332 | * @return bool |
||
333 | */ |
||
334 | 4 | private function is404Exception(S3Exception $exception) |
|
344 | |||
345 | /** |
||
346 | * Get all the meta data of a file or directory. |
||
347 | * |
||
348 | * @param string $path |
||
349 | * |
||
350 | * @return false|array |
||
351 | */ |
||
352 | 2 | public function getSize($path) |
|
356 | |||
357 | /** |
||
358 | * Get the mimetype of a file. |
||
359 | * |
||
360 | * @param string $path |
||
361 | * |
||
362 | * @return false|array |
||
363 | */ |
||
364 | 2 | public function getMimetype($path) |
|
368 | |||
369 | /** |
||
370 | * Get the timestamp of a file. |
||
371 | * |
||
372 | * @param string $path |
||
373 | * |
||
374 | * @return false|array |
||
375 | */ |
||
376 | 2 | public function getTimestamp($path) |
|
380 | |||
381 | /** |
||
382 | * Write a new file using a stream. |
||
383 | * |
||
384 | * @param string $path |
||
385 | * @param resource $resource |
||
386 | * @param Config $config Config object |
||
387 | * |
||
388 | * @return array|false false on failure file meta data on success |
||
389 | */ |
||
390 | 4 | public function writeStream($path, $resource, Config $config) |
|
394 | |||
395 | /** |
||
396 | * Update a file using a stream. |
||
397 | * |
||
398 | * @param string $path |
||
399 | * @param resource $resource |
||
400 | * @param Config $config Config object |
||
401 | * |
||
402 | * @return array|false false on failure file meta data on success |
||
403 | */ |
||
404 | 4 | public function updateStream($path, $resource, Config $config) |
|
408 | |||
409 | /** |
||
410 | * Copy a file. |
||
411 | * |
||
412 | * @param string $path |
||
413 | * @param string $newpath |
||
414 | * |
||
415 | * @return bool |
||
416 | */ |
||
417 | 8 | public function copy($path, $newpath) |
|
438 | |||
439 | /** |
||
440 | * Read a file as a stream. |
||
441 | * |
||
442 | * @param string $path |
||
443 | * |
||
444 | * @return array|false |
||
445 | */ |
||
446 | 6 | public function readStream($path) |
|
457 | |||
458 | /** |
||
459 | * Read an object and normalize the response. |
||
460 | * |
||
461 | * @param $path |
||
462 | * |
||
463 | * @return array|bool |
||
464 | */ |
||
465 | 10 | protected function readObject($path) |
|
487 | |||
488 | /** |
||
489 | * Set the visibility for a file. |
||
490 | * |
||
491 | * @param string $path |
||
492 | * @param string $visibility |
||
493 | * |
||
494 | * @return array|false file meta data |
||
495 | */ |
||
496 | 6 | public function setVisibility($path, $visibility) |
|
515 | |||
516 | /** |
||
517 | * Get the visibility of a file. |
||
518 | * |
||
519 | * @param string $path |
||
520 | * |
||
521 | * @return array|false |
||
522 | */ |
||
523 | 4 | public function getVisibility($path) |
|
527 | |||
528 | /** |
||
529 | * {@inheritdoc} |
||
530 | */ |
||
531 | 68 | public function applyPathPrefix($path) |
|
535 | |||
536 | /** |
||
537 | * {@inheritdoc} |
||
538 | */ |
||
539 | 76 | public function setPathPrefix($prefix) |
|
545 | |||
546 | /** |
||
547 | * Get the object acl presented as a visibility. |
||
548 | * |
||
549 | * @param string $path |
||
550 | * |
||
551 | * @return string |
||
552 | */ |
||
553 | 12 | protected function getRawVisibility($path) |
|
579 | |||
580 | /** |
||
581 | * Upload an object. |
||
582 | * |
||
583 | * @param $path |
||
584 | * @param $body |
||
585 | * @param Config $config |
||
586 | * |
||
587 | * @return array|bool |
||
588 | */ |
||
589 | 14 | protected function upload($path, $body, Config $config) |
|
615 | |||
616 | /** |
||
617 | * Get options from the config. |
||
618 | * |
||
619 | * @param Config $config |
||
620 | * |
||
621 | * @return array |
||
622 | */ |
||
623 | 14 | protected function getOptionsFromConfig(Config $config) |
|
650 | |||
651 | /** |
||
652 | * Normalize the object result array. |
||
653 | * |
||
654 | * @param array $response |
||
655 | * @param string $path |
||
656 | * |
||
657 | * @return array |
||
658 | */ |
||
659 | 28 | protected function normalizeResponse(array $response, $path = null) |
|
681 | |||
682 | /** |
||
683 | * @param $location |
||
684 | * |
||
685 | * @return bool |
||
686 | */ |
||
687 | 10 | protected function doesDirectoryExist($location) |
|
712 | } |
||
713 |