Conditions | 13 |
Paths | 20 |
Total Lines | 74 |
Code Lines | 37 |
Lines | 3 |
Ratio | 4.05 % |
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 |
||
34 | public function galleryUpload(string $id) |
||
35 | { |
||
36 | $this->setJsonHeader(); |
||
37 | |||
38 | // check if id is passed |
||
39 | if (Str::likeEmpty($id)) { |
||
40 | throw new NativeException('Wrong input data'); |
||
41 | } |
||
42 | |||
43 | // check if user have permission to access there |
||
44 | View Code Duplication | if (!App::$User->isAuth() || !App::$User->identity()->role->can('global/file')) { |
|
|
|||
45 | throw new NativeException(__('Permissions to upload is denied')); |
||
46 | } |
||
47 | |||
48 | // check if directory exist |
||
49 | if (!Directory::exist('/upload/gallery/' . $id)) { |
||
50 | Directory::create('/upload/gallery/' . $id); |
||
51 | } |
||
52 | |||
53 | // get file object |
||
54 | /** @var $file \Symfony\Component\HttpFoundation\File\UploadedFile */ |
||
55 | $file = $this->request->files->get('file'); |
||
56 | if ($file === null || $file->getError() !== 0) { |
||
57 | throw new NativeException(__('Unexpected error in upload process')); |
||
58 | } |
||
59 | |||
60 | // check file size |
||
61 | if ($file->getSize() < 1 || $file->getSize() > $this->maxSize) { |
||
62 | throw new ForbiddenException(__('File size is too big. Max size: %size%kb', ['size' => (int)($this->maxSize/1024)])); |
||
63 | } |
||
64 | |||
65 | // check file extension |
||
66 | if (!Arr::in($file->guessExtension(), $this->allowedExt)) { |
||
67 | throw new ForbiddenException(__('File extension is not allowed to upload. Allowed: %s%', ['s' => implode(', ', $this->allowedExt)])); |
||
68 | } |
||
69 | |||
70 | // create origin directory |
||
71 | $originPath = '/upload/gallery/' . $id . '/orig/'; |
||
72 | if (!Directory::exist($originPath)) { |
||
73 | Directory::create($originPath); |
||
74 | } |
||
75 | |||
76 | // lets make a new file name |
||
77 | $fileName = App::$Security->simpleHash($file->getClientOriginalName() . $file->getSize()); |
||
78 | $fileNewName = $fileName . '.' . $file->guessExtension(); |
||
79 | // check if image is already loaded |
||
80 | if (File::exist($originPath . $fileNewName)) { |
||
81 | throw new ForbiddenException(__('File is always exists!')); |
||
82 | } |
||
83 | // save file from tmp to gallery origin directory |
||
84 | $file->move(Normalize::diskFullPath($originPath), $fileNewName); |
||
85 | |||
86 | // lets resize preview image for it |
||
87 | $thumbPath = '/upload/gallery/' . $id . '/thumb/'; |
||
88 | if (!Directory::exist($thumbPath)) { |
||
89 | Directory::create($thumbPath); |
||
90 | } |
||
91 | |||
92 | $thumb = new Image(); |
||
93 | $thumb->setCacheDir(root . '/Private/Cache/images'); |
||
94 | |||
95 | // open original file, resize it and save |
||
96 | $thumbSaveName = Normalize::diskFullPath($thumbPath) . '/' . $fileName . '.jpg'; |
||
97 | $thumb->open(Normalize::diskFullPath($originPath) . DIRECTORY_SEPARATOR . $fileNewName) |
||
98 | ->cropResize($this->maxResize) |
||
99 | ->save($thumbSaveName, 'jpg', 90); |
||
100 | $thumb = null; |
||
101 | |||
102 | return json_encode(['status' => 1, 'file' => [ |
||
103 | 'thumbnailUrl' => '/upload/gallery/' . $id . '/thumb/' . $fileName . '.jpg', |
||
104 | 'url' => '/upload/gallery/' . $id . '/orig/' . $fileNewName, |
||
105 | 'name' => $fileNewName |
||
106 | ]]); |
||
107 | } |
||
108 | } |
||
109 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.