Conditions | 11 |
Paths | 483 |
Total Lines | 90 |
Code Lines | 53 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 |
||
188 | public function save(){ |
||
189 | $response = new JSONResponse(); |
||
190 | try { |
||
191 | $esId = $this->request->server['HTTP_WEBODF_SESSION_ID']; |
||
192 | $session = $this->loadSession($esId); |
||
193 | |||
194 | $memberId = $this->request->server['HTTP_WEBODF_MEMBER_ID']; |
||
195 | $currentMember = $this->loadMember($memberId, $esId); |
||
196 | |||
197 | // Extra info for future usage |
||
198 | // $sessionRevision = $this->request->server['HTTP_WEBODF_SESSION_REVISION']; |
||
199 | |||
200 | //NB ouch! New document content is passed as an input stream content |
||
201 | $stream = fopen('php://input','r'); |
||
202 | if (!$stream){ |
||
203 | throw new \Exception('New content missing'); |
||
204 | } |
||
205 | $content = stream_get_contents($stream); |
||
206 | |||
207 | try { |
||
208 | if ($currentMember->getIsGuest()){ |
||
209 | $file = File::getByShareToken($currentMember->getToken()); |
||
210 | } else { |
||
211 | $file = new File($session->getFileId()); |
||
212 | } |
||
213 | |||
214 | $view = $file->getOwnerView(true); |
||
215 | $path = $file->getPath(true); |
||
216 | } catch (\Exception $e){ |
||
217 | //File was deleted or unshared. We need to save content as new file anyway |
||
218 | //Sorry, but for guests it would be lost :( |
||
219 | if ($this->uid){ |
||
220 | $view = new View('/' . $this->uid . '/files'); |
||
221 | |||
222 | $dir = \OC::$server->getConfig()->getUserValue($this->uid, 'documents', 'save_path', ''); |
||
223 | $path = Helper::getNewFileName($view, $dir . 'New Document.odt'); |
||
224 | } else { |
||
225 | throw $e; |
||
226 | } |
||
227 | } |
||
228 | |||
229 | $member = new Db\Member(); |
||
230 | $members = $member->getActiveCollection($esId); |
||
231 | $memberIds = array_map( |
||
232 | function($x){ |
||
233 | return ($x['member_id']); |
||
234 | }, |
||
235 | $members |
||
236 | ); |
||
237 | |||
238 | // Active users except current user |
||
239 | $memberCount = count($memberIds) - 1; |
||
240 | |||
241 | if ($view->file_exists($path)){ |
||
242 | $currentHash = $view->hash('sha1', $path, false); |
||
243 | |||
244 | if (!Helper::isVersionsEnabled() && $currentHash !== $session->getGenesisHash()){ |
||
245 | // Original file was modified externally. Save to a new one |
||
246 | $path = Helper::getNewFileName($view, $path, '-conflict'); |
||
247 | } |
||
248 | |||
249 | $mimetype = $view->getMimeType($path); |
||
250 | } else { |
||
251 | $mimetype = Storage::MIMETYPE_LIBREOFFICE_WORDPROCESSOR; |
||
252 | } |
||
253 | |||
254 | $data = Filter::write($content, $mimetype); |
||
255 | |||
256 | if ($view->file_put_contents($path, $data['content'])){ |
||
257 | // Not a last user |
||
258 | if ($memberCount>0){ |
||
259 | // Update genesis hash to prevent conflicts |
||
260 | $this->logger->debug('Update hash', [ 'app' => $this->appName ]); |
||
261 | $session->updateGenesisHash($esId, sha1($data['content'])); |
||
262 | } else { |
||
263 | // Last user. Kill session data |
||
264 | Db\Session::cleanUp($esId); |
||
265 | } |
||
266 | |||
267 | $view->touch($path); |
||
268 | } |
||
269 | $response->setData(['status'=>'success']); |
||
270 | } catch (\Exception $e){ |
||
271 | $response->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR); |
||
272 | $response->setData([]); |
||
273 | $this->logger->warning('Saving failed. Reason:' . $e->getMessage(), [ 'app' => $this->appName ]); |
||
274 | } |
||
275 | |||
276 | return $response; |
||
277 | } |
||
278 | |||
322 |