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 | 64 | 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 | 8 | public function has($path) |
|
236 | |||
237 | /** |
||
238 | * Read a file. |
||
239 | * |
||
240 | * @param string $path |
||
241 | * |
||
242 | * @return false|array |
||
243 | */ |
||
244 | 4 | public function read($path) |
|
254 | |||
255 | /** |
||
256 | * List contents of a directory. |
||
257 | * |
||
258 | * @param string $directory |
||
259 | * @param bool $recursive |
||
260 | * |
||
261 | * @return array |
||
262 | */ |
||
263 | 2 | public function listContents($directory = '', $recursive = false) |
|
278 | |||
279 | /** |
||
280 | * @param array $options |
||
281 | * |
||
282 | * @return array |
||
283 | */ |
||
284 | 2 | protected function retrievePaginatedListing(array $options) |
|
295 | |||
296 | /** |
||
297 | * Get all the meta data of a file or directory. |
||
298 | * |
||
299 | * @param string $path |
||
300 | * |
||
301 | * @return false|array |
||
302 | */ |
||
303 | 12 | public function getMetadata($path) |
|
328 | |||
329 | /** |
||
330 | * Get all the meta data of a file or directory. |
||
331 | * |
||
332 | * @param string $path |
||
333 | * |
||
334 | * @return false|array |
||
335 | */ |
||
336 | 2 | public function getSize($path) |
|
340 | |||
341 | /** |
||
342 | * Get the mimetype of a file. |
||
343 | * |
||
344 | * @param string $path |
||
345 | * |
||
346 | * @return false|array |
||
347 | */ |
||
348 | 2 | public function getMimetype($path) |
|
352 | |||
353 | /** |
||
354 | * Get the timestamp of a file. |
||
355 | * |
||
356 | * @param string $path |
||
357 | * |
||
358 | * @return false|array |
||
359 | */ |
||
360 | 2 | public function getTimestamp($path) |
|
364 | |||
365 | /** |
||
366 | * Write a new file using a stream. |
||
367 | * |
||
368 | * @param string $path |
||
369 | * @param resource $resource |
||
370 | * @param Config $config Config object |
||
371 | * |
||
372 | * @return array|false false on failure file meta data on success |
||
373 | */ |
||
374 | 2 | public function writeStream($path, $resource, Config $config) |
|
378 | |||
379 | /** |
||
380 | * Update a file using a stream. |
||
381 | * |
||
382 | * @param string $path |
||
383 | * @param resource $resource |
||
384 | * @param Config $config Config object |
||
385 | * |
||
386 | * @return array|false false on failure file meta data on success |
||
387 | */ |
||
388 | 2 | public function updateStream($path, $resource, Config $config) |
|
392 | |||
393 | /** |
||
394 | * Copy a file. |
||
395 | * |
||
396 | * @param string $path |
||
397 | * @param string $newpath |
||
398 | * |
||
399 | * @return bool |
||
400 | */ |
||
401 | 8 | public function copy($path, $newpath) |
|
423 | |||
424 | /** |
||
425 | * Read a file as a stream. |
||
426 | * |
||
427 | * @param string $path |
||
428 | * |
||
429 | * @return array|false |
||
430 | */ |
||
431 | 2 | public function readStream($path) |
|
443 | |||
444 | /** |
||
445 | * Read an object and normalize the response. |
||
446 | * |
||
447 | * @param $path |
||
448 | * |
||
449 | * @return array|bool |
||
450 | */ |
||
451 | 6 | protected function readObject($path) |
|
470 | |||
471 | /** |
||
472 | * Set the visibility for a file. |
||
473 | * |
||
474 | * @param string $path |
||
475 | * @param string $visibility |
||
476 | * |
||
477 | * @return array|false file meta data |
||
478 | */ |
||
479 | 6 | public function setVisibility($path, $visibility) |
|
498 | |||
499 | /** |
||
500 | * Get the visibility of a file. |
||
501 | * |
||
502 | * @param string $path |
||
503 | * |
||
504 | * @return array|false |
||
505 | */ |
||
506 | 4 | public function getVisibility($path) |
|
510 | |||
511 | /** |
||
512 | * {@inheritdoc} |
||
513 | */ |
||
514 | 58 | public function applyPathPrefix($prefix) |
|
518 | |||
519 | /** |
||
520 | * {@inheritdoc} |
||
521 | */ |
||
522 | 64 | public function setPathPrefix($prefix) |
|
528 | |||
529 | /** |
||
530 | * Get the object acl presented as a visibility. |
||
531 | * |
||
532 | * @param string $path |
||
533 | * |
||
534 | * @return string |
||
535 | */ |
||
536 | 12 | protected function getRawVisibility($path) |
|
562 | |||
563 | /** |
||
564 | * Upload an object. |
||
565 | * |
||
566 | * @param $path |
||
567 | * @param $body |
||
568 | * @param Config $config |
||
569 | * |
||
570 | * @return array |
||
571 | */ |
||
572 | 10 | protected function upload($path, $body, Config $config) |
|
590 | |||
591 | /** |
||
592 | * Get options from the config. |
||
593 | * |
||
594 | * @param Config $config |
||
595 | * |
||
596 | * @return array |
||
597 | */ |
||
598 | 10 | protected function getOptionsFromConfig(Config $config) |
|
625 | |||
626 | /** |
||
627 | * Normalize the object result array. |
||
628 | * |
||
629 | * @param array $response |
||
630 | * @param string $path |
||
631 | * |
||
632 | * @return array |
||
633 | */ |
||
634 | 22 | protected function normalizeResponse(array $response, $path = null) |
|
656 | } |
||
657 |