| Conditions | 25 |
| Paths | 2928 |
| Total Lines | 118 |
| Code Lines | 58 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 namespace App\Services; |
||
| 59 | public function handleUpload(Request $request) |
||
| 60 | { |
||
| 61 | $fineUploaderUuid = null; |
||
| 62 | if ($request->has('qquuid')) { |
||
| 63 | $fineUploaderUuid = $request->get('qquuid'); |
||
| 64 | } |
||
| 65 | |||
| 66 | //------------------------------ |
||
| 67 | // Is it Post-processing? |
||
| 68 | //------------------------------ |
||
| 69 | |||
| 70 | if ($request->has('post-process') && $request->get('post-process') == 1) { |
||
| 71 | $validator = app('validator')->make($request->all(), [ |
||
| 72 | 'qquuid' => 'required|string|size:36', |
||
| 73 | 'qqfilename' => 'required|string', |
||
| 74 | 'qqtotalfilesize' => 'required|numeric', |
||
| 75 | 'qqtotalparts' => 'required|numeric' |
||
| 76 | ]); |
||
| 77 | if ($validator->fails()) { |
||
| 78 | throw new ValidationException($validator); |
||
| 79 | } |
||
| 80 | |||
| 81 | # Combine chunks. |
||
| 82 | $this->combineChunks($request); |
||
| 83 | |||
| 84 | # Real MIME validation of the uploaded file. |
||
| 85 | $this->validateUploadRealMimeAgainstAllowedTypes($this->getAbsolutePath($fineUploaderUuid)); |
||
| 86 | |||
| 87 | # Move file to its final permanent destination. |
||
| 88 | $hash = hash_file('sha256', $this->getAbsolutePath($fineUploaderUuid)); |
||
| 89 | $destination = $this->renameAndMoveUploadedFileByItsHash($fineUploaderUuid, $hash); |
||
| 90 | |||
| 91 | # Persist file record in database. |
||
| 92 | $this->persistDatabaseRecord(new SymfonyFile($this->getAbsolutePath($destination))); |
||
| 93 | |||
| 94 | return response()->json(['message' => 'Created', 'success' => true, 'uuid' => $fineUploaderUuid])->setStatusCode(IlluminateResponse::HTTP_CREATED); |
||
| 95 | } |
||
| 96 | |||
| 97 | //---------------- |
||
| 98 | // Prelim work. |
||
| 99 | //---------------- |
||
| 100 | |||
| 101 | $filesystem = app('filesystem')->disk(); |
||
| 102 | |||
| 103 | if (!file_exists($this->temporaryChunksFolder) || !is_dir($this->temporaryChunksFolder)) { |
||
| 104 | $filesystem->makeDirectory($this->temporaryChunksFolder); |
||
| 105 | } |
||
| 106 | |||
| 107 | # Temp folder writable? |
||
| 108 | if (!is_writable($absolutePathToTemporaryChunksFolder = config('filesystems.disks.local.root') . $this->temporaryChunksFolder) || !is_executable($absolutePathToTemporaryChunksFolder)) { |
||
| 109 | throw new FileStreamExceptions\TemporaryUploadFolderNotWritableException; |
||
| 110 | } |
||
| 111 | |||
| 112 | # Cleanup chunks. |
||
| 113 | if (1 === mt_rand(1, 1 / $this->chunksCleanupProbability)) { |
||
| 114 | $this->cleanupChunks(); |
||
| 115 | } |
||
| 116 | |||
| 117 | # Check upload size against the size-limit, if any. |
||
| 118 | if (!empty($this->sizeLimit)) { |
||
| 119 | $uploadIsTooLarge = false; |
||
| 120 | $request->has('qqtotalfilesize') && intval($request->get('qqtotalfilesize')) > $this->sizeLimit && $uploadIsTooLarge = true; |
||
| 121 | $this->filesizeFromHumanReadableToBytes(ini_get('post_max_size')) < $this->sizeLimit && $uploadIsTooLarge = true; |
||
| 122 | $this->filesizeFromHumanReadableToBytes(ini_get('upload_max_filesize')) < $this->sizeLimit && $uploadIsTooLarge = true; |
||
| 123 | if ($uploadIsTooLarge) { |
||
| 124 | throw new FileStreamExceptions\UploadTooLargeException; |
||
| 125 | } |
||
| 126 | } |
||
| 127 | |||
| 128 | # Is there attempt for multiple file uploads? |
||
| 129 | $collectionOfUploadedFiles = collect($request->file()); |
||
| 130 | if ($collectionOfUploadedFiles->count() > 1) { |
||
| 131 | throw new FileStreamExceptions\MultipleSimultaneousUploadsNotAllowedException; |
||
| 132 | } |
||
| 133 | |||
| 134 | /** @var UploadedFile $file */ |
||
| 135 | $file = $collectionOfUploadedFiles->first(); |
||
| 136 | |||
| 137 | //-------------------- |
||
| 138 | // Upload handling. |
||
| 139 | //-------------------- |
||
| 140 | |||
| 141 | if ($file->getSize() == 0) { |
||
| 142 | throw new FileStreamExceptions\UploadIsEmptyException; |
||
| 143 | } |
||
| 144 | |||
| 145 | $name = $file->getClientOriginalName(); |
||
| 146 | if ($request->has('qqfilename')) { |
||
| 147 | $name = $request->get('qqfilename'); |
||
| 148 | } |
||
| 149 | if (empty($name)) { |
||
| 150 | throw new FileStreamExceptions\UploadFilenameIsEmptyException; |
||
| 151 | } |
||
| 152 | |||
| 153 | $totalNumberOfChunks = $request->has('qqtotalparts') ? $request->get('qqtotalparts') : 1; |
||
| 154 | |||
| 155 | if ($totalNumberOfChunks > 1) { |
||
| 156 | $chunkIndex = intval($request->get('qqpartindex')); |
||
| 157 | $targetFolder = $this->temporaryChunksFolder . DIRECTORY_SEPARATOR . $fineUploaderUuid; |
||
| 158 | if (!$filesystem->exists($targetFolder)) { |
||
| 159 | $filesystem->makeDirectory($targetFolder); |
||
| 160 | } |
||
| 161 | |||
| 162 | if (!$file->isValid()) { |
||
| 163 | throw new FileStreamExceptions\UploadAttemptFailedException; |
||
| 164 | } |
||
| 165 | $file->move(storage_path('app' . $targetFolder), $chunkIndex); |
||
| 166 | |||
| 167 | return response()->json(['success' => true, 'uuid' => $fineUploaderUuid]); |
||
| 168 | } else { |
||
| 169 | if (!$file->isValid()) { |
||
| 170 | throw new FileStreamExceptions\UploadAttemptFailedException; |
||
| 171 | } |
||
| 172 | $file->move(storage_path('app'), $name); |
||
| 173 | |||
| 174 | return response()->json(['success' => true, 'uuid' => $fineUploaderUuid])->setStatusCode(IlluminateResponse::HTTP_CREATED); |
||
| 175 | } |
||
| 176 | } |
||
| 177 | |||
| 343 |