Complex classes like Share20OCS 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 Share20OCS, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 52 | class Share20OCS extends OCSController { |
||
| 53 | |||
| 54 | /** @var IManager */ |
||
| 55 | private $shareManager; |
||
| 56 | /** @var IGroupManager */ |
||
| 57 | private $groupManager; |
||
| 58 | /** @var IUserManager */ |
||
| 59 | private $userManager; |
||
| 60 | /** @var IRequest */ |
||
| 61 | protected $request; |
||
| 62 | /** @var IRootFolder */ |
||
| 63 | private $rootFolder; |
||
| 64 | /** @var IURLGenerator */ |
||
| 65 | private $urlGenerator; |
||
| 66 | /** @var IUser */ |
||
| 67 | private $currentUser; |
||
| 68 | /** @var IL10N */ |
||
| 69 | private $l; |
||
| 70 | /** @var \OCP\Files\Node */ |
||
| 71 | private $lockedNode; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Share20OCS constructor. |
||
| 75 | * |
||
| 76 | * @param string $appName |
||
| 77 | * @param IRequest $request |
||
| 78 | * @param IManager $shareManager |
||
| 79 | * @param IGroupManager $groupManager |
||
| 80 | * @param IUserManager $userManager |
||
| 81 | * @param IRootFolder $rootFolder |
||
| 82 | * @param IURLGenerator $urlGenerator |
||
| 83 | * @param IUser $currentUser |
||
| 84 | * @param IL10N $l10n |
||
| 85 | */ |
||
| 86 | public function __construct( |
||
| 87 | $appName, |
||
| 88 | IRequest $request, |
||
| 89 | IManager $shareManager, |
||
| 90 | IGroupManager $groupManager, |
||
| 91 | IUserManager $userManager, |
||
| 92 | IRootFolder $rootFolder, |
||
| 93 | IURLGenerator $urlGenerator, |
||
| 94 | IUser $currentUser, |
||
| 95 | IL10N $l10n |
||
| 96 | ) { |
||
| 97 | parent::__construct($appName, $request); |
||
| 98 | |||
| 99 | $this->shareManager = $shareManager; |
||
| 100 | $this->userManager = $userManager; |
||
| 101 | $this->groupManager = $groupManager; |
||
| 102 | $this->request = $request; |
||
| 103 | $this->rootFolder = $rootFolder; |
||
| 104 | $this->urlGenerator = $urlGenerator; |
||
| 105 | $this->currentUser = $currentUser; |
||
| 106 | $this->l = $l10n; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Convert an IShare to an array for OCS output |
||
| 111 | * |
||
| 112 | * @param \OCP\Share\IShare $share |
||
| 113 | * @return array |
||
| 114 | * @throws NotFoundException In case the node can't be resolved. |
||
| 115 | */ |
||
| 116 | protected function formatShare(\OCP\Share\IShare $share) { |
||
| 117 | $sharedBy = $this->userManager->get($share->getSharedBy()); |
||
| 118 | $shareOwner = $this->userManager->get($share->getShareOwner()); |
||
| 119 | |||
| 120 | $result = [ |
||
| 121 | 'id' => $share->getId(), |
||
| 122 | 'share_type' => $share->getShareType(), |
||
| 123 | 'uid_owner' => $share->getSharedBy(), |
||
| 124 | 'displayname_owner' => $sharedBy !== null ? $sharedBy->getDisplayName() : $share->getSharedBy(), |
||
| 125 | 'permissions' => $share->getPermissions(), |
||
| 126 | 'stime' => $share->getShareTime()->getTimestamp(), |
||
| 127 | 'parent' => null, |
||
| 128 | 'expiration' => null, |
||
| 129 | 'token' => null, |
||
| 130 | 'uid_file_owner' => $share->getShareOwner(), |
||
| 131 | 'displayname_file_owner' => $shareOwner !== null ? $shareOwner->getDisplayName() : $share->getShareOwner(), |
||
| 132 | ]; |
||
| 133 | |||
| 134 | $userFolder = $this->rootFolder->getUserFolder($this->currentUser->getUID()); |
||
| 135 | $nodes = $userFolder->getById($share->getNodeId()); |
||
| 136 | |||
| 137 | if (empty($nodes)) { |
||
| 138 | throw new NotFoundException(); |
||
| 139 | } |
||
| 140 | |||
| 141 | $node = $nodes[0]; |
||
| 142 | |||
| 143 | $result['path'] = $userFolder->getRelativePath($node->getPath()); |
||
| 144 | if ($node instanceOf \OCP\Files\Folder) { |
||
| 145 | $result['item_type'] = 'folder'; |
||
| 146 | } else { |
||
| 147 | $result['item_type'] = 'file'; |
||
| 148 | } |
||
| 149 | $result['mimetype'] = $node->getMimetype(); |
||
| 150 | $result['storage_id'] = $node->getStorage()->getId(); |
||
| 151 | $result['storage'] = $node->getStorage()->getCache()->getNumericStorageId(); |
||
| 152 | $result['item_source'] = $node->getId(); |
||
| 153 | $result['file_source'] = $node->getId(); |
||
| 154 | $result['file_parent'] = $node->getParent()->getId(); |
||
| 155 | $result['file_target'] = $share->getTarget(); |
||
| 156 | |||
| 157 | if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { |
||
| 158 | $sharedWith = $this->userManager->get($share->getSharedWith()); |
||
| 159 | $result['share_with'] = $share->getSharedWith(); |
||
| 160 | $result['share_with_displayname'] = $sharedWith !== null ? $sharedWith->getDisplayName() : $share->getSharedWith(); |
||
| 161 | } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { |
||
| 162 | $result['share_with'] = $share->getSharedWith(); |
||
| 163 | $result['share_with_displayname'] = $share->getSharedWith(); |
||
| 164 | } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { |
||
| 165 | |||
| 166 | $result['share_with'] = $share->getPassword(); |
||
| 167 | $result['share_with_displayname'] = $share->getPassword(); |
||
| 168 | |||
| 169 | $result['token'] = $share->getToken(); |
||
| 170 | $result['url'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $share->getToken()]); |
||
| 171 | |||
| 172 | $expiration = $share->getExpirationDate(); |
||
| 173 | if ($expiration !== null) { |
||
| 174 | $result['expiration'] = $expiration->format('Y-m-d 00:00:00'); |
||
| 175 | } |
||
| 176 | |||
| 177 | } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) { |
||
| 178 | $result['share_with'] = $share->getSharedWith(); |
||
| 179 | $result['share_with_displayname'] = $share->getSharedWith(); |
||
| 180 | $result['token'] = $share->getToken(); |
||
| 181 | } |
||
| 182 | |||
| 183 | $result['mail_send'] = $share->getMailSend() ? 1 : 0; |
||
| 184 | |||
| 185 | return $result; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Get a specific share by id |
||
| 190 | * |
||
| 191 | * @NoAdminRequired |
||
| 192 | * |
||
| 193 | * @param string $id |
||
| 194 | * @return DataResponse |
||
| 195 | * @throws OCSNotFoundException |
||
| 196 | */ |
||
| 197 | public function getShare($id) { |
||
| 198 | try { |
||
| 199 | $share = $this->getShareById($id); |
||
| 200 | } catch (ShareNotFound $e) { |
||
| 201 | throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist')); |
||
| 202 | } |
||
| 203 | |||
| 204 | if ($this->canAccessShare($share)) { |
||
| 205 | try { |
||
| 206 | $share = $this->formatShare($share); |
||
| 207 | return new DataResponse([$share]); |
||
| 208 | } catch (NotFoundException $e) { |
||
| 209 | //Fall trough |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist')); |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Delete a share |
||
| 218 | * |
||
| 219 | * @NoAdminRequired |
||
| 220 | * |
||
| 221 | * @param string $id |
||
| 222 | * @return DataResponse |
||
| 223 | * @throws OCSNotFoundException |
||
| 224 | */ |
||
| 225 | public function deleteShare($id) { |
||
| 226 | try { |
||
| 227 | $share = $this->getShareById($id); |
||
| 228 | } catch (ShareNotFound $e) { |
||
| 229 | throw new OCSNotFoundException($this->l->t('Wrong share ID, share doesn\'t exist')); |
||
| 230 | } |
||
| 231 | |||
| 232 | try { |
||
| 233 | $this->lock($share->getNode()); |
||
| 234 | } catch (LockedException $e) { |
||
| 235 | throw new OCSNotFoundException($this->l->t('could not delete share')); |
||
| 236 | } |
||
| 237 | |||
| 238 | if (!$this->canAccessShare($share, false)) { |
||
| 239 | throw new OCSNotFoundException($this->l->t('Could not delete share')); |
||
| 240 | } |
||
| 241 | |||
| 242 | $this->shareManager->deleteShare($share); |
||
| 243 | |||
| 244 | return new DataResponse(); |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @NoAdminRequired |
||
| 249 | * |
||
| 250 | * @param string $path |
||
| 251 | * @param int $permissions |
||
| 252 | * @param int $shareType |
||
| 253 | * @param string $shareWith |
||
| 254 | * @param string $publicUpload |
||
| 255 | * @param string $password |
||
| 256 | * @param string $expireDate |
||
| 257 | * |
||
| 258 | * @return DataResponse |
||
| 259 | * @throws OCSNotFoundException |
||
| 260 | * @throws OCSForbiddenException |
||
| 261 | * @throws OCSBadRequestException |
||
| 262 | * @throws OCSException |
||
| 263 | */ |
||
| 264 | public function createShare( |
||
| 265 | $path = null, |
||
| 266 | $permissions = \OCP\Constants::PERMISSION_ALL, |
||
| 267 | $shareType = -1, |
||
| 268 | $shareWith = null, |
||
| 269 | $publicUpload = 'false', |
||
| 270 | $password = '', |
||
| 271 | $expireDate = '' |
||
| 272 | ) { |
||
| 273 | $share = $this->shareManager->newShare(); |
||
| 274 | |||
| 275 | // Verify path |
||
| 276 | if ($path === null) { |
||
| 277 | throw new OCSNotFoundException($this->l->t('Please specify a file or folder path')); |
||
| 278 | } |
||
| 279 | |||
| 280 | $userFolder = $this->rootFolder->getUserFolder($this->currentUser->getUID()); |
||
| 281 | try { |
||
| 282 | $path = $userFolder->get($path); |
||
| 283 | } catch (NotFoundException $e) { |
||
| 284 | throw new OCSNotFoundException($this->l->t('Wrong path, file/folder doesn\'t exist')); |
||
| 285 | } |
||
| 286 | |||
| 287 | $share->setNode($path); |
||
| 288 | |||
| 289 | try { |
||
| 290 | $this->lock($share->getNode()); |
||
| 291 | } catch (LockedException $e) { |
||
| 292 | throw new OCSNotFoundException($this->l->t('Could not create share')); |
||
| 293 | } |
||
| 294 | |||
| 295 | if ($permissions < 0 || $permissions > \OCP\Constants::PERMISSION_ALL) { |
||
| 296 | throw new OCSNotFoundException($this->l->t('invalid permissions')); |
||
| 297 | } |
||
| 298 | |||
| 299 | // Shares always require read permissions |
||
| 300 | $permissions |= \OCP\Constants::PERMISSION_READ; |
||
| 301 | |||
| 302 | if ($path instanceof \OCP\Files\File) { |
||
| 303 | // Single file shares should never have delete or create permissions |
||
| 304 | $permissions &= ~\OCP\Constants::PERMISSION_DELETE; |
||
| 305 | $permissions &= ~\OCP\Constants::PERMISSION_CREATE; |
||
| 306 | } |
||
| 307 | |||
| 308 | /* |
||
| 309 | * Hack for https://github.com/owncloud/core/issues/22587 |
||
| 310 | * We check the permissions via webdav. But the permissions of the mount point |
||
| 311 | * do not equal the share permissions. Here we fix that for federated mounts. |
||
| 312 | */ |
||
| 313 | if ($path->getStorage()->instanceOfStorage('OCA\Files_Sharing\External\Storage')) { |
||
| 314 | $permissions &= ~($permissions & ~$path->getPermissions()); |
||
| 315 | } |
||
| 316 | |||
| 317 | if ($shareType === \OCP\Share::SHARE_TYPE_USER) { |
||
| 318 | // Valid user is required to share |
||
| 319 | if ($shareWith === null || !$this->userManager->userExists($shareWith)) { |
||
| 320 | throw new OCSNotFoundException($this->l->t('Please specify a valid user')); |
||
| 321 | } |
||
| 322 | $share->setSharedWith($shareWith); |
||
| 323 | $share->setPermissions($permissions); |
||
| 324 | } else if ($shareType === \OCP\Share::SHARE_TYPE_GROUP) { |
||
| 325 | if (!$this->shareManager->allowGroupSharing()) { |
||
| 326 | throw new OCSNotFoundException($this->l->t('Group sharing is disabled by the administrator')); |
||
| 327 | } |
||
| 328 | |||
| 329 | // Valid group is required to share |
||
| 330 | if ($shareWith === null || !$this->groupManager->groupExists($shareWith)) { |
||
| 331 | throw new OCSNotFoundException($this->l->t('Please specify a valid group')); |
||
| 332 | } |
||
| 333 | $share->setSharedWith($shareWith); |
||
| 334 | $share->setPermissions($permissions); |
||
| 335 | } else if ($shareType === \OCP\Share::SHARE_TYPE_LINK) { |
||
| 336 | //Can we even share links? |
||
| 337 | if (!$this->shareManager->shareApiAllowLinks()) { |
||
| 338 | throw new OCSNotFoundException($this->l->t('Public link sharing is disabled by the administrator')); |
||
| 339 | } |
||
| 340 | |||
| 341 | /* |
||
| 342 | * For now we only allow 1 link share. |
||
| 343 | * Return the existing link share if this is a duplicate |
||
| 344 | */ |
||
| 345 | $existingShares = $this->shareManager->getSharesBy($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_LINK, $path, false, 1, 0); |
||
| 346 | if (!empty($existingShares)) { |
||
| 347 | return new DataResponse($this->formatShare($existingShares[0])); |
||
| 348 | } |
||
| 349 | |||
| 350 | if ($publicUpload === 'true') { |
||
| 351 | // Check if public upload is allowed |
||
| 352 | if (!$this->shareManager->shareApiLinkAllowPublicUpload()) { |
||
| 353 | throw new OCSForbiddenException($this->l->t('Public upload disabled by the administrator')); |
||
| 354 | } |
||
| 355 | |||
| 356 | // Public upload can only be set for folders |
||
| 357 | if ($path instanceof \OCP\Files\File) { |
||
| 358 | throw new OCSNotFoundException($this->l->t('Public upload is only possible for publicly shared folders')); |
||
| 359 | } |
||
| 360 | |||
| 361 | $share->setPermissions( |
||
| 362 | \OCP\Constants::PERMISSION_READ | |
||
| 363 | \OCP\Constants::PERMISSION_CREATE | |
||
| 364 | \OCP\Constants::PERMISSION_UPDATE | |
||
| 365 | \OCP\Constants::PERMISSION_DELETE |
||
| 366 | ); |
||
| 367 | } else { |
||
| 368 | $share->setPermissions(\OCP\Constants::PERMISSION_READ); |
||
| 369 | } |
||
| 370 | |||
| 371 | // Set password |
||
| 372 | if ($password !== '') { |
||
| 373 | $share->setPassword($password); |
||
| 374 | } |
||
| 375 | |||
| 376 | //Expire date |
||
| 377 | if ($expireDate !== '') { |
||
| 378 | try { |
||
| 379 | $expireDate = $this->parseDate($expireDate); |
||
| 380 | $share->setExpirationDate($expireDate); |
||
| 381 | } catch (\Exception $e) { |
||
| 382 | throw new OCSNotFoundException($this->l->t('Invalid date, date format must be YYYY-MM-DD')); |
||
| 383 | } |
||
| 384 | } |
||
| 385 | |||
| 386 | } else if ($shareType === \OCP\Share::SHARE_TYPE_REMOTE) { |
||
| 387 | if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) { |
||
| 388 | throw new OCSForbiddenException($this->l->t('Sharing %s failed because the back end does not allow shares from type %s', [$path->getPath(), $shareType])); |
||
| 389 | } |
||
| 390 | |||
| 391 | $share->setSharedWith($shareWith); |
||
| 392 | $share->setPermissions($permissions); |
||
| 393 | } else { |
||
| 394 | throw new OCSBadRequestException($this->l->t('Unknown share type')); |
||
| 395 | } |
||
| 396 | |||
| 397 | $share->setShareType($shareType); |
||
| 398 | $share->setSharedBy($this->currentUser->getUID()); |
||
| 399 | |||
| 400 | try { |
||
| 401 | $share = $this->shareManager->createShare($share); |
||
| 402 | } catch (GenericShareException $e) { |
||
| 403 | $code = $e->getCode() === 0 ? 403 : $e->getCode(); |
||
| 404 | throw new OCSException($e->getHint(), $code); |
||
| 405 | }catch (\Exception $e) { |
||
| 406 | throw new OCSForbiddenException($e->getMessage()); |
||
| 407 | } |
||
| 408 | |||
| 409 | $output = $this->formatShare($share); |
||
| 410 | |||
| 411 | return new DataResponse($output); |
||
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * @param \OCP\Files\File|\OCP\Files\Folder $node |
||
| 416 | * @return DataResponse |
||
| 417 | */ |
||
| 418 | private function getSharedWithMe($node = null) { |
||
| 419 | $userShares = $this->shareManager->getSharedWith($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_USER, $node, -1, 0); |
||
| 420 | $groupShares = $this->shareManager->getSharedWith($this->currentUser->getUID(), \OCP\Share::SHARE_TYPE_GROUP, $node, -1, 0); |
||
| 421 | |||
| 422 | $shares = array_merge($userShares, $groupShares); |
||
| 423 | |||
| 424 | $shares = array_filter($shares, function(IShare $share) { |
||
| 425 | return $share->getShareOwner() !== $this->currentUser->getUID(); |
||
| 426 | }); |
||
| 427 | |||
| 428 | $formatted = []; |
||
| 429 | foreach ($shares as $share) { |
||
| 430 | if ($this->canAccessShare($share)) { |
||
| 431 | try { |
||
| 432 | $formatted[] = $this->formatShare($share); |
||
| 433 | } catch (NotFoundException $e) { |
||
| 434 | // Ignore this share |
||
| 435 | } |
||
| 436 | } |
||
| 437 | } |
||
| 438 | |||
| 439 | return new DataResponse($formatted); |
||
| 440 | } |
||
| 441 | |||
| 442 | /** |
||
| 443 | * @param \OCP\Files\Folder $folder |
||
| 444 | * @return DataResponse |
||
| 445 | * @throws OCSBadRequestException |
||
| 446 | */ |
||
| 447 | private function getSharesInDir($folder) { |
||
| 475 | |||
| 476 | /** |
||
| 477 | * The getShares function. |
||
| 478 | * |
||
| 479 | * @NoAdminRequired |
||
| 480 | * |
||
| 481 | * @param string $shared_with_me |
||
| 482 | * @param string $reshares |
||
| 483 | * @param string $subfiles |
||
| 484 | * @param string $path |
||
| 485 | * |
||
| 486 | * - Get shares by the current user |
||
| 487 | * - Get shares by the current user and reshares (?reshares=true) |
||
| 488 | * - Get shares with the current user (?shared_with_me=true) |
||
| 489 | * - Get shares for a specific path (?path=...) |
||
| 490 | * - Get all shares in a folder (?subfiles=true&path=..) |
||
| 491 | * |
||
| 492 | * @return DataResponse |
||
| 493 | * @throws OCSNotFoundException |
||
| 494 | */ |
||
| 495 | public function getShares( |
||
| 552 | |||
| 553 | /** |
||
| 554 | * @NoAdminRequired |
||
| 555 | * |
||
| 556 | * @param int $id |
||
| 557 | * @param int $permissions |
||
| 558 | * @param string $password |
||
| 559 | * @param string $publicUpload |
||
| 560 | * @param string $expireDate |
||
| 561 | * @return DataResponse |
||
| 562 | * @throws OCSNotFoundException |
||
| 563 | * @throws OCSBadRequestException |
||
| 564 | * @throws OCSForbiddenException |
||
| 565 | */ |
||
| 566 | public function updateShare( |
||
| 691 | |||
| 692 | /** |
||
| 693 | * @param \OCP\Share\IShare $share |
||
| 694 | * @return bool |
||
| 695 | */ |
||
| 696 | protected function canAccessShare(\OCP\Share\IShare $share, $checkGroups = true) { |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Make sure that the passed date is valid ISO 8601 |
||
| 727 | * So YYYY-MM-DD |
||
| 728 | * If not throw an exception |
||
| 729 | * |
||
| 730 | * @param string $expireDate |
||
| 731 | * |
||
| 732 | * @throws \Exception |
||
| 733 | * @return \DateTime |
||
| 734 | */ |
||
| 735 | private function parseDate($expireDate) { |
||
| 750 | |||
| 751 | /** |
||
| 752 | * Since we have multiple providers but the OCS Share API v1 does |
||
| 753 | * not support this we need to check all backends. |
||
| 754 | * |
||
| 755 | * @param string $id |
||
| 756 | * @return \OCP\Share\IShare |
||
| 757 | * @throws ShareNotFound |
||
| 758 | */ |
||
| 759 | private function getShareById($id) { |
||
| 775 | |||
| 776 | /** |
||
| 777 | * Lock a Node |
||
| 778 | * @param \OCP\Files\Node $node |
||
| 779 | */ |
||
| 780 | private function lock(\OCP\Files\Node $node) { |
||
| 784 | |||
| 785 | /** |
||
| 786 | * Cleanup the remaining locks |
||
| 787 | */ |
||
| 788 | public function cleanup() { |
||
| 793 | } |
||
| 794 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.