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 |
||
15 | class AwsS3Adapter extends AbstractAdapter implements CanOverwriteFiles |
||
16 | { |
||
17 | const PUBLIC_GRANT_URI = 'http://acs.amazonaws.com/groups/global/AllUsers'; |
||
18 | |||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | protected static $resultMap = [ |
||
23 | 'Body' => 'contents', |
||
24 | 'ContentLength' => 'size', |
||
25 | 'ContentType' => 'mimetype', |
||
26 | 'Size' => 'size', |
||
27 | 'Metadata' => 'metadata', |
||
28 | ]; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | protected static $metaOptions = [ |
||
34 | 'CacheControl', |
||
35 | 'Expires', |
||
36 | 'StorageClass', |
||
37 | 'ServerSideEncryption', |
||
38 | 'Metadata', |
||
39 | 'ACL', |
||
40 | 'ContentType', |
||
41 | 'ContentEncoding', |
||
42 | 'ContentDisposition', |
||
43 | 'ContentLength', |
||
44 | 'Tagging', |
||
45 | 'WebsiteRedirectLocation', |
||
46 | 'SSEKMSKeyId' |
||
47 | ]; |
||
48 | |||
49 | /** |
||
50 | * @var S3Client |
||
51 | */ |
||
52 | protected $s3Client; |
||
53 | |||
54 | /** |
||
55 | * @var string |
||
56 | */ |
||
57 | protected $bucket; |
||
58 | |||
59 | /** |
||
60 | * @var array |
||
61 | */ |
||
62 | protected $options = []; |
||
63 | |||
64 | /** |
||
65 | * Constructor. |
||
66 | * |
||
67 | * @param S3Client $client |
||
68 | * @param string $bucket |
||
69 | * @param string $prefix |
||
70 | * @param array $options |
||
71 | */ |
||
72 | 72 | public function __construct(S3Client $client, $bucket, $prefix = '', array $options = []) |
|
79 | |||
80 | /** |
||
81 | * Get the S3Client bucket. |
||
82 | * |
||
83 | * @return string |
||
84 | */ |
||
85 | 4 | public function getBucket() |
|
89 | |||
90 | /** |
||
91 | * Set the S3Client bucket. |
||
92 | * |
||
93 | * @return string |
||
94 | */ |
||
95 | 2 | public function setBucket($bucket) |
|
99 | |||
100 | /** |
||
101 | * Get the S3Client instance. |
||
102 | * |
||
103 | * @return S3Client |
||
104 | */ |
||
105 | 2 | public function getClient() |
|
109 | |||
110 | /** |
||
111 | * Write a new file. |
||
112 | * |
||
113 | * @param string $path |
||
114 | * @param string $contents |
||
115 | * @param Config $config Config object |
||
116 | * |
||
117 | * @return false|array false on failure file meta data on success |
||
118 | */ |
||
119 | 2 | public function write($path, $contents, Config $config) |
|
123 | |||
124 | /** |
||
125 | * Update a file. |
||
126 | * |
||
127 | * @param string $path |
||
128 | * @param string $contents |
||
129 | * @param Config $config Config object |
||
130 | * |
||
131 | * @return false|array false on failure file meta data on success |
||
132 | */ |
||
133 | 2 | public function update($path, $contents, Config $config) |
|
137 | |||
138 | /** |
||
139 | * Rename a file. |
||
140 | * |
||
141 | * @param string $path |
||
142 | * @param string $newpath |
||
143 | * |
||
144 | * @return bool |
||
145 | */ |
||
146 | 4 | public function rename($path, $newpath) |
|
154 | |||
155 | /** |
||
156 | * Delete a file. |
||
157 | * |
||
158 | * @param string $path |
||
159 | * |
||
160 | * @return bool |
||
161 | */ |
||
162 | 4 | public function delete($path) |
|
178 | |||
179 | /** |
||
180 | * Delete a directory. |
||
181 | * |
||
182 | * @param string $dirname |
||
183 | * |
||
184 | * @return bool |
||
185 | */ |
||
186 | 4 | public function deleteDir($dirname) |
|
197 | |||
198 | /** |
||
199 | * Create a directory. |
||
200 | * |
||
201 | * @param string $dirname directory name |
||
202 | * @param Config $config |
||
203 | * |
||
204 | * @return bool|array |
||
205 | */ |
||
206 | 2 | public function createDir($dirname, Config $config) |
|
210 | |||
211 | /** |
||
212 | * Check whether a file exists. |
||
213 | * |
||
214 | * @param string $path |
||
215 | * |
||
216 | * @return bool |
||
217 | */ |
||
218 | 12 | public function has($path) |
|
228 | |||
229 | /** |
||
230 | * Read a file. |
||
231 | * |
||
232 | * @param string $path |
||
233 | * |
||
234 | * @return false|array |
||
235 | */ |
||
236 | 4 | public function read($path) |
|
246 | |||
247 | /** |
||
248 | * List contents of a directory. |
||
249 | * |
||
250 | * @param string $directory |
||
251 | * @param bool $recursive |
||
252 | * |
||
253 | * @return array |
||
254 | */ |
||
255 | 2 | public function listContents($directory = '', $recursive = false) |
|
270 | |||
271 | /** |
||
272 | * @param array $options |
||
273 | * |
||
274 | * @return array |
||
275 | */ |
||
276 | 2 | protected function retrievePaginatedListing(array $options) |
|
287 | |||
288 | /** |
||
289 | * Get all the meta data of a file or directory. |
||
290 | * |
||
291 | * @param string $path |
||
292 | * |
||
293 | * @return false|array |
||
294 | */ |
||
295 | 12 | public function getMetadata($path) |
|
320 | |||
321 | /** |
||
322 | * Get all the meta data of a file or directory. |
||
323 | * |
||
324 | * @param string $path |
||
325 | * |
||
326 | * @return false|array |
||
327 | */ |
||
328 | 2 | public function getSize($path) |
|
332 | |||
333 | /** |
||
334 | * Get the mimetype of a file. |
||
335 | * |
||
336 | * @param string $path |
||
337 | * |
||
338 | * @return false|array |
||
339 | */ |
||
340 | 2 | public function getMimetype($path) |
|
344 | |||
345 | /** |
||
346 | * Get the timestamp of a file. |
||
347 | * |
||
348 | * @param string $path |
||
349 | * |
||
350 | * @return false|array |
||
351 | */ |
||
352 | 2 | public function getTimestamp($path) |
|
356 | |||
357 | /** |
||
358 | * Write a new file using a stream. |
||
359 | * |
||
360 | * @param string $path |
||
361 | * @param resource $resource |
||
362 | * @param Config $config Config object |
||
363 | * |
||
364 | * @return array|false false on failure file meta data on success |
||
365 | */ |
||
366 | 2 | public function writeStream($path, $resource, Config $config) |
|
370 | |||
371 | /** |
||
372 | * Update a file using a stream. |
||
373 | * |
||
374 | * @param string $path |
||
375 | * @param resource $resource |
||
376 | * @param Config $config Config object |
||
377 | * |
||
378 | * @return array|false false on failure file meta data on success |
||
379 | */ |
||
380 | 2 | public function updateStream($path, $resource, Config $config) |
|
384 | |||
385 | /** |
||
386 | * Copy a file. |
||
387 | * |
||
388 | * @param string $path |
||
389 | * @param string $newpath |
||
390 | * |
||
391 | * @return bool |
||
392 | */ |
||
393 | 8 | public function copy($path, $newpath) |
|
414 | |||
415 | /** |
||
416 | * Read a file as a stream. |
||
417 | * |
||
418 | * @param string $path |
||
419 | * |
||
420 | * @return array|false |
||
421 | */ |
||
422 | 4 | public function readStream($path) |
|
433 | |||
434 | /** |
||
435 | * Read an object and normalize the response. |
||
436 | * |
||
437 | * @param $path |
||
438 | * |
||
439 | * @return array|bool |
||
440 | */ |
||
441 | 8 | protected function readObject($path) |
|
463 | |||
464 | /** |
||
465 | * Set the visibility for a file. |
||
466 | * |
||
467 | * @param string $path |
||
468 | * @param string $visibility |
||
469 | * |
||
470 | * @return array|false file meta data |
||
471 | */ |
||
472 | 6 | public function setVisibility($path, $visibility) |
|
491 | |||
492 | /** |
||
493 | * Get the visibility of a file. |
||
494 | * |
||
495 | * @param string $path |
||
496 | * |
||
497 | * @return array|false |
||
498 | */ |
||
499 | 4 | public function getVisibility($path) |
|
503 | |||
504 | /** |
||
505 | * {@inheritdoc} |
||
506 | */ |
||
507 | 64 | public function applyPathPrefix($path) |
|
511 | |||
512 | /** |
||
513 | * {@inheritdoc} |
||
514 | */ |
||
515 | 72 | public function setPathPrefix($prefix) |
|
521 | |||
522 | /** |
||
523 | * Get the object acl presented as a visibility. |
||
524 | * |
||
525 | * @param string $path |
||
526 | * |
||
527 | * @return string |
||
528 | */ |
||
529 | 12 | protected function getRawVisibility($path) |
|
555 | |||
556 | /** |
||
557 | * Upload an object. |
||
558 | * |
||
559 | * @param $path |
||
560 | * @param $body |
||
561 | * @param Config $config |
||
562 | * |
||
563 | * @return array |
||
564 | */ |
||
565 | 10 | protected function upload($path, $body, Config $config) |
|
587 | |||
588 | /** |
||
589 | * Get options from the config. |
||
590 | * |
||
591 | * @param Config $config |
||
592 | * |
||
593 | * @return array |
||
594 | */ |
||
595 | 10 | protected function getOptionsFromConfig(Config $config) |
|
622 | |||
623 | /** |
||
624 | * Normalize the object result array. |
||
625 | * |
||
626 | * @param array $response |
||
627 | * @param string $path |
||
628 | * |
||
629 | * @return array |
||
630 | */ |
||
631 | 24 | protected function normalizeResponse(array $response, $path = null) |
|
653 | |||
654 | /** |
||
655 | * @param $location |
||
656 | * |
||
657 | * @return bool |
||
658 | */ |
||
659 | 10 | protected function doesDirectoryExist($location) |
|
684 | } |
||
685 |