Conditions | 31 |
Paths | 492 |
Total Lines | 133 |
Code Lines | 75 |
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 |
||
41 | public function execute(Request $request, WorkingFolder $workingFolder, EventDispatcher $dispatcher, Config $config, CacheManager $cache, ThumbnailRepository $thumbsRepository) |
||
42 | { |
||
43 | // #111 IE9 download JSON issue workaround |
||
44 | if ($request->get('asPlainText')) { |
||
45 | $uploadEvents = array( |
||
46 | CKFinderEvent::AFTER_COMMAND_FILE_UPLOAD, |
||
47 | CKFinderEvent::AFTER_COMMAND_QUICK_UPLOAD |
||
48 | ); |
||
49 | |||
50 | foreach ($uploadEvents as $eventName) { |
||
51 | $dispatcher->addListener($eventName, function (AfterCommandEvent $event) { |
||
52 | $response = $event->getResponse(); |
||
53 | $response->headers->set('Content-Type', 'text/plain'); |
||
54 | }); |
||
55 | } |
||
56 | } |
||
57 | |||
58 | $uploaded = 0; |
||
59 | |||
60 | $warningErrorCode = null; |
||
61 | $upload = $request->files->get('upload'); |
||
62 | |||
63 | if (null === $upload) { |
||
64 | throw new InvalidUploadException(); |
||
65 | } |
||
66 | |||
67 | $uploadedFile = new UploadedFile($upload, $this->app); |
||
68 | |||
69 | if (!$uploadedFile->isValid()) { |
||
70 | throw new InvalidUploadException($uploadedFile->getErrorMessage()); |
||
71 | } |
||
72 | |||
73 | $uploadedFile->sanitizeFilename(); |
||
74 | |||
75 | if ($uploadedFile->wasRenamed()) { |
||
76 | $warningErrorCode = Error::UPLOADED_INVALID_NAME_RENAMED; |
||
77 | } |
||
78 | |||
79 | if (!$uploadedFile->hasValidFilename() || $uploadedFile->isHiddenFile()) { |
||
80 | throw new InvalidNameException(); |
||
81 | } |
||
82 | |||
83 | if (!$uploadedFile->hasAllowedExtension()) { |
||
84 | throw new InvalidExtensionException(); |
||
85 | } |
||
86 | |||
87 | // Autorename if required |
||
88 | $overwriteOnUpload = $config->get('overwriteOnUpload'); |
||
89 | if (!$overwriteOnUpload && $uploadedFile->autorename()) { |
||
90 | $warningErrorCode = Error::UPLOADED_FILE_RENAMED; |
||
91 | } |
||
92 | |||
93 | $fileName = $uploadedFile->getFilename(); |
||
94 | |||
95 | if (!$uploadedFile->isAllowedHtmlFile() && $uploadedFile->containsHtml()) { |
||
96 | throw new InvalidUploadException('HTML detected in disallowed file type', Error::UPLOADED_WRONG_HTML_FILE); |
||
97 | } |
||
98 | |||
99 | if ($config->get('secureImageUploads') && $uploadedFile->isImage() && !$uploadedFile->isValidImage()) { |
||
100 | throw new InvalidUploadException('Invalid upload: corrupted image', Error::UPLOADED_CORRUPT); |
||
101 | } |
||
102 | |||
103 | $maxFileSize = $workingFolder->getResourceType()->getMaxSize(); |
||
104 | |||
105 | if (!$config->get('checkSizeAfterScaling') && $maxFileSize && $uploadedFile->getSize() > $maxFileSize) { |
||
106 | throw new InvalidUploadException('Uploaded file is too big', Error::UPLOADED_TOO_BIG); |
||
107 | } |
||
108 | |||
109 | if (Image::isSupportedExtension($uploadedFile->getExtension())) { |
||
110 | $imagesConfig = $config->get('images'); |
||
111 | $image = Image::create($uploadedFile->getContents()); |
||
112 | |||
113 | if ($imagesConfig['maxWidth'] && $image->getWidth() > $imagesConfig['maxWidth'] || |
||
|
|||
114 | $imagesConfig['maxHeight'] && $image->getHeight() > $imagesConfig['maxHeight']) { |
||
115 | $image->resize($imagesConfig['maxWidth'], $imagesConfig['maxHeight'], $imagesConfig['quality']); |
||
116 | $imageData = $image->getData(); |
||
117 | $uploadedFile->save($imageData); |
||
118 | } |
||
119 | |||
120 | $cache->set( |
||
121 | Path::combine( |
||
122 | $workingFolder->getResourceType()->getName(), |
||
123 | $workingFolder->getClientCurrentFolder(), |
||
124 | $fileName), |
||
125 | $image->getInfo() |
||
126 | ); |
||
127 | |||
128 | unset($imageData); |
||
129 | unset($image); |
||
130 | } |
||
131 | |||
132 | if ($maxFileSize && $uploadedFile->getSize() > $maxFileSize) { |
||
133 | throw new InvalidUploadException('Uploaded file is too big', Error::UPLOADED_TOO_BIG); |
||
134 | } |
||
135 | |||
136 | $event = new FileUploadEvent($this->app, $uploadedFile); |
||
137 | $dispatcher->dispatch(CKFinderEvent::FILE_UPLOAD, $event); |
||
138 | |||
139 | if (!$event->isPropagationStopped()) { |
||
140 | $uploadedFileStream = $uploadedFile->getContentsStream(); |
||
141 | $uploaded = (int) $workingFolder->putStream($fileName, $uploadedFileStream, $uploadedFile->getMimeType()); |
||
142 | |||
143 | if (is_resource($uploadedFileStream)) { |
||
144 | fclose($uploadedFileStream); |
||
145 | } |
||
146 | |||
147 | if ($overwriteOnUpload) { |
||
148 | $thumbsRepository->deleteThumbnails( |
||
149 | $workingFolder->getResourceType(), |
||
150 | $workingFolder->getClientCurrentFolder(), |
||
151 | $fileName |
||
152 | ); |
||
153 | } |
||
154 | |||
155 | if (!$uploaded) { |
||
156 | $warningErrorCode = Error::ACCESS_DENIED; |
||
157 | } |
||
158 | } |
||
159 | |||
160 | $responseData = array( |
||
161 | 'fileName' => $fileName, |
||
162 | 'uploaded' => $uploaded |
||
163 | ); |
||
164 | |||
165 | if ($warningErrorCode) { |
||
166 | $errorMessage = $this->app['translator']->translateErrorMessage($warningErrorCode, array('name' => $fileName)); |
||
167 | $responseData['error'] = array( |
||
168 | 'number' => $warningErrorCode, |
||
169 | 'message' => $errorMessage |
||
170 | ); |
||
171 | } |
||
172 | |||
173 | return $responseData; |
||
174 | } |
||
176 |