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 | 'ACL', |
||
35 | 'CacheControl', |
||
36 | 'ContentDisposition', |
||
37 | 'ContentEncoding', |
||
38 | 'ContentLength', |
||
39 | 'ContentType', |
||
40 | 'Expires', |
||
41 | 'GrantFullControl', |
||
42 | 'GrantRead', |
||
43 | 'GrantReadACP', |
||
44 | 'GrantWriteACP', |
||
45 | 'Metadata', |
||
46 | 'RequestPayer', |
||
47 | 'SSECustomerAlgorithm', |
||
48 | 'SSECustomerKey', |
||
49 | 'SSECustomerKeyMD5', |
||
50 | 'SSEKMSKeyId', |
||
51 | 'ServerSideEncryption', |
||
52 | 'StorageClass', |
||
53 | 'Tagging', |
||
54 | 'WebsiteRedirectLocation' |
||
55 | ]; |
||
56 | |||
57 | /** |
||
58 | * @var S3Client |
||
59 | */ |
||
60 | protected $s3Client; |
||
61 | |||
62 | /** |
||
63 | * @var string |
||
64 | */ |
||
65 | protected $bucket; |
||
66 | |||
67 | /** |
||
68 | * @var array |
||
69 | */ |
||
70 | protected $options = []; |
||
71 | |||
72 | /** |
||
73 | * Constructor. |
||
74 | * |
||
75 | * @param S3Client $client |
||
76 | * @param string $bucket |
||
77 | * @param string $prefix |
||
78 | * @param array $options |
||
79 | */ |
||
80 | 72 | public function __construct(S3Client $client, $bucket, $prefix = '', array $options = []) |
|
87 | |||
88 | /** |
||
89 | * Get the S3Client bucket. |
||
90 | * |
||
91 | * @return string |
||
92 | */ |
||
93 | 4 | public function getBucket() |
|
97 | |||
98 | /** |
||
99 | * Set the S3Client bucket. |
||
100 | * |
||
101 | * @return string |
||
102 | */ |
||
103 | 2 | public function setBucket($bucket) |
|
107 | |||
108 | /** |
||
109 | * Get the S3Client instance. |
||
110 | * |
||
111 | * @return S3Client |
||
112 | */ |
||
113 | 2 | public function getClient() |
|
117 | |||
118 | /** |
||
119 | * Write a new file. |
||
120 | * |
||
121 | * @param string $path |
||
122 | * @param string $contents |
||
123 | * @param Config $config Config object |
||
124 | * |
||
125 | * @return false|array false on failure file meta data on success |
||
126 | */ |
||
127 | 2 | public function write($path, $contents, Config $config) |
|
131 | |||
132 | /** |
||
133 | * Update a file. |
||
134 | * |
||
135 | * @param string $path |
||
136 | * @param string $contents |
||
137 | * @param Config $config Config object |
||
138 | * |
||
139 | * @return false|array false on failure file meta data on success |
||
140 | */ |
||
141 | 2 | public function update($path, $contents, Config $config) |
|
145 | |||
146 | /** |
||
147 | * Rename a file. |
||
148 | * |
||
149 | * @param string $path |
||
150 | * @param string $newpath |
||
151 | * |
||
152 | * @return bool |
||
153 | */ |
||
154 | 4 | public function rename($path, $newpath) |
|
162 | |||
163 | /** |
||
164 | * Delete a file. |
||
165 | * |
||
166 | * @param string $path |
||
167 | * |
||
168 | * @return bool |
||
169 | */ |
||
170 | 4 | public function delete($path) |
|
186 | |||
187 | /** |
||
188 | * Delete a directory. |
||
189 | * |
||
190 | * @param string $dirname |
||
191 | * |
||
192 | * @return bool |
||
193 | */ |
||
194 | 4 | public function deleteDir($dirname) |
|
205 | |||
206 | /** |
||
207 | * Create a directory. |
||
208 | * |
||
209 | * @param string $dirname directory name |
||
210 | * @param Config $config |
||
211 | * |
||
212 | * @return bool|array |
||
213 | */ |
||
214 | 2 | public function createDir($dirname, Config $config) |
|
218 | |||
219 | /** |
||
220 | * Check whether a file exists. |
||
221 | * |
||
222 | * @param string $path |
||
223 | * |
||
224 | * @return bool |
||
225 | */ |
||
226 | 12 | 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) |
|
422 | |||
423 | /** |
||
424 | * Read a file as a stream. |
||
425 | * |
||
426 | * @param string $path |
||
427 | * |
||
428 | * @return array|false |
||
429 | */ |
||
430 | 4 | public function readStream($path) |
|
441 | |||
442 | /** |
||
443 | * Read an object and normalize the response. |
||
444 | * |
||
445 | * @param $path |
||
446 | * |
||
447 | * @return array|bool |
||
448 | */ |
||
449 | 8 | protected function readObject($path) |
|
471 | |||
472 | /** |
||
473 | * Set the visibility for a file. |
||
474 | * |
||
475 | * @param string $path |
||
476 | * @param string $visibility |
||
477 | * |
||
478 | * @return array|false file meta data |
||
479 | */ |
||
480 | 6 | public function setVisibility($path, $visibility) |
|
499 | |||
500 | /** |
||
501 | * Get the visibility of a file. |
||
502 | * |
||
503 | * @param string $path |
||
504 | * |
||
505 | * @return array|false |
||
506 | */ |
||
507 | 4 | public function getVisibility($path) |
|
511 | |||
512 | /** |
||
513 | * {@inheritdoc} |
||
514 | */ |
||
515 | 64 | public function applyPathPrefix($path) |
|
519 | |||
520 | /** |
||
521 | * {@inheritdoc} |
||
522 | */ |
||
523 | 72 | public function setPathPrefix($prefix) |
|
529 | |||
530 | /** |
||
531 | * Get the object acl presented as a visibility. |
||
532 | * |
||
533 | * @param string $path |
||
534 | * |
||
535 | * @return string |
||
536 | */ |
||
537 | 12 | protected function getRawVisibility($path) |
|
563 | |||
564 | /** |
||
565 | * Upload an object. |
||
566 | * |
||
567 | * @param $path |
||
568 | * @param $body |
||
569 | * @param Config $config |
||
570 | * |
||
571 | * @return array |
||
572 | */ |
||
573 | 10 | protected function upload($path, $body, Config $config) |
|
595 | |||
596 | /** |
||
597 | * Get options from the config. |
||
598 | * |
||
599 | * @param Config $config |
||
600 | * |
||
601 | * @return array |
||
602 | */ |
||
603 | 10 | protected function getOptionsFromConfig(Config $config) |
|
630 | |||
631 | /** |
||
632 | * Normalize the object result array. |
||
633 | * |
||
634 | * @param array $response |
||
635 | * @param string $path |
||
636 | * |
||
637 | * @return array |
||
638 | */ |
||
639 | 24 | protected function normalizeResponse(array $response, $path = null) |
|
672 | |||
673 | /** |
||
674 | * @param $location |
||
675 | * |
||
676 | * @return bool |
||
677 | */ |
||
678 | 10 | protected function doesDirectoryExist($location) |
|
703 | } |
||
704 |