Conditions | 13 |
Paths | 82 |
Total Lines | 73 |
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 |
||
146 | public function doPUT($request) |
||
147 | { |
||
148 | self::$logger->debug('>>doPUT($request=['.var_export($request, true).'])'); |
||
149 | |||
150 | $params = $request->getParams(); |
||
151 | |||
152 | $config = ConfigProvider::getInstance(); |
||
153 | |||
154 | try { |
||
155 | if (isset($params['articleID'])) { |
||
156 | $article = new Article(); |
||
157 | $article->load($params['articleID']); |
||
158 | |||
159 | if (isset($params['uploadBut'])) { |
||
160 | $source = $request->getFile('userfile')['tmp_name']; |
||
161 | $dest = $article->getAttachmentsLocation().'/'.$request->getFile('userfile')['name']; |
||
162 | |||
163 | // upload the file to the attachments directory |
||
164 | FileUtils::copy($source, $dest); |
||
165 | |||
166 | if (!file_exists($dest)) { |
||
167 | throw new AlphaException('Could not move the uploaded file ['.$request->getFile('userfile')['name'].']'); |
||
168 | } |
||
169 | |||
170 | // set read/write permissions on the file |
||
171 | $success = chmod($dest, 0666); |
||
172 | |||
173 | if (!$success) { |
||
174 | throw new AlphaException('Unable to set read/write permissions on the uploaded file ['.$dest.'].'); |
||
175 | } |
||
176 | |||
177 | if ($success) { |
||
178 | self::$logger->action('File '.$source.' uploaded to '.$dest); |
||
179 | $this->setStatusMessage(View::displayUpdateMessage('File '.$source.' uploaded to '.$dest)); |
||
180 | } |
||
181 | } elseif (isset($params['deletefile']) && $params['deletefile'] != '') { |
||
182 | $success = unlink($article->getAttachmentsLocation().'/'.$params['deletefile']); |
||
183 | |||
184 | if (!$success) { |
||
185 | throw new AlphaException('Could not delete the file ['.$params['deletefile'].']'); |
||
186 | } |
||
187 | |||
188 | if ($success) { |
||
189 | self::$logger->action('File '.$article->getAttachmentsLocation().'/'.$params['deletefile'].' deleted'); |
||
190 | $this->setStatusMessage(View::displayUpdateMessage('File '.$article->getAttachmentsLocation().'/'.$params['deletefile'].' deleted')); |
||
191 | } |
||
192 | } |
||
193 | |||
194 | $response = new Response(301); |
||
195 | |||
196 | if ($this->getNextJob() != '') { |
||
197 | $response->redirect($this->getNextJob()); |
||
198 | } else { |
||
199 | if ($this->request->isSecureURI()) { |
||
200 | $response->redirect(FrontController::generateSecureURL('act=Alpha\\Controller\\ActiveRecordController&ActiveRecordType=Alpha\Model\Article&ActiveRecordID='.$article->getID().'&view=edit')); |
||
201 | } else { |
||
202 | $title = str_replace(' ', $config->get('cms.url.title.separator'), $article->get('title')); |
||
203 | $response->redirect($config->get('app.url').'/a/'.$title.'/edit'); |
||
204 | } |
||
205 | } |
||
206 | |||
207 | self::$logger->debug('<<doPUT'); |
||
208 | |||
209 | return $response; |
||
210 | } else { |
||
211 | self::$logger->error('Could not process article attachment as articleID was not provided!'); |
||
212 | throw new IllegalArguementException('File not found'); |
||
213 | } |
||
214 | } catch (IllegalArguementException $e) { |
||
215 | self::$logger->error($e->getMessage()); |
||
216 | throw new ResourceNotFoundException($e->getMessage()); |
||
217 | } |
||
218 | } |
||
219 | } |
||
220 |