Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
38 | class AwsS3Provider extends Provider implements ProviderInterface |
||
39 | { |
||
40 | /** |
||
41 | * All the configurations needed by this class with the |
||
42 | * optional configurations default values. |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $default = [ |
||
47 | 'url' => null, |
||
48 | 'threshold' => 10, |
||
49 | 'compression' => [ |
||
50 | 'extensions' => [], |
||
51 | 'algorithm' => null, |
||
52 | 'level' => 9 |
||
53 | ], |
||
54 | 'providers' => [ |
||
55 | 'aws' => [ |
||
56 | 's3' => [ |
||
57 | 'version' => null, |
||
58 | 'region' => null, |
||
59 | 'endpoint' => null, |
||
60 | 'buckets' => null, |
||
61 | 'upload_folder' => '', |
||
62 | 'http' => null, |
||
63 | 'acl' => 'public-read', |
||
64 | 'cloudfront' => [ |
||
65 | 'use' => false, |
||
66 | 'cdn_url' => null, |
||
67 | ], |
||
68 | ], |
||
69 | ], |
||
70 | ], |
||
71 | ]; |
||
72 | |||
73 | /** |
||
74 | * Required configurations (must exist in the config file). |
||
75 | * |
||
76 | * @var array |
||
77 | */ |
||
78 | protected $rules = ['version', 'region', 'key', 'secret', 'buckets', 'url']; |
||
79 | |||
80 | /** |
||
81 | * this array holds the parsed configuration to be used across the class. |
||
82 | * |
||
83 | * @var Array |
||
84 | */ |
||
85 | protected $supplier; |
||
86 | |||
87 | /** |
||
88 | * @var Instance of Aws\S3\S3Client |
||
89 | */ |
||
90 | protected $s3_client; |
||
91 | |||
92 | /** |
||
93 | * @var Instance of Guzzle\Batch\BatchBuilder |
||
94 | */ |
||
95 | protected $batch; |
||
96 | |||
97 | /** |
||
98 | * @var \Publiux\laravelcdn\Contracts\CdnHelperInterface |
||
99 | */ |
||
100 | protected $cdn_helper; |
||
101 | |||
102 | /** |
||
103 | * @var \Publiux\laravelcdn\Validators\Contracts\ConfigurationsInterface |
||
104 | */ |
||
105 | protected $configurations; |
||
106 | |||
107 | /** |
||
108 | * @var \Publiux\laravelcdn\Validators\Contracts\ProviderValidatorInterface |
||
109 | */ |
||
110 | protected $provider_validator; |
||
111 | |||
112 | /** |
||
113 | * @param \Symfony\Component\Console\Output\ConsoleOutput $console |
||
114 | * @param \Publiux\laravelcdn\Validators\Contracts\ProviderValidatorInterface $provider_validator |
||
115 | * @param \Publiux\laravelcdn\Contracts\CdnHelperInterface $cdn_helper |
||
116 | */ |
||
117 | public function __construct( |
||
118 | ConsoleOutput $console, |
||
119 | ProviderValidatorInterface $provider_validator, |
||
120 | CdnHelperInterface $cdn_helper |
||
121 | ) { |
||
122 | $this->console = $console; |
||
|
|||
123 | $this->provider_validator = $provider_validator; |
||
124 | $this->cdn_helper = $cdn_helper; |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * Read the configuration and prepare an array with the relevant configurations |
||
129 | * for the (AWS S3) provider. and return itself. |
||
130 | * |
||
131 | * @param $configurations |
||
132 | * |
||
133 | * @return $this |
||
134 | */ |
||
135 | public function init($configurations) |
||
136 | { |
||
137 | // merge the received config array with the default configurations array to |
||
138 | // fill missed keys with null or default values. |
||
139 | $this->default = array_replace_recursive($this->default, $configurations); |
||
140 | |||
141 | $supplier = [ |
||
142 | 'provider_url' => $this->default['url'], |
||
143 | 'threshold' => $this->default['threshold'], |
||
144 | 'version' => $this->default['providers']['aws']['s3']['version'], |
||
145 | 'region' => $this->default['providers']['aws']['s3']['region'], |
||
146 | 'endpoint' => $this->default['providers']['aws']['s3']['endpoint'], |
||
147 | 'buckets' => $this->default['providers']['aws']['s3']['buckets'], |
||
148 | 'acl' => $this->default['providers']['aws']['s3']['acl'], |
||
149 | 'cloudfront' => $this->default['providers']['aws']['s3']['cloudfront']['use'], |
||
150 | 'cloudfront_url' => $this->default['providers']['aws']['s3']['cloudfront']['cdn_url'], |
||
151 | 'http' => $this->default['providers']['aws']['s3']['http'], |
||
152 | 'upload_folder' => $this->default['providers']['aws']['s3']['upload_folder'], |
||
153 | 'compression' => $this->default['compression'], |
||
154 | ]; |
||
155 | |||
156 | // check if any required configuration is missed |
||
157 | $this->provider_validator->validate($supplier, $this->rules); |
||
158 | |||
159 | $this->supplier = $supplier; |
||
160 | |||
161 | return $this; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Upload assets. |
||
166 | * |
||
167 | * @param $assets |
||
168 | * |
||
169 | * @return bool |
||
170 | */ |
||
171 | public function upload($assets) |
||
172 | { |
||
173 | // connect before uploading |
||
174 | $connected = $this->connect(); |
||
175 | |||
176 | if (!$connected) { |
||
177 | return false; |
||
178 | } |
||
179 | |||
180 | // user terminal message |
||
181 | $this->console->writeln('<fg=yellow>Comparing local files and bucket...</fg=yellow>'); |
||
182 | |||
183 | $assets = $this->getFilesAlreadyOnBucket($assets); |
||
184 | |||
185 | // upload each asset file to the CDN |
||
186 | $count = count($assets); |
||
187 | if ($count > 0) { |
||
188 | $this->console->writeln('<fg=yellow>Upload in progress......</fg=yellow>'); |
||
189 | foreach ($assets as $i => $file) { |
||
190 | try { |
||
191 | $needsCompression = $this->needCompress($file); |
||
192 | $this->console->writeln( |
||
193 | '<fg=magenta>' . str_pad( number_format (($count / ($i + 1) ) * 100, 2), 6, ' ',STR_PAD_LEFT) . '% </fg=magenta>' . |
||
194 | '<fg=cyan>Uploading file path: ' . $file->getRealpath() . '</fg=cyan>' . |
||
195 | ($needsCompression ? ' <fg=green>Compressed</fg=green>' : '') |
||
196 | ); |
||
197 | $command = $this->s3_client->getCommand('putObject', [ |
||
198 | |||
199 | // the bucket name |
||
200 | 'Bucket' => $this->getBucket(), |
||
201 | // the path of the file on the server (CDN) |
||
202 | 'Key' => $this->supplier['upload_folder'] . str_replace('\\', '/', $file->getPathName()), |
||
203 | // the path of the path locally |
||
204 | 'Body' => $this->getFileContent($file, $needsCompression), |
||
205 | // the permission of the file |
||
206 | |||
207 | 'ACL' => $this->acl, |
||
208 | 'CacheControl' => $this->default['providers']['aws']['s3']['cache-control'], |
||
209 | 'Metadata' => $this->default['providers']['aws']['s3']['metadata'], |
||
210 | 'Expires' => $this->default['providers']['aws']['s3']['expires'], |
||
211 | 'ContentType' => $this->getMimetype($file), |
||
212 | 'ContentEncoding' => $needsCompression ? $this->compression['algorithm'] : 'identity', |
||
213 | ]); |
||
214 | // var_dump(get_class($command));exit(); |
||
215 | |||
216 | |||
217 | $this->s3_client->execute($command); |
||
218 | } catch (S3Exception $e) { |
||
219 | $this->console->writeln('<fg=red>Upload error: '.$e->getMessage().'</fg=red>'); |
||
220 | return false; |
||
221 | } |
||
222 | } |
||
223 | |||
224 | // user terminal message |
||
225 | $this->console->writeln('<fg=green>Upload completed successfully.</fg=green>'); |
||
226 | } else { |
||
227 | // user terminal message |
||
228 | $this->console->writeln('<fg=yellow>No new files to upload.</fg=yellow>'); |
||
229 | } |
||
230 | |||
231 | return true; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Create an S3 client instance |
||
236 | * (Note: it will read the credentials form the .env file). |
||
237 | * |
||
238 | * @return bool |
||
239 | */ |
||
240 | public function connect() |
||
259 | |||
260 | /** |
||
261 | * @param $s3_client |
||
262 | */ |
||
263 | public function setS3Client($s3_client) |
||
267 | |||
268 | /** |
||
269 | * @param $assets |
||
270 | * @return mixed |
||
271 | */ |
||
272 | private function getFilesAlreadyOnBucket($assets) |
||
309 | |||
310 | /** |
||
311 | * @return array |
||
312 | */ |
||
313 | public function getBucket() |
||
325 | |||
326 | /** |
||
327 | * Empty bucket. |
||
328 | * |
||
329 | * @return bool |
||
330 | */ |
||
331 | public function emptyBucket() |
||
375 | |||
376 | /** |
||
377 | * This function will be called from the CdnFacade class when |
||
378 | * someone use this {{ Cdn::asset('') }} facade helper. |
||
379 | * |
||
380 | * @param $path |
||
381 | * |
||
382 | * @return string |
||
383 | */ |
||
384 | public function urlGenerator($path) |
||
399 | |||
400 | /** |
||
401 | * @return string |
||
402 | */ |
||
403 | public function getCloudFront() |
||
411 | |||
412 | /** |
||
413 | * @return string |
||
414 | */ |
||
415 | public function getCloudFrontUrl() |
||
419 | |||
420 | /** |
||
421 | * @return string |
||
422 | */ |
||
423 | public function getUrl() |
||
427 | |||
428 | /** |
||
429 | * @param $attr |
||
430 | * |
||
431 | * @return Mix | null |
||
432 | */ |
||
433 | public function __get($attr) |
||
437 | |||
438 | /** |
||
439 | * Does file needs compression |
||
440 | * |
||
441 | * @param SplFileInfo $file File info |
||
442 | * |
||
443 | * @return bool |
||
444 | */ |
||
445 | private function needCompress(SplFileInfo $file) { |
||
451 | |||
452 | /** |
||
453 | * Read file content and compress |
||
454 | * |
||
455 | * @param SplFileInfo $file File to read |
||
456 | * @param bool $needsCompress Need file to compress |
||
457 | * |
||
458 | * @return resource|string |
||
459 | */ |
||
460 | private function getFileContent(SplFileInfo $file, $needsCompress) { |
||
483 | |||
484 | /** |
||
485 | * Get mimetype from config or from system |
||
486 | * |
||
487 | * @param SplFileInfo $file File info to get mimetype |
||
488 | * |
||
489 | * @return false|string |
||
490 | */ |
||
491 | protected function getMimetype(SplFileInfo $file) { |
||
498 | |||
499 | } |
||
500 |
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..