Conditions | 11 |
Paths | 59 |
Total Lines | 144 |
Lines | 14 |
Ratio | 9.72 % |
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 |
||
280 | public function processNotification($notificationType, |
||
281 | $resourceType, |
||
282 | $providerId, |
||
283 | $notification |
||
284 | ) { |
||
285 | try { |
||
286 | if (!\is_array($notification)) { |
||
287 | throw new BadRequestException( |
||
288 | 'server can not add remote share, missing parameter' |
||
289 | ); |
||
290 | } |
||
291 | |||
292 | $notification = \array_merge( |
||
293 | ['sharedSecret' => null], |
||
294 | $notification |
||
295 | ); |
||
296 | |||
297 | $this->ocmMiddleware->assertNotNull( |
||
298 | [ |
||
299 | 'notificationType' => $notificationType, |
||
300 | 'resourceType' => $resourceType, |
||
301 | 'providerId' => $providerId, |
||
302 | 'sharedSecret' => $notification['sharedSecret'] |
||
303 | ] |
||
304 | ); |
||
305 | |||
306 | if ($this->isSupportedResourceType($resourceType) === false) { |
||
307 | throw new NotImplementedException( |
||
308 | "ResourceType {$resourceType} is not supported" |
||
309 | ); |
||
310 | } |
||
311 | |||
312 | switch ($notificationType) { |
||
313 | View Code Duplication | case FileNotification::NOTIFICATION_TYPE_SHARE_ACCEPTED: |
|
314 | $this->ocmMiddleware->assertOutgoingSharingEnabled(); |
||
315 | $share = $this->ocmMiddleware->getValidShare( |
||
316 | $providerId, $notification['sharedSecret'] |
||
317 | ); |
||
318 | $this->fedShareManager->acceptShare($share); |
||
319 | break; |
||
320 | View Code Duplication | case FileNotification::NOTIFICATION_TYPE_SHARE_DECLINED: |
|
321 | $this->ocmMiddleware->assertOutgoingSharingEnabled(); |
||
322 | $share = $this->ocmMiddleware->getValidShare( |
||
323 | $providerId, $notification['sharedSecret'] |
||
324 | ); |
||
325 | $this->fedShareManager->declineShare($share); |
||
326 | break; |
||
327 | case FileNotification::NOTIFICATION_TYPE_REQUEST_RESHARE: |
||
328 | $this->ocmMiddleware->assertOutgoingSharingEnabled(); |
||
329 | $this->ocmMiddleware->assertNotNull( |
||
330 | [ |
||
331 | 'shareWith' => $notification['shareWith'], |
||
332 | 'senderId' => $notification['senderId'], |
||
333 | ] |
||
334 | ); |
||
335 | $share = $this->ocmMiddleware->getValidShare( |
||
336 | $providerId, $notification['sharedSecret'] |
||
337 | ); |
||
338 | |||
339 | // don't allow to share a file back to the owner |
||
340 | $owner = $share->getShareOwner(); |
||
341 | $ownerAddress = $this->addressHandler->getLocalUserFederatedAddress($owner); |
||
342 | $shareWithAddress = new Address($notification['shareWith']); |
||
343 | $this->ocmMiddleware->assertNotSameUser($ownerAddress, $shareWithAddress); |
||
344 | $this->ocmMiddleware->assertSharingPermissionSet($share); |
||
345 | |||
346 | $reShare = $this->fedShareManager->reShare( |
||
347 | $share, $notification['senderId'], |
||
348 | $notification['shareWith'] |
||
349 | ); |
||
350 | return new JSONResponse( |
||
351 | [ |
||
352 | 'sharedSecret' => $reShare->getToken(), |
||
353 | 'providerId' => $reShare->getId() |
||
354 | ], |
||
355 | Http::STATUS_CREATED |
||
356 | ); |
||
357 | break; |
||
358 | case FileNotification::NOTIFICATION_TYPE_RESHARE_CHANGE_PERMISSION: |
||
359 | // Stub. Let it fallback to the prev endpoint for now |
||
360 | return new JSONResponse( |
||
361 | ['message' => "Notification of type {$notificationType} is not supported"], |
||
362 | Http::STATUS_NOT_IMPLEMENTED |
||
363 | ); |
||
364 | |||
365 | $this->ocmMiddleware->assertNotNull( |
||
366 | [ |
||
367 | 'permission' => $notification['permission'] |
||
368 | ] |
||
369 | ); |
||
370 | $share = $this->ocmMiddleware->getValidShare( |
||
371 | $providerId, $notification['sharedSecret'] |
||
372 | ); |
||
373 | $this->fedShareManager->updateOcmPermissions( |
||
374 | $share, |
||
375 | $notification['permission'] |
||
376 | ); |
||
377 | break; |
||
378 | case FileNotification::NOTIFICATION_TYPE_SHARE_UNSHARED: |
||
379 | $this->fedShareManager->unshare( |
||
380 | $providerId, $notification['sharedSecret'] |
||
381 | ); |
||
382 | break; |
||
383 | case FileNotification::NOTIFICATION_TYPE_RESHARE_UNDO: |
||
384 | // Stub. Let it fallback to the prev endpoint for now |
||
385 | return new JSONResponse( |
||
386 | ['message' => "Notification of type {$notificationType} is not supported"], |
||
387 | Http::STATUS_NOT_IMPLEMENTED |
||
388 | ); |
||
389 | |||
390 | // owner or sender unshared a resource |
||
391 | $share = $this->ocmMiddleware->getValidShare( |
||
392 | $providerId, $notification['sharedSecret'] |
||
393 | ); |
||
394 | $this->fedShareManager->undoReshare($share); |
||
395 | break; |
||
396 | default: |
||
397 | return new JSONResponse( |
||
398 | ['message' => "Notification of type {$notificationType} is not supported"], |
||
399 | Http::STATUS_NOT_IMPLEMENTED |
||
400 | ); |
||
401 | } |
||
402 | } catch (OcmException $e) { |
||
403 | return new JSONResponse( |
||
404 | ['message' => $e->getMessage()], |
||
405 | $e->getHttpStatusCode() |
||
406 | ); |
||
407 | } catch (\Exception $e) { |
||
408 | $this->logger->error( |
||
409 | "server can not process notification, {$e->getMessage()}", |
||
410 | ['app' => 'federatefilesharing'] |
||
411 | ); |
||
412 | return new JSONResponse( |
||
413 | [ |
||
414 | 'message' => "internal server error, was not able to process notification" |
||
415 | ], |
||
416 | Http::STATUS_INTERNAL_SERVER_ERROR |
||
417 | ); |
||
418 | } |
||
419 | return new JSONResponse( |
||
420 | [], |
||
421 | Http::STATUS_CREATED |
||
422 | ); |
||
423 | } |
||
424 | |||
464 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.