Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 37 | class ShareController extends ApiController { |
||
| 38 | private $userId; |
||
| 39 | private $activityService; |
||
| 40 | private $groupManager; |
||
| 41 | private $userManager; |
||
| 42 | private $vaultService; |
||
| 43 | private $shareService; |
||
| 44 | private $credentialService; |
||
| 45 | private $notificationService; |
||
| 46 | private $fileService; |
||
| 47 | private $config; |
||
| 48 | |||
| 49 | private $limit = 50; |
||
| 50 | private $offset = 0; |
||
| 51 | |||
| 52 | public function __construct($AppName, |
||
| 78 | |||
| 79 | private function isSharingEnabled() { |
||
| 80 | View Code Duplication | if ($this->config->getAppValue('passman', 'link_sharing_enabled', 1) === 0 || $this->config->getAppValue('passman', 'link_sharing_enabled', 1) === '0') { |
|
|
|
|||
| 81 | return new JSONResponse(array()); |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * @param $item_id |
||
| 87 | * @param $item_guid |
||
| 88 | * @param $permissions |
||
| 89 | * @param $expire_timestamp |
||
| 90 | * @NoAdminRequired |
||
| 91 | * @NoCSRFRequired |
||
| 92 | */ |
||
| 93 | public function createPublicShare($item_id, $item_guid, $permissions, $expire_timestamp, $expire_views) { |
||
| 94 | $this->isSharingEnabled(); |
||
| 95 | |||
| 96 | |||
| 97 | try { |
||
| 98 | $credential = $this->credentialService->getCredentialByGUID($item_guid); |
||
| 99 | } catch (DoesNotExistException $exception) { |
||
| 100 | return new NotFoundResponse(); |
||
| 101 | } |
||
| 102 | |||
| 103 | try { |
||
| 104 | $acl = $this->shareService->getACL(null, $item_guid); |
||
| 105 | } catch (DoesNotExistException $exception) { |
||
| 106 | $acl = new SharingACL(); |
||
| 107 | } |
||
| 108 | |||
| 109 | |||
| 110 | $acl->setItemId($item_id); |
||
| 111 | $acl->setItemGuid($item_guid); |
||
| 112 | $acl->setPermissions($permissions); |
||
| 113 | $acl->setExpire($expire_timestamp); |
||
| 114 | $acl->setExpireViews($expire_views); |
||
| 115 | if (!$acl->getId()) { |
||
| 116 | $this->shareService->createACLEntry($acl); |
||
| 117 | |||
| 118 | $this->activityService->add( |
||
| 119 | 'item_shared_publicly', [$credential->getLabel()], |
||
| 120 | '', array(), |
||
| 121 | '', $this->userId->getUID(), Activity::TYPE_ITEM_SHARED); |
||
| 122 | } else { |
||
| 123 | $this->shareService->updateCredentialACL($acl); |
||
| 124 | } |
||
| 125 | |||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @NoAdminRequired |
||
| 130 | * @NoCSRFRequired |
||
| 131 | */ |
||
| 132 | public function applyIntermediateShare($item_id, $item_guid, $vaults, $permissions) { |
||
| 133 | $this->isSharingEnabled(); |
||
| 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 (DoesNotExistException $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 (DoesNotExistException $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) { |
||
| 205 | $users = array(); |
||
| 206 | $usersTmp = $this->userManager->searchDisplayName($search, $this->limit, $this->offset); |
||
| 207 | |||
| 208 | foreach ($usersTmp as $user) { |
||
| 209 | if ($this->userId->getUID() !== $user->getUID() && count($this->vaultService->getByUser($user->getUID())) >= 1) { |
||
| 210 | $users[] = array( |
||
| 211 | 'text' => $user->getDisplayName(), |
||
| 212 | 'uid' => $user->getUID(), |
||
| 213 | 'type' => 'user' |
||
| 214 | ); |
||
| 215 | } |
||
| 216 | } |
||
| 217 | return $users; |
||
| 218 | } |
||
| 219 | |||
| 220 | |||
| 221 | /** |
||
| 222 | * @NoAdminRequired |
||
| 223 | * @NoCSRFRequired |
||
| 224 | */ |
||
| 225 | public function unshareCredential($item_guid) { |
||
| 226 | $this->isSharingEnabled(); |
||
| 227 | $acl_list = $this->shareService->getCredentialAclList($item_guid); |
||
| 228 | $request_list = $this->shareService->getShareRequestsByGuid($item_guid); |
||
| 229 | foreach ($acl_list as $ACL) { |
||
| 230 | $this->shareService->deleteShareACL($ACL); |
||
| 231 | } |
||
| 232 | foreach ($request_list as $request) { |
||
| 233 | $this->shareService->deleteShareRequest($request); |
||
| 234 | $manager = \OC::$server->getNotificationManager(); |
||
| 235 | $notification = $manager->createNotification(); |
||
| 236 | $notification->setApp('passman') |
||
| 237 | ->setObject('passman_share_request', $request->getId()) |
||
| 238 | ->setUser($request->getTargetUserId()); |
||
| 239 | $manager->markProcessed($notification); |
||
| 240 | } |
||
| 241 | return new JSONResponse(array('result' => true)); |
||
| 242 | } |
||
| 243 | |||
| 244 | |||
| 245 | public function unshareCredentialFromUser($item_guid, $user_id) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @NoAdminRequired |
||
| 276 | * @NoCSRFRequired |
||
| 277 | */ |
||
| 278 | public function search($search) { |
||
| 282 | |||
| 283 | |||
| 284 | /** |
||
| 285 | * @NoAdminRequired |
||
| 286 | * @NoCSRFRequired |
||
| 287 | */ |
||
| 288 | public function getVaultsByUser($user_id) { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * @NoAdminRequired |
||
| 305 | * @NoCSRFRequired |
||
| 306 | */ |
||
| 307 | public function savePendingRequest($item_guid, $target_vault_guid, $final_shared_key) { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @NoAdminRequired |
||
| 338 | * @NoCSRFRequired |
||
| 339 | */ |
||
| 340 | public function getPendingRequests() { |
||
| 355 | |||
| 356 | /** |
||
| 357 | * @param $item_guid |
||
| 358 | * @return JSONResponse |
||
| 359 | * @NoAdminRequired |
||
| 360 | * @NoCSRFRequired |
||
| 361 | */ |
||
| 362 | public function getRevisions($item_guid) { |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Obtains the list of credentials shared with this vault |
||
| 372 | * |
||
| 373 | * @NoAdminRequired |
||
| 374 | * @NoCSRFRequired |
||
| 375 | */ |
||
| 376 | public function getVaultItems($vault_guid) { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @param $share_request_id |
||
| 388 | * @return JSONResponse |
||
| 389 | * @NoAdminRequired |
||
| 390 | * @NoCSRFRequired |
||
| 391 | */ |
||
| 392 | public function deleteShareRequest($share_request_id) { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @param $credential_guid |
||
| 423 | * @return JSONResponse |
||
| 424 | * @NoAdminRequired |
||
| 425 | * @NoCSRFRequired |
||
| 426 | * @PublicPage |
||
| 427 | */ |
||
| 428 | public function getPublicCredentialData($credential_guid) { |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @param $item_guid |
||
| 457 | * @return JSONResponse |
||
| 458 | * @NoAdminRequired |
||
| 459 | * @NoCSRFRequired |
||
| 460 | */ |
||
| 461 | public function getItemAcl($item_guid) { |
||
| 479 | |||
| 480 | /** |
||
| 481 | * @param $credential_guid |
||
| 482 | * @param $file_guid |
||
| 483 | * @NoAdminRequired |
||
| 484 | * @PublicPage |
||
| 485 | * @return JSONResponse |
||
| 486 | * @return NotFoundResponse |
||
| 487 | */ |
||
| 488 | public function getFile($item_guid, $file_guid) { |
||
| 502 | |||
| 503 | /** |
||
| 504 | * @param $item_guid |
||
| 505 | * @param $user_id |
||
| 506 | * @param $permission |
||
| 507 | * @return JSONResponse |
||
| 508 | * @NoAdminRequired |
||
| 509 | * @NoCSRFRequired |
||
| 510 | */ |
||
| 511 | public function updateSharedCredentialACL($item_guid, $user_id, $permission) { |
||
| 533 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.