Complex classes like AwsS3Provider 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 AwsS3Provider, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class AwsS3Provider extends Provider implements ProviderInterface |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * All the configurations needed by this class with the |
||
| 39 | * optional configurations default values. |
||
| 40 | * |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected $default = [ |
||
| 44 | 'url' => null, |
||
| 45 | 'threshold' => 10, |
||
| 46 | 'providers' => [ |
||
| 47 | 'aws' => [ |
||
| 48 | 's3' => [ |
||
| 49 | 'version' => null, |
||
| 50 | 'region' => null, |
||
| 51 | 'endpoint' => null, |
||
| 52 | 'buckets' => null, |
||
| 53 | 'upload_folder' => '', |
||
| 54 | 'http' => null, |
||
| 55 | 'acl' => 'public-read', |
||
| 56 | 'cloudfront' => [ |
||
| 57 | 'use' => false, |
||
| 58 | 'cdn_url' => null, |
||
| 59 | ], |
||
| 60 | ], |
||
| 61 | ], |
||
| 62 | ], |
||
| 63 | ]; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Required configurations (must exist in the config file). |
||
| 67 | * |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $rules = ['version', 'region', 'key', 'secret', 'buckets', 'url']; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * this array holds the parsed configuration to be used across the class. |
||
| 74 | * |
||
| 75 | * @var Array |
||
| 76 | */ |
||
| 77 | protected $supplier; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var Instance of Aws\S3\S3Client |
||
| 81 | */ |
||
| 82 | protected $s3_client; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var Instance of Guzzle\Batch\BatchBuilder |
||
| 86 | */ |
||
| 87 | protected $batch; |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @var \Publiux\laravelcdn\Contracts\CdnHelperInterface |
||
| 91 | */ |
||
| 92 | protected $cdn_helper; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @var \Publiux\laravelcdn\Validators\Contracts\ConfigurationsInterface |
||
| 96 | */ |
||
| 97 | protected $configurations; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var \Publiux\laravelcdn\Validators\Contracts\ProviderValidatorInterface |
||
| 101 | */ |
||
| 102 | protected $provider_validator; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param \Symfony\Component\Console\Output\ConsoleOutput $console |
||
| 106 | * @param \Publiux\laravelcdn\Validators\Contracts\ProviderValidatorInterface $provider_validator |
||
| 107 | * @param \Publiux\laravelcdn\Contracts\CdnHelperInterface $cdn_helper |
||
| 108 | */ |
||
| 109 | public function __construct( |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Read the configuration and prepare an array with the relevant configurations |
||
| 121 | * for the (AWS S3) provider. and return itself. |
||
| 122 | * |
||
| 123 | * @param $configurations |
||
| 124 | * |
||
| 125 | * @return $this |
||
| 126 | */ |
||
| 127 | public function init($configurations) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Upload assets. |
||
| 157 | * |
||
| 158 | * @param $assets |
||
| 159 | * |
||
| 160 | * @return bool |
||
| 161 | */ |
||
| 162 | public function upload($assets) |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Create an S3 client instance |
||
| 219 | * (Note: it will read the credentials form the .env file). |
||
| 220 | * |
||
| 221 | * @return bool |
||
| 222 | */ |
||
| 223 | public function connect() |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param $s3_client |
||
| 245 | */ |
||
| 246 | public function setS3Client($s3_client) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Get files to upload |
||
| 253 | * |
||
| 254 | * @param $assets |
||
| 255 | * @return mixed |
||
| 256 | */ |
||
| 257 | private function getFilesToUpload($assets) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @return array |
||
| 303 | */ |
||
| 304 | public function getBucket() |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Empty bucket. |
||
| 319 | * |
||
| 320 | * @return bool |
||
| 321 | */ |
||
| 322 | public function emptyBucket() |
||
| 366 | |||
| 367 | /** |
||
| 368 | * This function will be called from the CdnFacade class when |
||
| 369 | * someone use this {{ Cdn::asset('') }} facade helper. |
||
| 370 | * |
||
| 371 | * @param $path |
||
| 372 | * |
||
| 373 | * @return string |
||
| 374 | */ |
||
| 375 | public function urlGenerator($path) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @return string |
||
| 393 | */ |
||
| 394 | public function getCloudFront() |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @return string |
||
| 405 | */ |
||
| 406 | public function getCloudFrontUrl() |
||
| 410 | |||
| 411 | /** |
||
| 412 | * @return string |
||
| 413 | */ |
||
| 414 | public function getUrl() |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Calculate Amazon AWS ETag used on the S3 service |
||
| 421 | * |
||
| 422 | * @see https://stackoverflow.com/a/36072294/1762839 |
||
| 423 | * @author TheStoryCoder (https://stackoverflow.com/users/2404541/thestorycoder) |
||
| 424 | * |
||
| 425 | * @param string $filename path to file to check |
||
| 426 | * @param int $chunksize chunk size in Megabytes |
||
| 427 | * @param bool|string $expected verify calculated etag against this specified |
||
| 428 | * etag and return true or false instead if you make |
||
| 429 | * chunksize negative (eg. -8 instead of 8) the function |
||
| 430 | * will guess the chunksize by checking all possible sizes |
||
| 431 | * given the number of parts mentioned in $expected |
||
| 432 | * |
||
| 433 | * @return bool|string ETag (string) or boolean true|false if $expected is set |
||
| 434 | */ |
||
| 435 | protected function calculateEtag($filename, $chunksize, $expected = false) { |
||
| 496 | |||
| 497 | |||
| 498 | /** |
||
| 499 | * @param $attr |
||
| 500 | * |
||
| 501 | * @return Mix | null |
||
| 502 | */ |
||
| 503 | public function __get($attr) |
||
| 507 | } |
||
| 508 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..