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 | 'WebsiteRedirectLocation' |
||
43 | ]; |
||
44 | |||
45 | /** |
||
46 | * @var S3Client |
||
47 | */ |
||
48 | protected $s3Client; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $bucket; |
||
54 | |||
55 | /** |
||
56 | * @var array |
||
57 | */ |
||
58 | protected $options = []; |
||
59 | |||
60 | /** |
||
61 | * Constructor. |
||
62 | * |
||
63 | * @param S3Client $client |
||
64 | * @param string $bucket |
||
65 | * @param string $prefix |
||
66 | * @param array $options |
||
67 | */ |
||
68 | 70 | public function __construct(S3Client $client, $bucket, $prefix = '', array $options = []) |
|
69 | { |
||
70 | 70 | $this->s3Client = $client; |
|
71 | 70 | $this->bucket = $bucket; |
|
72 | 70 | $this->setPathPrefix($prefix); |
|
73 | 70 | $this->options = $options; |
|
74 | 70 | } |
|
75 | |||
76 | /** |
||
77 | * Get the S3Client bucket. |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | 2 | public function getBucket() |
|
85 | |||
86 | /** |
||
87 | * Set the S3Client bucket. |
||
88 | * |
||
89 | * @return string |
||
90 | */ |
||
91 | 2 | public function setBucket($bucket) |
|
95 | |||
96 | /** |
||
97 | * Get the S3Client instance. |
||
98 | * |
||
99 | * @return S3Client |
||
100 | */ |
||
101 | public function getClient() |
||
105 | 2 | ||
106 | /** |
||
107 | 2 | * Write a new file. |
|
108 | * |
||
109 | * @param string $path |
||
110 | * @param string $contents |
||
111 | * @param Config $config Config object |
||
112 | * |
||
113 | * @return false|array false on failure file meta data on success |
||
114 | */ |
||
115 | public function write($path, $contents, Config $config) |
||
119 | 2 | ||
120 | /** |
||
121 | 2 | * Update a file. |
|
122 | * |
||
123 | * @param string $path |
||
124 | * @param string $contents |
||
125 | * @param Config $config Config object |
||
126 | * |
||
127 | * @return false|array false on failure file meta data on success |
||
128 | */ |
||
129 | public function update($path, $contents, Config $config) |
||
133 | |||
134 | 4 | /** |
|
135 | 2 | * Rename a file. |
|
136 | * |
||
137 | * @param string $path |
||
138 | 2 | * @param string $newpath |
|
139 | * |
||
140 | * @return bool |
||
141 | */ |
||
142 | public function rename($path, $newpath) |
||
150 | 4 | ||
151 | /** |
||
152 | 4 | * Delete a file. |
|
153 | 4 | * |
|
154 | * @param string $path |
||
155 | 4 | * |
|
156 | 4 | * @return bool |
|
157 | 2 | */ |
|
158 | 4 | public function delete($path) |
|
174 | |||
175 | 4 | /** |
|
176 | 4 | * Delete a directory. |
|
177 | 4 | * |
|
178 | 2 | * @param string $dirname |
|
179 | * |
||
180 | * @return bool |
||
181 | 2 | */ |
|
182 | public function deleteDir($dirname) |
||
193 | |||
194 | 2 | /** |
|
195 | * Create a directory. |
||
196 | * |
||
197 | * @param string $dirname directory name |
||
198 | * @param Config $config |
||
199 | * |
||
200 | * @return bool|array |
||
201 | */ |
||
202 | public function createDir($dirname, Config $config) |
||
206 | 12 | ||
207 | /** |
||
208 | 12 | * Check whether a file exists. |
|
209 | 2 | * |
|
210 | * @param string $path |
||
211 | * |
||
212 | 10 | * @return bool |
|
213 | */ |
||
214 | public function has($path) |
||
224 | 4 | ||
225 | /** |
||
226 | 4 | * Read a file. |
|
227 | 2 | * |
|
228 | 2 | * @param string $path |
|
229 | * |
||
230 | 4 | * @return false|array |
|
231 | */ |
||
232 | public function read($path) |
||
242 | |||
243 | 2 | /** |
|
244 | 2 | * List contents of a directory. |
|
245 | * |
||
246 | 2 | * @param string $directory |
|
247 | 2 | * @param bool $recursive |
|
248 | 2 | * |
|
249 | * @return array |
||
250 | 2 | */ |
|
251 | 2 | public function listContents($directory = '', $recursive = false) |
|
266 | |||
267 | 2 | /** |
|
268 | * @param array $options |
||
269 | 2 | * |
|
270 | * @return array |
||
271 | 2 | */ |
|
272 | protected function retrievePaginatedListing(array $options) |
||
283 | 12 | ||
284 | 12 | /** |
|
285 | * Get all the meta data of a file or directory. |
||
286 | 12 | * |
|
287 | 12 | * @param string $path |
|
288 | * |
||
289 | 12 | * @return false|array |
|
290 | */ |
||
291 | public function getMetadata($path) |
||
316 | 2 | ||
317 | /** |
||
318 | * Get all the meta data of a file or directory. |
||
319 | * |
||
320 | * @param string $path |
||
321 | * |
||
322 | * @return false|array |
||
323 | */ |
||
324 | public function getSize($path) |
||
328 | 2 | ||
329 | /** |
||
330 | * Get the mimetype of a file. |
||
331 | * |
||
332 | * @param string $path |
||
333 | * |
||
334 | * @return false|array |
||
335 | */ |
||
336 | public function getMimetype($path) |
||
340 | 2 | ||
341 | /** |
||
342 | * Get the timestamp of a file. |
||
343 | * |
||
344 | * @param string $path |
||
345 | * |
||
346 | * @return false|array |
||
347 | */ |
||
348 | public function getTimestamp($path) |
||
352 | 2 | ||
353 | /** |
||
354 | 2 | * Write a new file using a stream. |
|
355 | * |
||
356 | * @param string $path |
||
357 | * @param resource $resource |
||
358 | * @param Config $config Config object |
||
359 | * |
||
360 | * @return array|false false on failure file meta data on success |
||
361 | */ |
||
362 | public function writeStream($path, $resource, Config $config) |
||
366 | 2 | ||
367 | /** |
||
368 | 2 | * Update a file using a stream. |
|
369 | * |
||
370 | * @param string $path |
||
371 | * @param resource $resource |
||
372 | * @param Config $config Config object |
||
373 | * |
||
374 | * @return array|false false on failure file meta data on success |
||
375 | */ |
||
376 | public function updateStream($path, $resource, Config $config) |
||
380 | |||
381 | 8 | /** |
|
382 | 8 | * Copy a file. |
|
383 | * |
||
384 | 8 | * @param string $path |
|
385 | 8 | * @param string $newpath |
|
386 | 8 | * |
|
387 | 8 | * @return bool |
|
388 | 8 | */ |
|
389 | 8 | public function copy($path, $newpath) |
|
410 | 4 | ||
411 | /** |
||
412 | 4 | * Read a file as a stream. |
|
413 | 4 | * |
|
414 | 4 | * @param string $path |
|
415 | 4 | * |
|
416 | * @return array|false |
||
417 | 4 | */ |
|
418 | public function readStream($path) |
||
429 | |||
430 | 8 | /** |
|
431 | 8 | * Read an object and normalize the response. |
|
432 | 8 | * |
|
433 | * @param $path |
||
434 | 8 | * |
|
435 | 2 | * @return array|bool |
|
436 | 2 | */ |
|
437 | protected function readObject($path) |
||
459 | |||
460 | 6 | /** |
|
461 | 6 | * Set the visibility for a file. |
|
462 | * |
||
463 | 6 | * @param string $path |
|
464 | 6 | * @param string $visibility |
|
465 | 6 | * |
|
466 | * @return array|false file meta data |
||
467 | 6 | */ |
|
468 | public function setVisibility($path, $visibility) |
||
487 | 4 | ||
488 | /** |
||
489 | * Get the visibility of a file. |
||
490 | * |
||
491 | * @param string $path |
||
492 | * |
||
493 | 64 | * @return array|false |
|
494 | */ |
||
495 | 64 | public function getVisibility($path) |
|
499 | |||
500 | /** |
||
501 | 70 | * {@inheritdoc} |
|
502 | */ |
||
503 | 70 | public function applyPathPrefix($prefix) |
|
507 | |||
508 | /** |
||
509 | * {@inheritdoc} |
||
510 | */ |
||
511 | public function setPathPrefix($prefix) |
||
517 | 12 | ||
518 | 12 | /** |
|
519 | * Get the object acl presented as a visibility. |
||
520 | 12 | * |
|
521 | 12 | * @param string $path |
|
522 | * |
||
523 | 12 | * @return string |
|
524 | */ |
||
525 | 12 | protected function getRawVisibility($path) |
|
551 | 10 | ||
552 | /** |
||
553 | 10 | * Upload an object. |
|
554 | 10 | * |
|
555 | 10 | * @param $path |
|
556 | * @param $body |
||
557 | 10 | * @param Config $config |
|
558 | 2 | * |
|
559 | 2 | * @return array |
|
560 | */ |
||
561 | 10 | protected function upload($path, $body, Config $config) |
|
583 | 10 | ||
584 | /** |
||
585 | 10 | * Get options from the config. |
|
586 | * |
||
587 | 8 | * @param Config $config |
|
588 | * |
||
589 | 8 | * @return array |
|
590 | 8 | */ |
|
591 | protected function getOptionsFromConfig(Config $config) |
||
618 | |||
619 | /** |
||
620 | 24 | * Normalize the object result array. |
|
621 | * |
||
622 | 24 | * @param array $response |
|
623 | 24 | * @param string $path |
|
624 | 24 | * |
|
625 | * @return array |
||
626 | 24 | */ |
|
627 | 14 | protected function normalizeResponse(array $response, $path = null) |
|
649 | 10 | ||
650 | 10 | /** |
|
651 | * @param $location |
||
652 | 10 | * |
|
653 | 10 | * @return bool |
|
654 | 10 | */ |
|
655 | protected function doesDirectoryExist($location) |
||
680 | } |
||
681 |