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