Share   F
last analyzed

Complexity

Total Complexity 83

Size/Duplication

Total Lines 579
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 164
dl 0
loc 579
rs 2
c 0
b 0
f 0
wmc 83

57 Methods

Rating   Name   Duplication   Size   Complexity  
A getNodeId() 0 6 2
A getId() 0 2 1
A getSharedWithDisplayName() 0 2 1
A setSharedWith() 0 6 2
A isExpired() 0 3 2
A setSendPasswordByTalk() 0 3 1
A setNodeType() 0 7 3
A setLabel() 0 3 1
A setMailSend() 0 3 1
A getNodeCacheEntry() 0 2 1
A getMailSend() 0 2 1
A getFullId() 0 5 3
A getStatus() 0 2 1
A getSharedWith() 0 2 1
A getNote() 0 5 2
A newAttributes() 0 2 1
A getNode() 0 23 6
A getPasswordExpirationTime() 0 2 1
A setStatus() 0 3 1
A getShareOwner() 0 3 1
A getSharedWithAvatar() 0 2 1
A getToken() 0 2 1
A setSharedWithDisplayName() 0 6 2
A setProviderId() 0 11 3
A setNode() 0 5 1
A setNote() 0 3 1
A setPasswordExpirationTime() 0 3 1
A setNodeId() 0 4 1
A getNodeType() 0 12 5
A setExpirationDate() 0 5 1
A getAttributes() 0 2 1
A getShareTime() 0 2 1
A setId() 0 15 4
A setNodeCacheEntry() 0 2 1
A getSharedBy() 0 3 1
A getPassword() 0 2 1
A setParent() 0 3 1
A getLabel() 0 2 1
A getPermissions() 0 2 1
A setShareType() 0 3 1
A __construct() 0 3 1
A getTarget() 0 2 1
A setTarget() 0 3 1
A getSendPasswordByTalk() 0 2 1
A setToken() 0 3 1
A setShareTime() 0 3 1
A setAttributes() 0 3 1
A getShareType() 0 2 1
A setShareOwner() 0 8 2
A getHideDownload() 0 2 1
A setSharedBy() 0 8 2
A setSharedWithAvatar() 0 6 2
A getParent() 0 2 1
A setPassword() 0 3 1
A setHideDownload() 0 3 1
A setPermissions() 0 5 1
A getExpirationDate() 0 2 1

How to fix   Complexity   

Complex Class

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
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Bjoern Schiessle <[email protected]>
6
 * @author Björn Schießle <[email protected]>
7
 * @author Christoph Wurst <[email protected]>
8
 * @author Daniel Calviño Sánchez <[email protected]>
9
 * @author Joas Schilling <[email protected]>
10
 * @author John Molakvoæ <[email protected]>
11
 * @author Maxence Lange <[email protected]>
12
 * @author Robin Appelman <[email protected]>
13
 * @author Roeland Jago Douma <[email protected]>
14
 *
15
 * @license AGPL-3.0
16
 *
17
 * This code is free software: you can redistribute it and/or modify
18
 * it under the terms of the GNU Affero General Public License, version 3,
19
 * as published by the Free Software Foundation.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License, version 3,
27
 * along with this program. If not, see <http://www.gnu.org/licenses/>
28
 *
29
 */
30
namespace OC\Share20;
31
32
use OCP\Files\File;
33
use OCP\Files\Cache\ICacheEntry;
34
use OCP\Files\FileInfo;
35
use OCP\Files\IRootFolder;
36
use OCP\Files\Node;
37
use OCP\Files\NotFoundException;
38
use OCP\IUserManager;
39
use OCP\Share\Exceptions\IllegalIDChangeException;
40
use OCP\Share\IAttributes;
41
use OCP\Share\IShare;
42
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() {
215
		if ($this->fileId === null) {
216
			$this->fileId = $this->getNode()->getId();
217
		}
218
219
		return $this->fileId;
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() {
299
		return $this->sharedWithDisplayName;
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)) {
0 ignored issues
show
introduced by
The condition is_string($this->note) is always true.
Loading history...
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() {
428
		return $this->getExpirationDate() !== null &&
429
			$this->getExpirationDate() <= new \DateTime();
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) {
508
		$this->sendPasswordByTalk = $sendPasswordByTalk;
509
		return $this;
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
0 ignored issues
show
Bug introduced by
The type OC\Share20\parent was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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) {
590
		$this->mailSend = $mailSend;
591
		return $this;
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() {
612
		return $this->nodeCacheEntry;
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