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 |
||
| 38 | class ShareController extends ApiController { |
||
| 39 | private $userId; |
||
| 40 | private $activityService; |
||
| 41 | private $groupManager; |
||
| 42 | private $userManager; |
||
| 43 | private $vaultService; |
||
| 44 | private $shareService; |
||
| 45 | private $credentialService; |
||
| 46 | private $notificationService; |
||
| 47 | private $fileService; |
||
| 48 | private $settings; |
||
| 49 | |||
| 50 | private $limit = 50; |
||
| 51 | private $offset = 0; |
||
| 52 | |||
| 53 | public function __construct($AppName, |
||
| 54 | IRequest $request, |
||
| 55 | $UserId, |
||
| 56 | IGroupManager $groupManager, |
||
| 57 | IUserManager $userManager, |
||
| 58 | ActivityService $activityService, |
||
| 59 | VaultService $vaultService, |
||
| 60 | ShareService $shareService, |
||
| 61 | CredentialService $credentialService, |
||
| 62 | NotificationService $notificationService, |
||
| 63 | FileService $fileService, |
||
| 64 | SettingsService $config |
||
| 65 | ) { |
||
| 66 | parent::__construct($AppName, $request); |
||
| 67 | |||
| 68 | $this->userId = $UserId; |
||
| 69 | $this->userManager = $userManager; |
||
| 70 | $this->groupManager = $groupManager; |
||
| 71 | $this->activityService = $activityService; |
||
| 72 | $this->vaultService = $vaultService; |
||
| 73 | $this->shareService = $shareService; |
||
| 74 | $this->credentialService = $credentialService; |
||
| 75 | $this->notificationService = $notificationService; |
||
| 76 | $this->fileService = $fileService; |
||
| 77 | $this->settings = $config; |
||
| 78 | } |
||
| 79 | |||
| 80 | |||
| 81 | /** |
||
| 82 | * @param $item_id |
||
| 83 | * @param $item_guid |
||
| 84 | * @param $permissions |
||
| 85 | * @param $expire_timestamp |
||
| 86 | * @NoAdminRequired |
||
| 87 | * @NoCSRFRequired |
||
| 88 | */ |
||
| 89 | public function createPublicShare($item_id, $item_guid, $permissions, $expire_timestamp, $expire_views) { |
||
| 90 | |||
| 91 | if (!$this->settings->isEnabled('link_sharing_enabled')) { |
||
| 92 | return new JSONResponse(array()); |
||
| 93 | } |
||
| 94 | |||
| 95 | try { |
||
| 96 | $credential = $this->credentialService->getCredentialByGUID($item_guid); |
||
| 97 | } catch (\Exception $exception) { |
||
| 98 | return new NotFoundResponse(); |
||
| 99 | } |
||
| 100 | |||
| 101 | try { |
||
| 102 | $acl = $this->shareService->getACL(null, $item_guid); |
||
| 103 | } catch (\Exception $exception) { |
||
| 104 | $acl = new SharingACL(); |
||
| 105 | } |
||
| 106 | |||
| 107 | |||
| 108 | $acl->setItemId($item_id); |
||
| 109 | $acl->setItemGuid($item_guid); |
||
| 110 | $acl->setPermissions($permissions); |
||
| 111 | $acl->setExpire($expire_timestamp); |
||
| 112 | $acl->setExpireViews($expire_views); |
||
| 113 | if (!$acl->getId()) { |
||
| 114 | $this->shareService->createACLEntry($acl); |
||
| 115 | |||
| 116 | $this->activityService->add( |
||
| 117 | 'item_shared_publicly', [$credential->getLabel()], |
||
| 118 | '', array(), |
||
| 119 | '', $this->userId->getUID(), Activity::TYPE_ITEM_SHARED); |
||
| 120 | } else { |
||
| 121 | $this->shareService->updateCredentialACL($acl); |
||
| 122 | } |
||
| 123 | |||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @NoAdminRequired |
||
| 128 | * @NoCSRFRequired |
||
| 129 | */ |
||
| 130 | public function applyIntermediateShare($item_id, $item_guid, $vaults, $permissions) { |
||
| 131 | if (!$this->settings->isEnabled('user_sharing_enabled')) { |
||
| 132 | return new JSONResponse(array()); |
||
| 133 | } |
||
| 134 | /** |
||
| 135 | * Assemble notification |
||
| 136 | */ |
||
| 137 | //@TODO add expire_time |
||
| 138 | //@TODO add expire_views |
||
| 139 | $credential = $this->credentialService->getCredentialById($item_id, $this->userId->getUID()); |
||
| 140 | $credential_owner = $credential->getUserId(); |
||
| 141 | |||
| 142 | $first_vault = $vaults[0]; |
||
| 143 | try { |
||
| 144 | $shareRequests = $this->shareService->getPendingShareRequestsForCredential($item_guid, $first_vault['user_id']); |
||
| 145 | if (count($shareRequests) > 0) { |
||
| 146 | return new JSONResponse(array('error' => 'User got already pending requests')); |
||
| 147 | } |
||
| 148 | } catch (\Exception $exception) { |
||
| 149 | // no need to catch this |
||
| 150 | } |
||
| 151 | |||
| 152 | $acl = null; |
||
| 153 | try { |
||
| 154 | $acl = $this->shareService->getCredentialAclForUser($first_vault['user_id'], $item_guid); |
||
| 155 | } catch (\Exception $exception) { |
||
| 156 | // no need to catch this |
||
| 157 | } |
||
| 158 | |||
| 159 | if ($acl) { |
||
| 160 | return new JSONResponse(array('error' => 'User got already this credential')); |
||
| 161 | } |
||
| 162 | |||
| 163 | $result = $this->shareService->createBulkRequests($item_id, $item_guid, $vaults, $permissions, $credential_owner); |
||
| 164 | if ($credential) { |
||
| 165 | $processed_users = array(); |
||
| 166 | foreach ($result as $vault) { |
||
| 167 | if (!in_array($vault->getTargetUserId(), $processed_users)) { |
||
| 168 | $target_user = $vault->getTargetUserId(); |
||
| 169 | $notification = array( |
||
| 170 | 'from_user' => ucfirst($this->userId->getDisplayName()), |
||
| 171 | 'credential_label' => $credential->getLabel(), |
||
| 172 | 'credential_id' => $credential->getId(), |
||
| 173 | 'item_id' => $credential->getId(), |
||
| 174 | 'target_user' => $target_user, |
||
| 175 | 'req_id' => $vault->getId() |
||
| 176 | ); |
||
| 177 | $this->notificationService->credentialSharedNotification( |
||
| 178 | $notification |
||
| 179 | ); |
||
| 180 | array_push($processed_users, $target_user); |
||
| 181 | |||
| 182 | $this->activityService->add( |
||
| 183 | 'item_shared', [$credential->getLabel(), $target_user], |
||
| 184 | '', array(), |
||
| 185 | '', $this->userId->getUID(), Activity::TYPE_ITEM_SHARED); |
||
| 186 | |||
| 187 | |||
| 188 | $this->activityService->add( |
||
| 189 | 'item_share_received', [$credential->getLabel(), $this->userId->getUID()], |
||
| 190 | '', array(), |
||
| 191 | '', $target_user, Activity::TYPE_ITEM_SHARED); |
||
| 192 | } |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | |||
| 197 | return new JSONResponse($result); |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @NoAdminRequired |
||
| 202 | * @NoCSRFRequired |
||
| 203 | */ |
||
| 204 | public function searchUsers($search) { |
||
| 219 | |||
| 220 | |||
| 221 | /** |
||
| 222 | * @NoAdminRequired |
||
| 223 | * @NoCSRFRequired |
||
| 224 | */ |
||
| 225 | public function unshareCredential($item_guid) { |
||
| 245 | |||
| 246 | |||
| 247 | public function unshareCredentialFromUser($item_guid, $user_id) { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @NoAdminRequired |
||
| 278 | * @NoCSRFRequired |
||
| 279 | */ |
||
| 280 | public function search($search) { |
||
| 284 | |||
| 285 | |||
| 286 | /** |
||
| 287 | * @NoAdminRequired |
||
| 288 | * @NoCSRFRequired |
||
| 289 | */ |
||
| 290 | public function getVaultsByUser($user_id) { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @NoAdminRequired |
||
| 307 | * @NoCSRFRequired |
||
| 308 | */ |
||
| 309 | public function savePendingRequest($item_guid, $target_vault_guid, $final_shared_key) { |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @NoAdminRequired |
||
| 340 | * @NoCSRFRequired |
||
| 341 | */ |
||
| 342 | public function getPendingRequests() { |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @param $item_guid |
||
| 363 | * @return JSONResponse |
||
| 364 | * @NoAdminRequired |
||
| 365 | * @NoCSRFRequired |
||
| 366 | */ |
||
| 367 | public function getRevisions($item_guid) { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Obtains the list of credentials shared with this vault |
||
| 377 | * |
||
| 378 | * @NoAdminRequired |
||
| 379 | * @NoCSRFRequired |
||
| 380 | */ |
||
| 381 | public function getVaultItems($vault_guid) { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @param $share_request_id |
||
| 395 | * @return JSONResponse |
||
| 396 | * @NoAdminRequired |
||
| 397 | * @NoCSRFRequired |
||
| 398 | */ |
||
| 399 | public function deleteShareRequest($share_request_id) { |
||
| 427 | |||
| 428 | /** |
||
| 429 | * @param $credential_guid |
||
| 430 | * @return JSONResponse |
||
| 431 | * @NoAdminRequired |
||
| 432 | * @NoCSRFRequired |
||
| 433 | * @PublicPage |
||
| 434 | */ |
||
| 435 | public function getPublicCredentialData($credential_guid) { |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @param $item_guid |
||
| 466 | * @return JSONResponse |
||
| 467 | * @NoAdminRequired |
||
| 468 | * @NoCSRFRequired |
||
| 469 | */ |
||
| 470 | public function getItemAcl($item_guid) { |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @param $credential_guid |
||
| 491 | * @param $file_guid |
||
| 492 | * @NoAdminRequired |
||
| 493 | * @PublicPage |
||
| 494 | * @return JSONResponse |
||
| 495 | * @return NotFoundResponse |
||
| 496 | */ |
||
| 497 | public function getFile($item_guid, $file_guid) { |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @param $item_guid |
||
| 514 | * @param $user_id |
||
| 515 | * @param $permission |
||
| 516 | * @return JSONResponse |
||
| 517 | * @NoAdminRequired |
||
| 518 | * @NoCSRFRequired |
||
| 519 | */ |
||
| 520 | public function updateSharedCredentialACL($item_guid, $user_id, $permission) { |
||
| 542 | } |