Complex classes like Container 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 Container, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class Container implements ContainerContract, FilesTransformerContract, Countable, JsonSerializable |
||
18 | { |
||
19 | use FilesTransformer; |
||
20 | |||
21 | /** |
||
22 | * @var \ArgentCrusade\Selectel\CloudStorage\Contracts\Api\ApiClientContract $api |
||
23 | */ |
||
24 | protected $api; |
||
25 | |||
26 | /** |
||
27 | * Container name. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $containerName; |
||
32 | |||
33 | /** |
||
34 | * Container data. |
||
35 | * |
||
36 | * @var array |
||
37 | */ |
||
38 | protected $data = []; |
||
39 | |||
40 | /** |
||
41 | * Determines if container data was already loaded. |
||
42 | * |
||
43 | * @var bool |
||
44 | */ |
||
45 | protected $dataLoaded = false; |
||
46 | |||
47 | /** |
||
48 | * @param \ArgentCrusade\Selectel\CloudStorage\Contracts\Api\ApiClientContract $api |
||
49 | * @param string $name |
||
50 | * @param array $data |
||
51 | */ |
||
52 | public function __construct(ApiClientContract $api, $name, array $data = []) |
||
59 | |||
60 | /** |
||
61 | * Returns specific container data. |
||
62 | * |
||
63 | * @param string $key |
||
64 | * @param mixed $default = null |
||
65 | * |
||
66 | * @return mixed |
||
67 | */ |
||
68 | protected function containerData($key, $default = null) |
||
76 | |||
77 | /** |
||
78 | * Lazy loading for container data. |
||
79 | * |
||
80 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\ApiRequestFailedException |
||
81 | */ |
||
82 | protected function loadContainerData() |
||
106 | |||
107 | /** |
||
108 | * Absolute path to file from storage root. |
||
109 | * |
||
110 | * @param string $path = '' Relative file path. |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | protected function absolutePath($path = '') |
||
118 | |||
119 | /** |
||
120 | * API Client. |
||
121 | * |
||
122 | * @return \ArgentCrusade\Selectel\CloudStorage\Contracts\Api\ApiClientContract |
||
123 | */ |
||
124 | public function apiClient() |
||
128 | |||
129 | /** |
||
130 | * Returns JSON representation of container. |
||
131 | * |
||
132 | * @return array |
||
133 | */ |
||
134 | public function jsonSerialize() |
||
145 | |||
146 | /** |
||
147 | * Container name. |
||
148 | * |
||
149 | * @return string |
||
150 | */ |
||
151 | public function name() |
||
155 | |||
156 | /** |
||
157 | * Container name. |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | public function containerName() |
||
165 | |||
166 | /** |
||
167 | * Container visibility type. |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | public function type() |
||
175 | |||
176 | /** |
||
177 | * Container files count. |
||
178 | * |
||
179 | * @return int |
||
180 | */ |
||
181 | public function filesCount() |
||
185 | |||
186 | /** |
||
187 | * Container files count. |
||
188 | * |
||
189 | * @return int |
||
190 | */ |
||
191 | public function count() |
||
195 | |||
196 | /** |
||
197 | * Container size in bytes. |
||
198 | * |
||
199 | * @return int |
||
200 | */ |
||
201 | public function size() |
||
205 | |||
206 | /** |
||
207 | * Total uploaded (received) bytes. |
||
208 | * |
||
209 | * @return int |
||
210 | */ |
||
211 | public function uploadedBytes() |
||
215 | |||
216 | /** |
||
217 | * Total downloaded (transmitted) bytes. |
||
218 | * |
||
219 | * @return int |
||
220 | */ |
||
221 | public function downloadedBytes() |
||
225 | |||
226 | /** |
||
227 | * Determines if container is public. |
||
228 | * |
||
229 | * @return bool |
||
230 | */ |
||
231 | public function isPublic() |
||
235 | |||
236 | /** |
||
237 | * Determines if container is private. |
||
238 | * |
||
239 | * @return bool |
||
240 | */ |
||
241 | public function isPrivate() |
||
245 | |||
246 | /** |
||
247 | * Determines if container is a gallery container. |
||
248 | * |
||
249 | * @return bool |
||
250 | */ |
||
251 | public function isGallery() |
||
255 | |||
256 | /** |
||
257 | * Sets container type to 'public'. |
||
258 | * |
||
259 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\ApiRequestFailedException |
||
260 | * |
||
261 | * @return string |
||
262 | */ |
||
263 | public function setPublic() |
||
267 | |||
268 | /** |
||
269 | * Sets container type to 'private'. |
||
270 | * |
||
271 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\ApiRequestFailedException |
||
272 | * |
||
273 | * @return string |
||
274 | */ |
||
275 | public function setPrivate() |
||
279 | |||
280 | /** |
||
281 | * Sets container type to 'gallery'. |
||
282 | * |
||
283 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\ApiRequestFailedException |
||
284 | * |
||
285 | * @return string |
||
286 | */ |
||
287 | public function setGallery() |
||
291 | |||
292 | /** |
||
293 | * Updates container type. |
||
294 | * |
||
295 | * @param string $type Container type, 'public', 'private' or 'gallery'. |
||
296 | * |
||
297 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\ApiRequestFailedException |
||
298 | * |
||
299 | * @return string |
||
300 | */ |
||
301 | protected function setType($type) |
||
319 | |||
320 | /** |
||
321 | * Creates new Fluent files loader instance. |
||
322 | * |
||
323 | * @return \ArgentCrusade\Selectel\CloudStorage\Contracts\FluentFilesLoaderContract |
||
324 | */ |
||
325 | public function files() |
||
329 | |||
330 | /** |
||
331 | * Determines whether file exists or not. |
||
332 | * |
||
333 | * @param string $path File path. |
||
334 | * |
||
335 | * @return bool |
||
336 | */ |
||
337 | public function fileExists($path) |
||
341 | |||
342 | /** |
||
343 | * Retrieves file object container. This method does not actually download file, see File::read or File::readStream. |
||
344 | * |
||
345 | * @param string $path |
||
346 | * |
||
347 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\FileNotFoundException |
||
348 | * |
||
349 | * @return \ArgentCrusade\Selectel\CloudStorage\Contracts\FileContract |
||
350 | */ |
||
351 | public function getFile($path) |
||
355 | |||
356 | /** |
||
357 | * Creates new directory. |
||
358 | * |
||
359 | * @param string $name Directory name. |
||
360 | * |
||
361 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\ApiRequestFailedException |
||
362 | * |
||
363 | * @return string |
||
364 | */ |
||
365 | public function createDir($name) |
||
379 | |||
380 | /** |
||
381 | * Deletes directory. |
||
382 | * |
||
383 | * @param string $name Directory name. |
||
384 | */ |
||
385 | public function deleteDir($name) |
||
395 | |||
396 | /** |
||
397 | * Uploads file contents from string. Returns ETag header value if upload was successful. |
||
398 | * |
||
399 | * @param string $path Remote path. |
||
400 | * @param string $contents File contents. |
||
401 | * @param array $params = [] Upload params. |
||
402 | * @param bool $verifyChecksum = true |
||
403 | * |
||
404 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\UploadFailedException |
||
405 | * |
||
406 | * @return string |
||
407 | */ |
||
408 | public function uploadFromString($path, $contents, array $params = [], $verifyChecksum = true) |
||
412 | |||
413 | /** |
||
414 | * Uploads file from stream. Returns ETag header value if upload was successful. |
||
415 | * |
||
416 | * @param string $path Remote path. |
||
417 | * @param resource $resource Stream resource. |
||
418 | * @param array $params = [] Upload params. |
||
419 | * |
||
420 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\UploadFailedException |
||
421 | * |
||
422 | * @return string |
||
423 | */ |
||
424 | public function uploadFromStream($path, $resource, array $params = []) |
||
428 | |||
429 | /** |
||
430 | * Upload file from string or stream resource. |
||
431 | * |
||
432 | * @param string $path Remote path. |
||
433 | * @param string | resource $contents File contents. |
||
434 | * @param array $params = [] Upload params. |
||
435 | * @param bool $verifyChecksum = true |
||
436 | * |
||
437 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\UploadFailedException |
||
438 | * |
||
439 | * @return string |
||
440 | */ |
||
441 | protected function uploadFrom($path, $contents, array $params = [], $verifyChecksum = true) |
||
454 | |||
455 | /** |
||
456 | * Parses upload parameters and assigns them to appropriate HTTP headers. |
||
457 | * |
||
458 | * @param string $contents = null |
||
459 | * @param array $params = [] |
||
460 | * @param bool $verifyChecksum = true |
||
461 | * |
||
462 | * @return array |
||
463 | */ |
||
464 | protected function convertUploadParamsToHeaders($contents = null, array $params = [], $verifyChecksum = true) |
||
487 | |||
488 | /** |
||
489 | * Deletes container. Container must be empty in order to perform this operation. |
||
490 | * |
||
491 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\ApiRequestFailedException |
||
492 | */ |
||
493 | public function delete() |
||
507 | } |
||
508 |