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 |
||
14 | class AwsS3Adapter extends AbstractAdapter |
||
15 | { |
||
16 | const PUBLIC_GRANT_URI = 'http://acs.amazonaws.com/groups/global/AllUsers'; |
||
17 | |||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | protected static $resultMap = [ |
||
22 | 'Body' => 'contents', |
||
23 | 'ContentLength' => 'size', |
||
24 | 'ContentType' => 'mimetype', |
||
25 | 'Size' => 'size', |
||
26 | ]; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | protected static $metaOptions = [ |
||
32 | 'CacheControl', |
||
33 | 'Expires', |
||
34 | 'StorageClass', |
||
35 | 'ServerSideEncryption', |
||
36 | 'Metadata', |
||
37 | 'ACL', |
||
38 | 'ContentType', |
||
39 | 'ContentEncoding', |
||
40 | 'ContentDisposition', |
||
41 | 'ContentLength', |
||
42 | ]; |
||
43 | |||
44 | /** |
||
45 | * @var S3Client |
||
46 | */ |
||
47 | protected $s3Client; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $bucket; |
||
53 | |||
54 | /** |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $options = []; |
||
58 | |||
59 | /** |
||
60 | * Constructor. |
||
61 | * |
||
62 | * @param S3Client $client |
||
63 | * @param string $bucket |
||
64 | * @param string $prefix |
||
65 | * @param array $options |
||
66 | */ |
||
67 | 62 | public function __construct(S3Client $client, $bucket, $prefix = '', array $options = []) |
|
74 | |||
75 | /** |
||
76 | * Get the S3Client bucket. |
||
77 | * |
||
78 | * @return string |
||
79 | */ |
||
80 | 2 | public function getBucket() |
|
84 | |||
85 | /** |
||
86 | * Get the S3Client instance. |
||
87 | * |
||
88 | * @return S3Client |
||
89 | */ |
||
90 | 2 | public function getClient() |
|
94 | |||
95 | /** |
||
96 | * Write a new file. |
||
97 | * |
||
98 | * @param string $path |
||
99 | * @param string $contents |
||
100 | * @param Config $config Config object |
||
101 | * |
||
102 | * @return false|array false on failure file meta data on success |
||
103 | */ |
||
104 | 2 | public function write($path, $contents, Config $config) |
|
108 | |||
109 | /** |
||
110 | * Update a file. |
||
111 | * |
||
112 | * @param string $path |
||
113 | * @param string $contents |
||
114 | * @param Config $config Config object |
||
115 | * |
||
116 | * @return false|array false on failure file meta data on success |
||
117 | */ |
||
118 | 2 | public function update($path, $contents, Config $config) |
|
122 | |||
123 | /** |
||
124 | * Rename a file. |
||
125 | * |
||
126 | * @param string $path |
||
127 | * @param string $newpath |
||
128 | * |
||
129 | * @return bool |
||
130 | */ |
||
131 | 4 | public function rename($path, $newpath) |
|
139 | |||
140 | /** |
||
141 | * Delete a file. |
||
142 | * |
||
143 | * @param string $path |
||
144 | * |
||
145 | * @return bool |
||
146 | */ |
||
147 | 4 | public function delete($path) |
|
163 | |||
164 | /** |
||
165 | * Delete a directory. |
||
166 | * |
||
167 | * @param string $dirname |
||
168 | * |
||
169 | * @return bool |
||
170 | */ |
||
171 | 4 | public function deleteDir($dirname) |
|
182 | |||
183 | /** |
||
184 | * Create a directory. |
||
185 | * |
||
186 | * @param string $dirname directory name |
||
187 | * @param Config $config |
||
188 | * |
||
189 | * @return bool|array |
||
190 | */ |
||
191 | 2 | public function createDir($dirname, Config $config) |
|
195 | |||
196 | /** |
||
197 | * Check whether a file exists. |
||
198 | * |
||
199 | * @param string $path |
||
200 | * |
||
201 | * @return bool |
||
202 | */ |
||
203 | 6 | public function has($path) |
|
209 | |||
210 | /** |
||
211 | * Read a file. |
||
212 | * |
||
213 | * @param string $path |
||
214 | * |
||
215 | * @return false|array |
||
216 | */ |
||
217 | 4 | public function read($path) |
|
227 | |||
228 | /** |
||
229 | * List contents of a directory. |
||
230 | * |
||
231 | * @param string $directory |
||
232 | * @param bool $recursive |
||
233 | * |
||
234 | * @return array |
||
235 | */ |
||
236 | 2 | public function listContents($directory = '', $recursive = false) |
|
251 | |||
252 | /** |
||
253 | * @param array $options |
||
254 | * |
||
255 | * @return array |
||
256 | */ |
||
257 | 2 | protected function retrievePaginatedListing(array $options) |
|
268 | |||
269 | /** |
||
270 | * Get all the meta data of a file or directory. |
||
271 | * |
||
272 | * @param string $path |
||
273 | * |
||
274 | * @return false|array |
||
275 | */ |
||
276 | 12 | public function getMetadata($path) |
|
301 | |||
302 | /** |
||
303 | * Get all the meta data of a file or directory. |
||
304 | * |
||
305 | * @param string $path |
||
306 | * |
||
307 | * @return false|array |
||
308 | */ |
||
309 | 2 | public function getSize($path) |
|
313 | |||
314 | /** |
||
315 | * Get the mimetype of a file. |
||
316 | * |
||
317 | * @param string $path |
||
318 | * |
||
319 | * @return false|array |
||
320 | */ |
||
321 | 2 | public function getMimetype($path) |
|
325 | |||
326 | /** |
||
327 | * Get the timestamp of a file. |
||
328 | * |
||
329 | * @param string $path |
||
330 | * |
||
331 | * @return false|array |
||
332 | */ |
||
333 | 2 | public function getTimestamp($path) |
|
337 | |||
338 | /** |
||
339 | * Write a new file using a stream. |
||
340 | * |
||
341 | * @param string $path |
||
342 | * @param resource $resource |
||
343 | * @param Config $config Config object |
||
344 | * |
||
345 | * @return array|false false on failure file meta data on success |
||
346 | */ |
||
347 | 2 | public function writeStream($path, $resource, Config $config) |
|
351 | |||
352 | /** |
||
353 | * Update a file using a stream. |
||
354 | * |
||
355 | * @param string $path |
||
356 | * @param resource $resource |
||
357 | * @param Config $config Config object |
||
358 | * |
||
359 | * @return array|false false on failure file meta data on success |
||
360 | */ |
||
361 | 2 | public function updateStream($path, $resource, Config $config) |
|
365 | |||
366 | /** |
||
367 | * Copy a file. |
||
368 | * |
||
369 | * @param string $path |
||
370 | * @param string $newpath |
||
371 | * |
||
372 | * @return bool |
||
373 | */ |
||
374 | 8 | public function copy($path, $newpath) |
|
396 | |||
397 | /** |
||
398 | * Read a file as a stream. |
||
399 | * |
||
400 | * @param string $path |
||
401 | * |
||
402 | * @return array|false |
||
403 | */ |
||
404 | 2 | public function readStream($path) |
|
416 | |||
417 | /** |
||
418 | * Read an object and normalize the response. |
||
419 | * |
||
420 | * @param $path |
||
421 | * |
||
422 | * @return array|bool |
||
423 | */ |
||
424 | 6 | protected function readObject($path) |
|
443 | |||
444 | /** |
||
445 | * Set the visibility for a file. |
||
446 | * |
||
447 | * @param string $path |
||
448 | * @param string $visibility |
||
449 | * |
||
450 | * @return array|false file meta data |
||
451 | */ |
||
452 | 6 | public function setVisibility($path, $visibility) |
|
471 | |||
472 | /** |
||
473 | * Get the visibility of a file. |
||
474 | * |
||
475 | * @param string $path |
||
476 | * |
||
477 | * @return array|false |
||
478 | */ |
||
479 | 4 | public function getVisibility($path) |
|
483 | |||
484 | /** |
||
485 | * {@inheritdoc} |
||
486 | */ |
||
487 | 56 | public function applyPathPrefix($prefix) |
|
491 | |||
492 | /** |
||
493 | * {@inheritdoc} |
||
494 | */ |
||
495 | 62 | public function setPathPrefix($prefix) |
|
501 | |||
502 | /** |
||
503 | * Get the object acl presented as a visibility. |
||
504 | * |
||
505 | * @param string $path |
||
506 | * |
||
507 | * @return string |
||
508 | */ |
||
509 | 12 | protected function getRawVisibility($path) |
|
535 | |||
536 | /** |
||
537 | * Upload an object. |
||
538 | * |
||
539 | * @param $path |
||
540 | * @param $body |
||
541 | * @param Config $config |
||
542 | * |
||
543 | * @return array |
||
544 | */ |
||
545 | 10 | protected function upload($path, $body, Config $config) |
|
563 | |||
564 | /** |
||
565 | * Get options from the config. |
||
566 | * |
||
567 | * @param Config $config |
||
568 | * |
||
569 | * @return array |
||
570 | */ |
||
571 | 10 | protected function getOptionsFromConfig(Config $config) |
|
598 | |||
599 | /** |
||
600 | * Normalize the object result array. |
||
601 | * |
||
602 | * @param array $response |
||
603 | * @param string $path |
||
604 | * |
||
605 | * @return array |
||
606 | */ |
||
607 | 22 | protected function normalizeResponse(array $response, $path = null) |
|
629 | } |
||
630 |