| Conditions | 19 |
| Paths | 21 |
| Total Lines | 78 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 43 | public function handleUpload(UploadedFile $file, int $userId = 0): string |
||
| 44 | { |
||
| 45 | $allowUploads = $this->uploadConfig['enabled']; |
||
| 46 | if (!$allowUploads) { |
||
| 47 | return ''; |
||
| 48 | } |
||
| 49 | if (!file_exists($this->avatarPath) || !is_readable($this->avatarPath) || !is_writable($this->avatarPath)) { |
||
| 50 | return ''; |
||
| 51 | } |
||
| 52 | |||
| 53 | if (UPLOAD_ERR_OK !== $file->getError()) { |
||
| 54 | return ''; |
||
| 55 | } |
||
| 56 | if (!is_numeric($userId) || $userId < 1) { |
||
|
|
|||
| 57 | return ''; |
||
| 58 | } |
||
| 59 | |||
| 60 | $shrinkImages = $this->uploadConfig['shrink_large_images']; |
||
| 61 | $filePath = $file->getRealPath(); |
||
| 62 | |||
| 63 | // check for file size limit |
||
| 64 | if (!$shrinkImages && filesize($filePath) > $this->uploadConfig['max_size']) { |
||
| 65 | unlink($filePath); |
||
| 66 | |||
| 67 | return ''; |
||
| 68 | } |
||
| 69 | |||
| 70 | // Get image information |
||
| 71 | $imageInfo = getimagesize($filePath); |
||
| 72 | if (!$imageInfo) { |
||
| 73 | // file is not an image |
||
| 74 | unlink($filePath); |
||
| 75 | |||
| 76 | return ''; |
||
| 77 | } |
||
| 78 | |||
| 79 | $extension = image_type_to_extension($imageInfo[2], false); |
||
| 80 | // check for image type |
||
| 81 | if (!in_array($extension, $this->imageExtensions, true)) { |
||
| 82 | unlink($filePath); |
||
| 83 | |||
| 84 | return ''; |
||
| 85 | } |
||
| 86 | |||
| 87 | // check for image dimensions limit |
||
| 88 | $isTooLarge = $imageInfo[0] > $this->uploadConfig['max_width'] || $imageInfo[1] > $this->uploadConfig['max_height']; |
||
| 89 | |||
| 90 | if ($isTooLarge && !$shrinkImages) { |
||
| 91 | unlink($filePath); |
||
| 92 | |||
| 93 | return ''; |
||
| 94 | } |
||
| 95 | |||
| 96 | // everything's OK, so move the file |
||
| 97 | $avatarFileNameWithoutExtension = 'pers_' . $userId; |
||
| 98 | $avatarFileName = $avatarFileNameWithoutExtension . '.' . $extension; |
||
| 99 | $avatarFilePath = $this->avatarPath . '/' . $avatarFileName; |
||
| 100 | |||
| 101 | // delete old user avatar |
||
| 102 | foreach ($this->imageExtensions as $ext) { |
||
| 103 | $oldFilePath = $this->avatarPath . '/' . $avatarFileNameWithoutExtension . '.' . $ext; |
||
| 104 | if (file_exists($oldFilePath)) { |
||
| 105 | unlink($oldFilePath); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | $file->move($this->avatarPath, $avatarFileName); |
||
| 110 | |||
| 111 | if ($isTooLarge && $shrinkImages) { |
||
| 112 | $imagine = new Imagine(); |
||
| 113 | $image = $imagine->open($avatarFilePath); |
||
| 114 | $image->resize(new Box($this->uploadConfig['max_width'], $this->uploadConfig['max_height'])) |
||
| 115 | ->save($avatarFilePath); |
||
| 116 | } |
||
| 117 | |||
| 118 | chmod($avatarFilePath, 0644); |
||
| 119 | |||
| 120 | return $avatarFileName; |
||
| 121 | } |
||
| 123 |