Total Complexity | 61 |
Total Lines | 466 |
Duplicated Lines | 0 % |
Changes | 9 | ||
Bugs | 5 | Features | 0 |
Complex classes like Uploader 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.
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 Uploader, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class Uploader |
||
29 | { |
||
30 | use ErrorCollectorTrait; |
||
31 | |||
32 | /** |
||
33 | * Uploader::$path |
||
34 | * |
||
35 | * Uploader file destination path. |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $path; |
||
40 | |||
41 | /** |
||
42 | * Uploader::$maxIncrementFilename |
||
43 | * |
||
44 | * Maximum incremental uploaded filename. |
||
45 | * |
||
46 | * @var int |
||
47 | */ |
||
48 | protected $maxIncrementFilename = 100; |
||
49 | |||
50 | /** |
||
51 | * Uploader::$allowedMimes |
||
52 | * |
||
53 | * Allowed uploaded file mime types. |
||
54 | * |
||
55 | * @var array |
||
56 | */ |
||
57 | protected $allowedMimes; |
||
58 | |||
59 | /** |
||
60 | * Uploader::$allowedExtensions |
||
61 | * |
||
62 | * Allowed uploaded file extensions. |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $allowedExtensions; |
||
67 | |||
68 | /** |
||
69 | * Uploader::$allowedFileSize |
||
70 | * |
||
71 | * Allowed uploaded file size. |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | protected $allowedFileSize = [ |
||
76 | 'min' => 0, |
||
77 | 'max' => 0, |
||
78 | ]; |
||
79 | |||
80 | /** |
||
81 | * Uploader::$targetFilename |
||
82 | * |
||
83 | * Uploader target filename. |
||
84 | * |
||
85 | * @var string |
||
86 | */ |
||
87 | protected $targetFilename; |
||
88 | |||
89 | protected $uploadedFiles = []; |
||
90 | |||
91 | // -------------------------------------------------------------------------------------- |
||
92 | |||
93 | /** |
||
94 | * Uploader::__construct |
||
95 | * |
||
96 | * @param array $config |
||
97 | * |
||
98 | * @throws \O2System\Spl\Exceptions\Logic\BadFunctionCall\BadDependencyCallException |
||
99 | */ |
||
100 | public function __construct(array $config = []) |
||
101 | { |
||
102 | language() |
||
103 | ->addFilePath(str_replace('Handlers', '', __DIR__) . DIRECTORY_SEPARATOR) |
||
104 | ->loadFile('uploader'); |
||
105 | |||
106 | if ( ! extension_loaded('fileinfo')) { |
||
107 | throw new BadDependencyCallException('UPLOADER_E_FINFO_EXTENSION'); |
||
108 | } |
||
109 | |||
110 | if (isset($config[ 'path' ])) { |
||
111 | $config[ 'path' ] = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $config[ 'path' ]); |
||
112 | |||
113 | if (is_dir($config[ 'path' ])) { |
||
114 | $this->path = $config[ 'path' ]; |
||
115 | } elseif (defined('PATH_STORAGE')) { |
||
116 | if (is_dir($config[ 'path' ])) { |
||
117 | $this->path = $config[ 'path' ]; |
||
118 | } else { |
||
119 | $this->path = PATH_STORAGE . str_replace(PATH_STORAGE, '', $config[ 'path' ]); |
||
|
|||
120 | } |
||
121 | } else { |
||
122 | $this->path = dirname($_SERVER[ 'SCRIPT_FILENAME' ]) . DIRECTORY_SEPARATOR . $config[ 'path' ]; |
||
123 | } |
||
124 | } elseif (defined('PATH_STORAGE')) { |
||
125 | $this->path = PATH_STORAGE; |
||
126 | } else { |
||
127 | $this->path = dirname($_SERVER[ 'SCRIPT_FILENAME' ]) . DIRECTORY_SEPARATOR . 'upload'; |
||
128 | } |
||
129 | |||
130 | $this->path = rtrim($this->path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
||
131 | |||
132 | if (isset($config[ 'allowedMimes' ])) { |
||
133 | $this->setAllowedMimes($config[ 'allowedMimes' ]); |
||
134 | } |
||
135 | |||
136 | if (isset($config[ 'allowedExtensions' ])) { |
||
137 | $this->setAllowedExtensions($config[ 'allowedExtensions' ]); |
||
138 | } |
||
139 | |||
140 | $this->uploadedFiles = new ArrayIterator(); |
||
141 | } |
||
142 | |||
143 | // ------------------------------------------------------------------------ |
||
144 | |||
145 | /** |
||
146 | * Uploader::setAllowedMimes |
||
147 | * |
||
148 | * Set allowed mime for uploaded file. |
||
149 | * |
||
150 | * @param string|array $mimes List of allowed file mime types. |
||
151 | * |
||
152 | * @return static |
||
153 | */ |
||
154 | public function setAllowedMimes($mimes) |
||
155 | { |
||
156 | if (is_string($mimes)) { |
||
157 | $mimes = explode(',', $mimes); |
||
158 | } |
||
159 | |||
160 | $this->allowedMimes = array_map('trim', $mimes); |
||
161 | |||
162 | return $this; |
||
163 | } |
||
164 | |||
165 | // -------------------------------------------------------------------------------------- |
||
166 | |||
167 | /** |
||
168 | * Uploader::setAllowedExtensions |
||
169 | * |
||
170 | * Set allowed extensions for uploaded file. |
||
171 | * |
||
172 | * @param string|array $extensions List of allowed file extensions. |
||
173 | * |
||
174 | * @return static |
||
175 | */ |
||
176 | public function setAllowedExtensions($extensions) |
||
177 | { |
||
178 | if (is_string($extensions)) { |
||
179 | $extensions = explode(',', $extensions); |
||
180 | } |
||
181 | |||
182 | $this->allowedExtensions = array_map('trim', $extensions); |
||
183 | |||
184 | return $this; |
||
185 | } |
||
186 | |||
187 | // -------------------------------------------------------------------------------------- |
||
188 | |||
189 | /** |
||
190 | * Uploader::setPath |
||
191 | * |
||
192 | * Sets uploaded file path. |
||
193 | * |
||
194 | * @param string $path [description] |
||
195 | * |
||
196 | * @return static |
||
197 | */ |
||
198 | public function setPath($path = '') |
||
199 | { |
||
200 | if (is_dir($path)) { |
||
201 | $this->path = $path; |
||
202 | } elseif (defined('PATH_STORAGE')) { |
||
203 | if (is_dir($path)) { |
||
204 | $this->path = $path; |
||
205 | } else { |
||
206 | $this->path = PATH_STORAGE . str_replace(PATH_STORAGE, '', $path); |
||
207 | } |
||
208 | } else { |
||
209 | $this->path = dirname($_SERVER[ 'SCRIPT_FILENAME' ]) . DIRECTORY_SEPARATOR . $path; |
||
210 | } |
||
211 | } |
||
212 | |||
213 | // -------------------------------------------------------------------------------------- |
||
214 | |||
215 | /** |
||
216 | * Uploader::setMinFileSize |
||
217 | * |
||
218 | * Set minimum file size |
||
219 | * |
||
220 | * @param int $fileSize Allowed minimum file size. |
||
221 | * @param string $unit Allowed minimum file size unit conversion. |
||
222 | * |
||
223 | * @return static |
||
224 | */ |
||
225 | public function setMinFileSize($fileSize, $unit = 'M') |
||
226 | { |
||
227 | switch ($unit) { |
||
228 | case 'B': |
||
229 | $fileSize = (int)$fileSize; |
||
230 | break; |
||
231 | case 'K': |
||
232 | $fileSize = (int)$fileSize * 1000; |
||
233 | break; |
||
234 | case 'M': |
||
235 | $fileSize = (int)$fileSize * 1000000; |
||
236 | break; |
||
237 | case 'G': |
||
238 | $fileSize = (int)$fileSize * 1000000000; |
||
239 | break; |
||
240 | } |
||
241 | |||
242 | $this->allowedFileSize[ 'min' ] = (int)$fileSize; |
||
243 | |||
244 | return $this; |
||
245 | } |
||
246 | |||
247 | // -------------------------------------------------------------------------------------- |
||
248 | |||
249 | /** |
||
250 | * Uploader::setMaxFileSize |
||
251 | * |
||
252 | * Set maximum file size |
||
253 | * |
||
254 | * @param int $fileSize Allowed maximum file size. |
||
255 | * @param string $unit Allowed maximum file size unit conversion. |
||
256 | * |
||
257 | * @return static |
||
258 | */ |
||
259 | public function setMaxFileSize($fileSize, $unit = 'M') |
||
260 | { |
||
261 | switch ($unit) { |
||
262 | case 'B': |
||
263 | $fileSize = (int)$fileSize; |
||
264 | break; |
||
265 | case 'K': |
||
266 | $fileSize = (int)$fileSize * 1000; |
||
267 | break; |
||
268 | case 'M': |
||
269 | $fileSize = (int)$fileSize * 1000000; |
||
270 | break; |
||
271 | case 'G': |
||
272 | $fileSize = (int)$fileSize * 1000000000; |
||
273 | break; |
||
274 | } |
||
275 | |||
276 | $this->allowedFileSize[ 'max' ] = (int)$fileSize; |
||
277 | |||
278 | return $this; |
||
279 | } |
||
280 | |||
281 | // -------------------------------------------------------------------------------------- |
||
282 | |||
283 | /** |
||
284 | * Uploader::setMaxIncrementFilename |
||
285 | * |
||
286 | * @param int $increment Maximum increment counter. |
||
287 | * |
||
288 | * @return static |
||
289 | */ |
||
290 | public function setMaxIncrementFilename($increment = 0) |
||
291 | { |
||
292 | $this->maxIncrementFilename = (int)$increment; |
||
293 | |||
294 | return $this; |
||
295 | } |
||
296 | |||
297 | // -------------------------------------------------------------------------------------- |
||
298 | |||
299 | /** |
||
300 | * Uploader::process |
||
301 | * |
||
302 | * @param string|null $field Field offset server uploaded files |
||
303 | * |
||
304 | * @return bool Returns TRUE on success or FALSE on failure. |
||
305 | */ |
||
306 | public function process($field = null) |
||
307 | { |
||
308 | $uploadFiles = input()->files($field); |
||
309 | |||
310 | if ( ! is_array($uploadFiles)) { |
||
311 | $uploadFiles = [$uploadFiles]; |
||
312 | } |
||
313 | |||
314 | if (count($uploadFiles)) { |
||
315 | foreach ($uploadFiles as $file) { |
||
316 | if ($file instanceof UploadFile) { |
||
317 | if (defined('PATH_STORAGE')) { |
||
318 | if ($this->path === PATH_STORAGE) { |
||
319 | if (strpos($file->getClientMediaType(), 'image') !== false) { |
||
320 | $this->path = $this->path . 'images' . DIRECTORY_SEPARATOR; |
||
321 | } else { |
||
322 | $this->path = $this->path . 'files' . DIRECTORY_SEPARATOR; |
||
323 | } |
||
324 | } |
||
325 | } |
||
326 | |||
327 | $targetPath = $this->path; |
||
328 | |||
329 | if (empty($this->targetFilename)) { |
||
330 | $this->setTargetFilename($file->getClientFilename()); |
||
331 | } |
||
332 | |||
333 | $filename = $this->targetFilename; |
||
334 | |||
335 | if ($this->validate($file)) { |
||
336 | if ( ! is_file($filePath = $targetPath . $filename . '.' . $file->getExtension())) { |
||
337 | $this->move($file, $filePath); |
||
338 | } elseif ( ! is_file($filePath = $targetPath . $filename . '-1' . '.' . $file->getExtension())) { |
||
339 | $this->move($file, $filePath); |
||
340 | } else { |
||
341 | $existingFiles = glob($targetPath . $filename . '*.' . $file->getExtension()); |
||
342 | if (count($existingFiles)) { |
||
343 | $increment = count($existingFiles) - 1; |
||
344 | } |
||
345 | |||
346 | foreach (range($increment + 1, $increment + 3, 1) as $increment) { |
||
347 | if ($increment > $this->maxIncrementFilename) { |
||
348 | $this->errors[] = language()->getLine( |
||
349 | 'UPLOADER_E_MAXIMUM_INCREMENT_FILENAME', |
||
350 | [$file->getClientFilename()] |
||
351 | ); |
||
352 | } |
||
353 | |||
354 | if ( ! is_file($filePath = $targetPath . $filename . '-' . $increment . '.' . $file->getExtension())) { |
||
355 | $this->move($file, $filePath); |
||
356 | break; |
||
357 | } |
||
358 | } |
||
359 | } |
||
360 | } |
||
361 | } |
||
362 | } |
||
363 | |||
364 | if (count($this->errors) == 0) { |
||
365 | return true; |
||
366 | } |
||
367 | } |
||
368 | |||
369 | return false; |
||
370 | } |
||
371 | |||
372 | // -------------------------------------------------------------------------------------- |
||
373 | |||
374 | /** |
||
375 | * Uploader::setTargetFilename |
||
376 | * |
||
377 | * Sets target filename. |
||
378 | * |
||
379 | * @param string $filename The target filename. |
||
380 | * @param string $conversionFunction Conversion function name, by default it's using dash inflector function. |
||
381 | * |
||
382 | * @return static |
||
383 | */ |
||
384 | public function setTargetFilename($filename, $conversionFunction = 'dash') |
||
385 | { |
||
386 | $this->targetFilename = call_user_func_array( |
||
387 | $conversionFunction, |
||
388 | [ |
||
389 | strtolower( |
||
390 | trim( |
||
391 | pathinfo($filename, PATHINFO_FILENAME) |
||
392 | ) |
||
393 | ), |
||
394 | ] |
||
395 | ); |
||
396 | |||
397 | return $this; |
||
398 | } |
||
399 | |||
400 | // ------------------------------------------------------------------------ |
||
401 | |||
402 | /** |
||
403 | * Uploader::validate |
||
404 | * |
||
405 | * @param \O2System\Kernel\Http\Message\UploadFile $file |
||
406 | * |
||
407 | * @return bool |
||
408 | */ |
||
409 | protected function validate(UploadFile $file) |
||
410 | { |
||
411 | /* Validate extension */ |
||
412 | if (is_array($this->allowedExtensions) && count($this->allowedExtensions)) { |
||
413 | if ( ! in_array($file->getExtension(), $this->allowedExtensions)) { |
||
414 | $this->errors[] = language()->getLine( |
||
415 | 'UPLOADER_E_ALLOWED_EXTENSIONS', |
||
416 | [implode(',', $this->allowedExtensions), $file->getExtension()] |
||
417 | ); |
||
418 | } |
||
419 | } |
||
420 | |||
421 | /* Validate mime */ |
||
422 | if (is_array($this->allowedMimes) && count($this->allowedExtensions)) { |
||
423 | if ( ! in_array($file->getFileMime(), $this->allowedMimes)) { |
||
424 | $this->errors[] = language()->getLine( |
||
425 | 'UPLOADER_E_ALLOWED_MIMES', |
||
426 | [implode(',', $this->allowedMimes), $file->getFileMime()] |
||
427 | ); |
||
428 | } |
||
429 | } |
||
430 | |||
431 | /* Validate min size */ |
||
432 | if ($this->allowedFileSize[ 'min' ] > 0) { |
||
433 | if ($file->getSize() < $this->allowedFileSize[ 'min' ]) { |
||
434 | $this->errors[] = language()->getLine( |
||
435 | 'UPLOADER_E_ALLOWED_MIN_FILESIZE', |
||
436 | [$this->allowedFileSize[ 'min' ], $file->getSize()] |
||
437 | ); |
||
438 | } |
||
439 | } |
||
440 | |||
441 | /* Validate max size */ |
||
442 | if ($this->allowedFileSize[ 'max' ] > 0) { |
||
443 | if ($file->getSize() > $this->allowedFileSize[ 'max' ]) { |
||
444 | $this->errors[] = language()->getLine( |
||
445 | 'UPLOADER_E_ALLOWED_MAX_FILESIZE', |
||
446 | [$this->allowedFileSize[ 'max' ], $file->getSize()] |
||
447 | ); |
||
448 | } |
||
449 | } |
||
450 | |||
451 | if (count($this->errors) == 0) { |
||
452 | return true; |
||
453 | } |
||
454 | |||
455 | return false; |
||
456 | } |
||
457 | |||
458 | // ------------------------------------------------------------------------ |
||
459 | |||
460 | /** |
||
461 | * Uploader::mode |
||
462 | * |
||
463 | * @param \O2System\Kernel\Http\Message\UploadFile $file |
||
464 | * @param $targetPath |
||
465 | */ |
||
466 | protected function move(UploadFile $file, $targetPath) |
||
467 | { |
||
468 | $fileInfo = [ |
||
469 | 'name' => pathinfo($targetPath, PATHINFO_BASENAME), |
||
470 | 'path' => $targetPath, |
||
471 | 'mime' => $file->getFileMime(), |
||
472 | 'size' => $file->getSize(), |
||
473 | ]; |
||
474 | |||
475 | $file->moveTo($targetPath); |
||
476 | |||
477 | if ( ! $file->getError()) { |
||
478 | $this->uploadedFiles[] = $fileInfo; |
||
479 | } else { |
||
480 | $this->errors[] = $file->getError(); |
||
481 | } |
||
482 | } |
||
483 | |||
484 | // ------------------------------------------------------------------------ |
||
485 | |||
486 | /** |
||
487 | * Uploader::getUploadedFiles |
||
488 | * |
||
489 | * @return array|\O2System\Spl\Iterators\ArrayIterator |
||
490 | */ |
||
491 | public function getUploadedFiles() |
||
494 | } |
||
495 | } |