Complex classes like ShareController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ShareController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 36 | class ShareController extends ApiController { |
||
| 37 | private $userId; |
||
| 38 | private $activityService; |
||
| 39 | private $groupManager; |
||
| 40 | private $userManager; |
||
| 41 | private $vaultService; |
||
| 42 | private $shareService; |
||
| 43 | private $credentialService; |
||
| 44 | private $notificationService; |
||
| 45 | private $fileService; |
||
| 46 | private $settings; |
||
| 47 | |||
| 48 | private $limit = 50; |
||
| 49 | private $offset = 0; |
||
| 50 | |||
| 51 | public function __construct($AppName, |
||
| 52 | IRequest $request, |
||
| 53 | $UserId, |
||
| 54 | IGroupManager $groupManager, |
||
| 55 | IUserManager $userManager, |
||
| 56 | ActivityService $activityService, |
||
| 57 | VaultService $vaultService, |
||
| 58 | ShareService $shareService, |
||
| 59 | CredentialService $credentialService, |
||
| 60 | NotificationService $notificationService, |
||
| 61 | FileService $fileService, |
||
| 62 | SettingsService $config |
||
| 63 | ) { |
||
| 64 | parent::__construct($AppName, $request); |
||
| 65 | |||
| 66 | $this->userId = $UserId; |
||
| 67 | $this->userManager = $userManager; |
||
| 68 | $this->groupManager = $groupManager; |
||
| 69 | $this->activityService = $activityService; |
||
| 70 | $this->vaultService = $vaultService; |
||
| 71 | $this->shareService = $shareService; |
||
| 72 | $this->credentialService = $credentialService; |
||
| 73 | $this->notificationService = $notificationService; |
||
| 74 | $this->fileService = $fileService; |
||
| 75 | $this->settings = $config; |
||
| 76 | } |
||
| 77 | |||
| 78 | |||
| 79 | /** |
||
| 80 | * @param $item_id |
||
| 81 | * @param $item_guid |
||
| 82 | * @param $permissions |
||
| 83 | * @param $expire_timestamp |
||
| 84 | * @NoAdminRequired |
||
| 85 | * @NoCSRFRequired |
||
| 86 | */ |
||
| 87 | public function createPublicShare($item_id, $item_guid, $permissions, $expire_timestamp, $expire_views) { |
||
| 88 | try { |
||
| 89 | $credential = $this->credentialService->getCredentialByGUID($item_guid); |
||
| 90 | } catch (\Exception $exception) { |
||
| 91 | return new NotFoundResponse(); |
||
| 92 | } |
||
| 93 | |||
| 94 | try { |
||
| 95 | $acl = $this->shareService->getACL(null, $item_guid); |
||
| 96 | } catch (\Exception $exception) { |
||
| 97 | $acl = new SharingACL(); |
||
| 98 | } |
||
| 99 | |||
| 100 | |||
| 101 | $acl->setItemId($item_id); |
||
| 102 | $acl->setItemGuid($item_guid); |
||
| 103 | $acl->setPermissions($permissions); |
||
| 104 | $acl->setExpire($expire_timestamp); |
||
| 105 | $acl->setExpireViews($expire_views); |
||
| 106 | if (!$acl->getId()) { |
||
| 107 | $this->shareService->createACLEntry($acl); |
||
| 108 | |||
| 109 | $this->activityService->add( |
||
| 110 | 'item_shared_publicly', [$credential->getLabel()], |
||
| 111 | '', array(), |
||
| 112 | '', $this->userId->getUID(), Activity::TYPE_ITEM_SHARED); |
||
| 113 | } else { |
||
| 114 | $this->shareService->updateCredentialACL($acl); |
||
| 115 | } |
||
| 116 | |||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @NoAdminRequired |
||
| 121 | * @NoCSRFRequired |
||
| 122 | */ |
||
| 123 | public function applyIntermediateShare($item_id, $item_guid, $vaults, $permissions) { |
||
| 124 | /** |
||
| 125 | * Assemble notification |
||
| 126 | */ |
||
| 127 | //@TODO add expire_time |
||
| 128 | //@TODO add expire_views |
||
| 129 | $credential = $this->credentialService->getCredentialById($item_id, $this->userId->getUID()); |
||
| 130 | $credential_owner = $credential->getUserId(); |
||
| 131 | |||
| 132 | $first_vault = $vaults[0]; |
||
| 133 | try { |
||
| 134 | $shareRequests = $this->shareService->getPendingShareRequestsForCredential($item_guid, $first_vault['user_id']); |
||
| 135 | if (count($shareRequests) > 0) { |
||
| 136 | return new JSONResponse(array('error' => 'User got already pending requests')); |
||
| 137 | } |
||
| 138 | } catch (\Exception $exception) { |
||
| 139 | // no need to catch this |
||
| 140 | } |
||
| 141 | |||
| 142 | $acl = null; |
||
| 143 | try { |
||
| 144 | $acl = $this->shareService->getCredentialAclForUser($first_vault['user_id'], $item_guid); |
||
| 145 | } catch (\Exception $exception) { |
||
| 146 | // no need to catch this |
||
| 147 | } |
||
| 148 | |||
| 149 | if ($acl) { |
||
| 150 | return new JSONResponse(array('error' => 'User got already this credential')); |
||
| 151 | } |
||
| 152 | |||
| 153 | $result = $this->shareService->createBulkRequests($item_id, $item_guid, $vaults, $permissions, $credential_owner); |
||
| 154 | if ($credential) { |
||
| 155 | $processed_users = array(); |
||
| 156 | foreach ($result as $vault) { |
||
| 157 | if (!in_array($vault->getTargetUserId(), $processed_users)) { |
||
| 158 | $target_user = $vault->getTargetUserId(); |
||
| 159 | $notification = array( |
||
| 160 | 'from_user' => ucfirst($this->userId->getDisplayName()), |
||
| 161 | 'credential_label' => $credential->getLabel(), |
||
| 162 | 'credential_id' => $credential->getId(), |
||
| 163 | 'item_id' => $credential->getId(), |
||
| 164 | 'target_user' => $target_user, |
||
| 165 | 'req_id' => $vault->getId() |
||
| 166 | ); |
||
| 167 | $this->notificationService->credentialSharedNotification( |
||
| 168 | $notification |
||
| 169 | ); |
||
| 170 | array_push($processed_users, $target_user); |
||
| 171 | |||
| 172 | $this->activityService->add( |
||
| 173 | 'item_shared', [$credential->getLabel(), $target_user], |
||
| 174 | '', array(), |
||
| 175 | '', $this->userId->getUID(), Activity::TYPE_ITEM_SHARED); |
||
| 176 | |||
| 177 | |||
| 178 | $this->activityService->add( |
||
| 179 | 'item_share_received', [$credential->getLabel(), $this->userId->getUID()], |
||
| 180 | '', array(), |
||
| 181 | '', $target_user, Activity::TYPE_ITEM_SHARED); |
||
| 182 | } |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | |||
| 187 | return new JSONResponse($result); |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @NoAdminRequired |
||
| 192 | * @NoCSRFRequired |
||
| 193 | */ |
||
| 194 | public function searchUsers($search) { |
||
| 209 | |||
| 210 | |||
| 211 | /** |
||
| 212 | * @NoAdminRequired |
||
| 213 | * @NoCSRFRequired |
||
| 214 | */ |
||
| 215 | public function unshareCredential($item_guid) { |
||
| 232 | |||
| 233 | |||
| 234 | public function unshareCredentialFromUser($item_guid, $user_id) { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @NoAdminRequired |
||
| 265 | * @NoCSRFRequired |
||
| 266 | */ |
||
| 267 | public function search($search) { |
||
| 271 | |||
| 272 | |||
| 273 | /** |
||
| 274 | * @NoAdminRequired |
||
| 275 | * @NoCSRFRequired |
||
| 276 | */ |
||
| 277 | public function getVaultsByUser($user_id) { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @NoAdminRequired |
||
| 294 | * @NoCSRFRequired |
||
| 295 | */ |
||
| 296 | public function savePendingRequest($item_guid, $target_vault_guid, $final_shared_key) { |
||
| 324 | |||
| 325 | /** |
||
| 326 | * @NoAdminRequired |
||
| 327 | * @NoCSRFRequired |
||
| 328 | */ |
||
| 329 | public function getPendingRequests() { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param $item_guid |
||
| 347 | * @return JSONResponse |
||
| 348 | * @NoAdminRequired |
||
| 349 | * @NoCSRFRequired |
||
| 350 | */ |
||
| 351 | public function getRevisions($item_guid) { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Obtains the list of credentials shared with this vault |
||
| 361 | * |
||
| 362 | * @NoAdminRequired |
||
| 363 | * @NoCSRFRequired |
||
| 364 | */ |
||
| 365 | public function getVaultItems($vault_guid) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @param $share_request_id |
||
| 375 | * @return JSONResponse |
||
| 376 | * @NoAdminRequired |
||
| 377 | * @NoCSRFRequired |
||
| 378 | */ |
||
| 379 | public function deleteShareRequest($share_request_id) { |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @param $credential_guid |
||
| 410 | * @return JSONResponse |
||
| 411 | * @NoAdminRequired |
||
| 412 | * @NoCSRFRequired |
||
| 413 | * @PublicPage |
||
| 414 | */ |
||
| 415 | public function getPublicCredentialData($credential_guid) { |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @param $item_guid |
||
| 443 | * @return JSONResponse |
||
| 444 | * @NoAdminRequired |
||
| 445 | * @NoCSRFRequired |
||
| 446 | */ |
||
| 447 | public function getItemAcl($item_guid) { |
||
| 465 | |||
| 466 | /** |
||
| 467 | * @param $credential_guid |
||
| 468 | * @param $file_guid |
||
| 469 | * @NoAdminRequired |
||
| 470 | * @PublicPage |
||
| 471 | * @return JSONResponse |
||
| 472 | * @return NotFoundResponse |
||
| 473 | */ |
||
| 474 | public function getFile($item_guid, $file_guid) { |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @param $item_guid |
||
| 491 | * @param $user_id |
||
| 492 | * @param $permission |
||
| 493 | * @return JSONResponse |
||
| 494 | * @NoAdminRequired |
||
| 495 | * @NoCSRFRequired |
||
| 496 | */ |
||
| 497 | public function updateSharedCredentialACL($item_guid, $user_id, $permission) { |
||
| 519 | } |