| Total Complexity | 74 |
| Total Lines | 519 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Share 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.
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 Share, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class Share implements \OCP\Share\IShare { |
||
| 36 | |||
| 37 | /** @var string */ |
||
| 38 | private $id; |
||
| 39 | /** @var string */ |
||
| 40 | private $providerId; |
||
| 41 | /** @var Node */ |
||
| 42 | private $node; |
||
| 43 | /** @var int */ |
||
| 44 | private $fileId; |
||
| 45 | /** @var string */ |
||
| 46 | private $nodeType; |
||
| 47 | /** @var int */ |
||
| 48 | private $shareType; |
||
| 49 | /** @var string */ |
||
| 50 | private $sharedWith; |
||
| 51 | /** @var string */ |
||
| 52 | private $sharedWithDisplayName; |
||
| 53 | /** @var string */ |
||
| 54 | private $sharedWithAvatar; |
||
| 55 | /** @var string */ |
||
| 56 | private $sharedBy; |
||
| 57 | /** @var string */ |
||
| 58 | private $shareOwner; |
||
| 59 | /** @var int */ |
||
| 60 | private $permissions; |
||
| 61 | /** @var string */ |
||
| 62 | private $note = ''; |
||
| 63 | /** @var \DateTime */ |
||
| 64 | private $expireDate; |
||
| 65 | /** @var string */ |
||
| 66 | private $password; |
||
| 67 | /** @var bool */ |
||
| 68 | private $sendPasswordByTalk = false; |
||
| 69 | /** @var string */ |
||
| 70 | private $token; |
||
| 71 | /** @var int */ |
||
| 72 | private $parent; |
||
| 73 | /** @var string */ |
||
| 74 | private $target; |
||
| 75 | /** @var \DateTime */ |
||
| 76 | private $shareTime; |
||
| 77 | /** @var bool */ |
||
| 78 | private $mailSend; |
||
| 79 | /** @var string */ |
||
| 80 | private $label = ''; |
||
| 81 | |||
| 82 | /** @var IRootFolder */ |
||
| 83 | private $rootFolder; |
||
| 84 | |||
| 85 | /** @var IUserManager */ |
||
| 86 | private $userManager; |
||
| 87 | |||
| 88 | /** @var ICacheEntry|null */ |
||
| 89 | private $nodeCacheEntry; |
||
| 90 | |||
| 91 | /** @var bool */ |
||
| 92 | private $hideDownload = false; |
||
| 93 | |||
| 94 | public function __construct(IRootFolder $rootFolder, IUserManager $userManager) { |
||
| 95 | $this->rootFolder = $rootFolder; |
||
| 96 | $this->userManager = $userManager; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @inheritdoc |
||
| 101 | */ |
||
| 102 | public function setId($id) { |
||
| 103 | if (is_int($id)) { |
||
| 104 | $id = (string)$id; |
||
| 105 | } |
||
| 106 | |||
| 107 | if(!is_string($id)) { |
||
| 108 | throw new \InvalidArgumentException('String expected.'); |
||
| 109 | } |
||
| 110 | |||
| 111 | if ($this->id !== null) { |
||
| 112 | throw new IllegalIDChangeException('Not allowed to assign a new internal id to a share'); |
||
| 113 | } |
||
| 114 | |||
| 115 | $this->id = trim($id); |
||
| 116 | return $this; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @inheritdoc |
||
| 121 | */ |
||
| 122 | public function getId() { |
||
| 123 | return $this->id; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @inheritdoc |
||
| 128 | */ |
||
| 129 | public function getFullId() { |
||
| 130 | if ($this->providerId === null || $this->id === null) { |
||
| 131 | throw new \UnexpectedValueException; |
||
| 132 | } |
||
| 133 | return $this->providerId . ':' . $this->id; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @inheritdoc |
||
| 138 | */ |
||
| 139 | public function setProviderId($id) { |
||
| 140 | if(!is_string($id)) { |
||
| 141 | throw new \InvalidArgumentException('String expected.'); |
||
| 142 | } |
||
| 143 | |||
| 144 | if ($this->providerId !== null) { |
||
| 145 | throw new IllegalIDChangeException('Not allowed to assign a new provider id to a share'); |
||
| 146 | } |
||
| 147 | |||
| 148 | $this->providerId = trim($id); |
||
| 149 | return $this; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @inheritdoc |
||
| 154 | */ |
||
| 155 | public function setNode(Node $node) { |
||
| 156 | $this->fileId = null; |
||
| 157 | $this->nodeType = null; |
||
| 158 | $this->node = $node; |
||
| 159 | return $this; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @inheritdoc |
||
| 164 | */ |
||
| 165 | public function getNode() { |
||
| 166 | if ($this->node === null) { |
||
| 167 | |||
| 168 | if ($this->shareOwner === null || $this->fileId === null) { |
||
| 169 | throw new NotFoundException(); |
||
| 170 | } |
||
| 171 | |||
| 172 | // for federated shares the owner can be a remote user, in this |
||
| 173 | // case we use the initiator |
||
| 174 | if($this->userManager->userExists($this->shareOwner)) { |
||
| 175 | $userFolder = $this->rootFolder->getUserFolder($this->shareOwner); |
||
| 176 | } else { |
||
| 177 | $userFolder = $this->rootFolder->getUserFolder($this->sharedBy); |
||
| 178 | } |
||
| 179 | |||
| 180 | $nodes = $userFolder->getById($this->fileId); |
||
| 181 | if (empty($nodes)) { |
||
| 182 | throw new NotFoundException('Node for share not found, fileid: ' . $this->fileId); |
||
| 183 | } |
||
| 184 | |||
| 185 | $this->node = $nodes[0]; |
||
| 186 | } |
||
| 187 | |||
| 188 | return $this->node; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @inheritdoc |
||
| 193 | */ |
||
| 194 | public function setNodeId($fileId) { |
||
| 195 | $this->node = null; |
||
| 196 | $this->fileId = $fileId; |
||
| 197 | return $this; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @inheritdoc |
||
| 202 | */ |
||
| 203 | public function getNodeId() { |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @inheritdoc |
||
| 213 | */ |
||
| 214 | public function setNodeType($type) { |
||
| 215 | if ($type !== 'file' && $type !== 'folder') { |
||
| 216 | throw new \InvalidArgumentException(); |
||
| 217 | } |
||
| 218 | |||
| 219 | $this->nodeType = $type; |
||
| 220 | return $this; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * @inheritdoc |
||
| 225 | */ |
||
| 226 | public function getNodeType() { |
||
| 227 | if ($this->nodeType === null) { |
||
| 228 | $node = $this->getNode(); |
||
| 229 | $this->nodeType = $node instanceof File ? 'file' : 'folder'; |
||
| 230 | } |
||
| 231 | |||
| 232 | return $this->nodeType; |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @inheritdoc |
||
| 237 | */ |
||
| 238 | public function setShareType($shareType) { |
||
| 239 | $this->shareType = $shareType; |
||
| 240 | return $this; |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @inheritdoc |
||
| 245 | */ |
||
| 246 | public function getShareType() { |
||
| 247 | return $this->shareType; |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @inheritdoc |
||
| 252 | */ |
||
| 253 | public function setSharedWith($sharedWith) { |
||
| 254 | if (!is_string($sharedWith)) { |
||
| 255 | throw new \InvalidArgumentException(); |
||
| 256 | } |
||
| 257 | $this->sharedWith = $sharedWith; |
||
| 258 | return $this; |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @inheritdoc |
||
| 263 | */ |
||
| 264 | public function getSharedWith() { |
||
| 265 | return $this->sharedWith; |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @inheritdoc |
||
| 270 | */ |
||
| 271 | public function setSharedWithDisplayName($displayName) { |
||
| 272 | if (!is_string($displayName)) { |
||
| 273 | throw new \InvalidArgumentException(); |
||
| 274 | } |
||
| 275 | $this->sharedWithDisplayName = $displayName; |
||
| 276 | return $this; |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @inheritdoc |
||
| 281 | */ |
||
| 282 | public function getSharedWithDisplayName() { |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @inheritdoc |
||
| 288 | */ |
||
| 289 | public function setSharedWithAvatar($src) { |
||
| 290 | if (!is_string($src)) { |
||
| 291 | throw new \InvalidArgumentException(); |
||
| 292 | } |
||
| 293 | $this->sharedWithAvatar = $src; |
||
| 294 | return $this; |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @inheritdoc |
||
| 299 | */ |
||
| 300 | public function getSharedWithAvatar() { |
||
| 301 | return $this->sharedWithAvatar; |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @inheritdoc |
||
| 306 | */ |
||
| 307 | public function setPermissions($permissions) { |
||
| 308 | //TODO checkes |
||
| 309 | |||
| 310 | $this->permissions = $permissions; |
||
| 311 | return $this; |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @inheritdoc |
||
| 316 | */ |
||
| 317 | public function getPermissions() { |
||
| 318 | return $this->permissions; |
||
| 319 | } |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @inheritdoc |
||
| 323 | */ |
||
| 324 | public function setNote($note) { |
||
| 325 | $this->note = $note; |
||
| 326 | return $this; |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @inheritdoc |
||
| 331 | */ |
||
| 332 | public function getNote() { |
||
| 333 | if (is_string($this->note)) { |
||
|
|
|||
| 334 | return $this->note; |
||
| 335 | } |
||
| 336 | return ''; |
||
| 337 | } |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @inheritdoc |
||
| 341 | */ |
||
| 342 | public function setLabel($label) { |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @inheritdoc |
||
| 349 | */ |
||
| 350 | public function getLabel() { |
||
| 351 | return $this->label; |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @inheritdoc |
||
| 356 | */ |
||
| 357 | public function setExpirationDate($expireDate) { |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * @inheritdoc |
||
| 366 | */ |
||
| 367 | public function getExpirationDate() { |
||
| 368 | return $this->expireDate; |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @inheritdoc |
||
| 373 | */ |
||
| 374 | public function isExpired() { |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @inheritdoc |
||
| 381 | */ |
||
| 382 | public function setSharedBy($sharedBy) { |
||
| 383 | if (!is_string($sharedBy)) { |
||
| 384 | throw new \InvalidArgumentException(); |
||
| 385 | } |
||
| 386 | //TODO checks |
||
| 387 | $this->sharedBy = $sharedBy; |
||
| 388 | |||
| 389 | return $this; |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @inheritdoc |
||
| 394 | */ |
||
| 395 | public function getSharedBy() { |
||
| 396 | //TODO check if set |
||
| 397 | return $this->sharedBy; |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @inheritdoc |
||
| 402 | */ |
||
| 403 | public function setShareOwner($shareOwner) { |
||
| 404 | if (!is_string($shareOwner)) { |
||
| 405 | throw new \InvalidArgumentException(); |
||
| 406 | } |
||
| 407 | //TODO checks |
||
| 408 | |||
| 409 | $this->shareOwner = $shareOwner; |
||
| 410 | return $this; |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @inheritdoc |
||
| 415 | */ |
||
| 416 | public function getShareOwner() { |
||
| 417 | //TODO check if set |
||
| 418 | return $this->shareOwner; |
||
| 419 | } |
||
| 420 | |||
| 421 | /** |
||
| 422 | * @inheritdoc |
||
| 423 | */ |
||
| 424 | public function setPassword($password) { |
||
| 425 | $this->password = $password; |
||
| 426 | return $this; |
||
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * @inheritdoc |
||
| 431 | */ |
||
| 432 | public function getPassword() { |
||
| 433 | return $this->password; |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @inheritdoc |
||
| 438 | */ |
||
| 439 | public function setSendPasswordByTalk(bool $sendPasswordByTalk) { |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @inheritdoc |
||
| 446 | */ |
||
| 447 | public function getSendPasswordByTalk(): bool { |
||
| 448 | return $this->sendPasswordByTalk; |
||
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * @inheritdoc |
||
| 453 | */ |
||
| 454 | public function setToken($token) { |
||
| 455 | $this->token = $token; |
||
| 456 | return $this; |
||
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @inheritdoc |
||
| 461 | */ |
||
| 462 | public function getToken() { |
||
| 463 | return $this->token; |
||
| 464 | } |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Set the parent of this share |
||
| 468 | * |
||
| 469 | * @param int parent |
||
| 470 | * @return \OCP\Share\IShare |
||
| 471 | * @deprecated The new shares do not have parents. This is just here for legacy reasons. |
||
| 472 | */ |
||
| 473 | public function setParent($parent) { |
||
| 474 | $this->parent = $parent; |
||
| 475 | return $this; |
||
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Get the parent of this share. |
||
| 480 | * |
||
| 481 | * @return int |
||
| 482 | * @deprecated The new shares do not have parents. This is just here for legacy reasons. |
||
| 483 | */ |
||
| 484 | public function getParent() { |
||
| 485 | return $this->parent; |
||
| 486 | } |
||
| 487 | |||
| 488 | /** |
||
| 489 | * @inheritdoc |
||
| 490 | */ |
||
| 491 | public function setTarget($target) { |
||
| 492 | $this->target = $target; |
||
| 493 | return $this; |
||
| 494 | } |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @inheritdoc |
||
| 498 | */ |
||
| 499 | public function getTarget() { |
||
| 500 | return $this->target; |
||
| 501 | } |
||
| 502 | |||
| 503 | /** |
||
| 504 | * @inheritdoc |
||
| 505 | */ |
||
| 506 | public function setShareTime(\DateTime $shareTime) { |
||
| 507 | $this->shareTime = $shareTime; |
||
| 508 | return $this; |
||
| 509 | } |
||
| 510 | |||
| 511 | /** |
||
| 512 | * @inheritdoc |
||
| 513 | */ |
||
| 514 | public function getShareTime() { |
||
| 515 | return $this->shareTime; |
||
| 516 | } |
||
| 517 | |||
| 518 | /** |
||
| 519 | * @inheritdoc |
||
| 520 | */ |
||
| 521 | public function setMailSend($mailSend) { |
||
| 524 | } |
||
| 525 | |||
| 526 | /** |
||
| 527 | * @inheritdoc |
||
| 528 | */ |
||
| 529 | public function getMailSend() { |
||
| 530 | return $this->mailSend; |
||
| 531 | } |
||
| 532 | |||
| 533 | /** |
||
| 534 | * @inheritdoc |
||
| 535 | */ |
||
| 536 | public function setNodeCacheEntry(ICacheEntry $entry) { |
||
| 537 | $this->nodeCacheEntry = $entry; |
||
| 538 | } |
||
| 539 | |||
| 540 | /** |
||
| 541 | * @inheritdoc |
||
| 542 | */ |
||
| 543 | public function getNodeCacheEntry() { |
||
| 545 | } |
||
| 546 | |||
| 547 | public function setHideDownload(bool $hide): IShare { |
||
| 548 | $this->hideDownload = $hide; |
||
| 549 | return $this; |
||
| 550 | } |
||
| 551 | |||
| 552 | public function getHideDownload(): bool { |
||
| 553 | return $this->hideDownload; |
||
| 554 | } |
||
| 555 | } |
||
| 556 |