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 | * Returns JSON representation of container. |
||
| 121 | * |
||
| 122 | * @return array |
||
| 123 | */ |
||
| 124 | public function jsonSerialize() |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Container name. |
||
| 138 | * |
||
| 139 | * @return string |
||
| 140 | */ |
||
| 141 | public function name() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Container name. |
||
| 148 | * |
||
| 149 | * @return string |
||
| 150 | */ |
||
| 151 | public function containerName() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Container visibility type. |
||
| 158 | * |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | public function type() |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Container files count. |
||
| 168 | * |
||
| 169 | * @return int |
||
| 170 | */ |
||
| 171 | public function filesCount() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Container files count. |
||
| 178 | * |
||
| 179 | * @return int |
||
| 180 | */ |
||
| 181 | public function count() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Container size in bytes. |
||
| 188 | * |
||
| 189 | * @return int |
||
| 190 | */ |
||
| 191 | public function size() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Total uploaded (received) bytes. |
||
| 198 | * |
||
| 199 | * @return int |
||
| 200 | */ |
||
| 201 | public function uploadedBytes() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Total downloaded (transmitted) bytes. |
||
| 208 | * |
||
| 209 | * @return int |
||
| 210 | */ |
||
| 211 | public function downloadedBytes() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Determines if container is public. |
||
| 218 | * |
||
| 219 | * @return bool |
||
| 220 | */ |
||
| 221 | public function isPublic() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Determines if container is private. |
||
| 228 | * |
||
| 229 | * @return bool |
||
| 230 | */ |
||
| 231 | public function isPrivate() |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Determines if container is a gallery container. |
||
| 238 | * |
||
| 239 | * @return bool |
||
| 240 | */ |
||
| 241 | public function isGallery() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Sets container type to 'public'. |
||
| 248 | * |
||
| 249 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\ApiRequestFailedException |
||
| 250 | * |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | public function setPublic() |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Sets container type to 'private'. |
||
| 260 | * |
||
| 261 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\ApiRequestFailedException |
||
| 262 | * |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | public function setPrivate() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Sets container type to 'gallery'. |
||
| 272 | * |
||
| 273 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\ApiRequestFailedException |
||
| 274 | * |
||
| 275 | * @return string |
||
| 276 | */ |
||
| 277 | public function setGallery() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Updates container type. |
||
| 284 | * |
||
| 285 | * @param string $type Container type, 'public', 'private' or 'gallery'. |
||
| 286 | * |
||
| 287 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\ApiRequestFailedException |
||
| 288 | * |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | protected function setType($type) |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Creates new Fluent files loader instance. |
||
| 312 | * |
||
| 313 | * @return \ArgentCrusade\Selectel\CloudStorage\Contracts\FluentFilesLoaderContract |
||
| 314 | */ |
||
| 315 | public function files() |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Determines whether file exists or not. |
||
| 322 | * |
||
| 323 | * @param string $path File path. |
||
| 324 | * |
||
| 325 | * @return bool |
||
| 326 | */ |
||
| 327 | public function fileExists($path) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Retrieves file object container. This method does not actually download file, see File::read or File::readStream. |
||
| 334 | * |
||
| 335 | * @param string $path |
||
| 336 | * |
||
| 337 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\FileNotFoundException |
||
| 338 | * |
||
| 339 | * @return \ArgentCrusade\Selectel\CloudStorage\Contracts\FileContract |
||
| 340 | */ |
||
| 341 | public function getFile($path) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Creates new directory. |
||
| 348 | * |
||
| 349 | * @param string $name Directory name. |
||
| 350 | * |
||
| 351 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\ApiRequestFailedException |
||
| 352 | * |
||
| 353 | * @return string |
||
| 354 | */ |
||
| 355 | public function createDir($name) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Deletes directory. |
||
| 372 | * |
||
| 373 | * @param string $name Directory name. |
||
| 374 | */ |
||
| 375 | public function deleteDir($name) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Uploads file contents from string. Returns ETag header value if upload was successful. |
||
| 388 | * |
||
| 389 | * @param string $path Remote path. |
||
| 390 | * @param string $contents File contents. |
||
| 391 | * @param array $params = [] Upload params. |
||
| 392 | * @param bool $verifyChecksum = true |
||
| 393 | * |
||
| 394 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\UploadFailedException |
||
| 395 | * |
||
| 396 | * @return string |
||
| 397 | */ |
||
| 398 | public function uploadFromString($path, $contents, array $params = [], $verifyChecksum = true) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Uploads file from stream. Returns ETag header value if upload was successful. |
||
| 405 | * |
||
| 406 | * @param string $path Remote path. |
||
| 407 | * @param resource $resource Stream resource. |
||
| 408 | * @param array $params = [] Upload params. |
||
| 409 | * |
||
| 410 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\UploadFailedException |
||
| 411 | * |
||
| 412 | * @return string |
||
| 413 | */ |
||
| 414 | public function uploadFromStream($path, $resource, array $params = []) |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Upload file from string or stream resource. |
||
| 421 | * |
||
| 422 | * @param string $path Remote path. |
||
| 423 | * @param string | resource $contents File contents. |
||
| 424 | * @param array $params = [] Upload params. |
||
| 425 | * @param bool $verifyChecksum = true |
||
| 426 | * |
||
| 427 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\UploadFailedException |
||
| 428 | * |
||
| 429 | * @return string |
||
| 430 | */ |
||
| 431 | protected function uploadFrom($path, $contents, array $params = [], $verifyChecksum = true) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Parses upload parameters and assigns them to appropriate HTTP headers. |
||
| 447 | * |
||
| 448 | * @param string $contents = null |
||
| 449 | * @param array $params = [] |
||
| 450 | * @param bool $verifyChecksum = true |
||
| 451 | * |
||
| 452 | * @return array |
||
| 453 | */ |
||
| 454 | protected function convertUploadParamsToHeaders($contents = null, array $params = [], $verifyChecksum = true) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Deletes container. Container must be empty in order to perform this operation. |
||
| 480 | * |
||
| 481 | * @throws \ArgentCrusade\Selectel\CloudStorage\Exceptions\ApiRequestFailedException |
||
| 482 | */ |
||
| 483 | public function delete() |
||
| 497 | } |
||
| 498 |