Completed
Push — master ( 143145...82021b )
by Morris
38:23 queued 15:00
created

Share   F

Complexity

Total Complexity 63

Size/Duplication

Total Lines 447
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 447
rs 3.36
c 0
b 0
f 0
wmc 63
lcom 2
cbo 5

41 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setId() 0 16 4
A getId() 0 3 1
A getFullId() 0 6 3
A setProviderId() 0 12 3
A setNode() 0 6 1
B getNode() 0 25 6
A setNodeId() 0 5 1
A getNodeId() 0 7 2
A setNodeType() 0 8 3
A getNodeType() 0 8 3
A setShareType() 0 4 1
A getShareType() 0 3 1
A setSharedWith() 0 7 2
A getSharedWith() 0 3 1
A setSharedWithDisplayName() 0 7 2
A getSharedWithDisplayName() 0 3 1
A setSharedWithAvatar() 0 7 2
A getSharedWithAvatar() 0 3 1
A setPermissions() 0 6 1
A getPermissions() 0 3 1
A setExpirationDate() 0 6 1
A getExpirationDate() 0 3 1
A setSharedBy() 0 9 2
A getSharedBy() 0 4 1
A setShareOwner() 0 9 2
A getShareOwner() 0 4 1
A setPassword() 0 4 1
A getPassword() 0 3 1
A setToken() 0 4 1
A getToken() 0 3 1
A setParent() 0 4 1
A getParent() 0 3 1
A setTarget() 0 4 1
A getTarget() 0 3 1
A setShareTime() 0 4 1
A getShareTime() 0 3 1
A setMailSend() 0 4 1
A getMailSend() 0 3 1
A setNodeCacheEntry() 0 3 1
A getNodeCacheEntry() 0 3 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. 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 Share, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Björn Schießle <[email protected]>
6
 * @author Robin Appelman <[email protected]>
7
 * @author Roeland Jago Douma <[email protected]>
8
 *
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
namespace OC\Share20;
25
26
use OCP\Files\Cache\ICacheEntry;
27
use OCP\Files\File;
28
use OCP\Files\IRootFolder;
29
use OCP\Files\Node;
30
use OCP\Files\NotFoundException;
31
use OCP\IUserManager;
32
use OCP\Share\Exceptions\IllegalIDChangeException;
33
34
class Share implements \OCP\Share\IShare {
35
36
	/** @var string */
37
	private $id;
38
	/** @var string */
39
	private $providerId;
40
	/** @var Node */
41
	private $node;
42
	/** @var int */
43
	private $fileId;
44
	/** @var string */
45
	private $nodeType;
46
	/** @var int */
47
	private $shareType;
48
	/** @var string */
49
	private $sharedWith;
50
	/** @var string */
51
	private $sharedWithDisplayName;
52
	/** @var string */
53
	private $sharedWithAvatar;
54
	/** @var string */
55
	private $sharedBy;
56
	/** @var string */
57
	private $shareOwner;
58
	/** @var int */
59
	private $permissions;
60
	/** @var \DateTime */
61
	private $expireDate;
62
	/** @var string */
63
	private $password;
64
	/** @var string */
65
	private $token;
66
	/** @var int */
67
	private $parent;
68
	/** @var string */
69
	private $target;
70
	/** @var \DateTime */
71
	private $shareTime;
72
	/** @var bool */
73
	private $mailSend;
74
75
	/** @var IRootFolder */
76
	private $rootFolder;
77
78
	/** @var IUserManager */
79
	private $userManager;
80
81
	/** @var ICacheEntry|null */
82
	private $nodeCacheEntry;
83
84
	public function __construct(IRootFolder $rootFolder, IUserManager $userManager) {
85
		$this->rootFolder = $rootFolder;
86
		$this->userManager = $userManager;
87
	}
88
89
	/**
90
	 * @inheritdoc
91
	 */
92
	public function setId($id) {
93
		if (is_int($id)) {
94
			$id = (string)$id;
95
		}
96
97
		if(!is_string($id)) {
98
			throw new \InvalidArgumentException('String expected.');
99
		}
100
101
		if ($this->id !== null) {
102
			throw new IllegalIDChangeException('Not allowed to assign a new internal id to a share');
103
		}
104
105
		$this->id = trim($id);
106
		return $this;
107
	}
108
109
	/**
110
	 * @inheritdoc
111
	 */
112
	public function getId() {
113
		return $this->id;
114
	}
115
116
	/**
117
	 * @inheritdoc
118
	 */
119
	public function getFullId() {
120
		if ($this->providerId === null || $this->id === null) {
121
			throw new \UnexpectedValueException;
122
		}
123
		return $this->providerId . ':' . $this->id;
124
	}
125
126
	/**
127
	 * @inheritdoc
128
	 */
129
	public function setProviderId($id) {
130
		if(!is_string($id)) {
131
			throw new \InvalidArgumentException('String expected.');
132
		}
133
134
		if ($this->providerId !== null) {
135
			throw new IllegalIDChangeException('Not allowed to assign a new provider id to a share');
136
		}
137
138
		$this->providerId = trim($id);
139
		return $this;
140
	}
141
142
	/**
143
	 * @inheritdoc
144
	 */
145
	public function setNode(Node $node) {
146
		$this->fileId = null;
147
		$this->nodeType = null;
148
		$this->node = $node;
149
		return $this;
150
	}
151
152
	/**
153
	 * @inheritdoc
154
	 */
155
	public function getNode() {
156
		if ($this->node === null) {
157
158
			if ($this->shareOwner === null || $this->fileId === null) {
159
				throw new NotFoundException();
160
			}
161
162
			// for federated shares the owner can be a remote user, in this
163
			// case we use the initiator
164
			if($this->userManager->userExists($this->shareOwner)) {
165
				$userFolder = $this->rootFolder->getUserFolder($this->shareOwner);
166
			} else {
167
				$userFolder = $this->rootFolder->getUserFolder($this->sharedBy);
168
			}
169
170
			$nodes = $userFolder->getById($this->fileId);
171
			if (empty($nodes)) {
172
				throw new NotFoundException('Node for share not found, fileid: ' . $this->fileId);
173
			}
174
175
			$this->node = $nodes[0];
176
		}
177
178
		return $this->node;
179
	}
180
181
	/**
182
	 * @inheritdoc
183
	 */
184
	public function setNodeId($fileId) {
185
		$this->node = null;
186
		$this->fileId = $fileId;
187
		return $this;
188
	}
189
190
	/**
191
	 * @inheritdoc
192
	 */
193
	public function getNodeId() {
194
		if ($this->fileId === null) {
195
			$this->fileId = $this->getNode()->getId();
196
		}
197
198
		return $this->fileId;
199
	}
200
201
	/**
202
	 * @inheritdoc
203
	 */
204
	public function setNodeType($type) {
205
		if ($type !== 'file' && $type !== 'folder') {
206
			throw new \InvalidArgumentException();
207
		}
208
209
		$this->nodeType = $type;
210
		return $this;
211
	}
212
213
	/**
214
	 * @inheritdoc
215
	 */
216
	public function getNodeType() {
217
		if ($this->nodeType === null) {
218
			$node = $this->getNode();
219
			$this->nodeType = $node instanceof File ? 'file' : 'folder';
220
		}
221
222
		return $this->nodeType;
223
	}
224
225
	/**
226
	 * @inheritdoc
227
	 */
228
	public function setShareType($shareType) {
229
		$this->shareType = $shareType;
230
		return $this;
231
	}
232
233
	/**
234
	 * @inheritdoc
235
	 */
236
	public function getShareType() {
237
		return $this->shareType;
238
	}
239
240
	/**
241
	 * @inheritdoc
242
	 */
243
	public function setSharedWith($sharedWith) {
244
		if (!is_string($sharedWith)) {
245
			throw new \InvalidArgumentException();
246
		}
247
		$this->sharedWith = $sharedWith;
248
		return $this;
249
	}
250
251
	/**
252
	 * @inheritdoc
253
	 */
254
	public function getSharedWith() {
255
		return $this->sharedWith;
256
	}
257
258
	/**
259
	 * @inheritdoc
260
	 */
261
	public function setSharedWithDisplayName($displayName) {
262
		if (!is_string($displayName)) {
263
			throw new \InvalidArgumentException();
264
		}
265
		$this->sharedWithDisplayName = $displayName;
266
		return $this;
267
	}
268
269
	/**
270
	 * @inheritdoc
271
	 */
272
	public function getSharedWithDisplayName() {
273
		return $this->sharedWithDisplayName;
274
	}
275
276
	/**
277
	 * @inheritdoc
278
	 */
279
	public function setSharedWithAvatar($src) {
280
		if (!is_string($src)) {
281
			throw new \InvalidArgumentException();
282
		}
283
		$this->sharedWithAvatar = $src;
284
		return $this;
285
	}
286
287
	/**
288
	 * @inheritdoc
289
	 */
290
	public function getSharedWithAvatar() {
291
		return $this->sharedWithAvatar;
292
	}
293
294
	/**
295
	 * @inheritdoc
296
	 */
297
	public function setPermissions($permissions) {
298
		//TODO checkes
299
300
		$this->permissions = $permissions;
301
		return $this;
302
	}
303
304
	/**
305
	 * @inheritdoc
306
	 */
307
	public function getPermissions() {
308
		return $this->permissions;
309
	}
310
311
	/**
312
	 * @inheritdoc
313
	 */
314
	public function setExpirationDate($expireDate) {
315
		//TODO checks
316
317
		$this->expireDate = $expireDate;
318
		return $this;
319
	}
320
321
	/**
322
	 * @inheritdoc
323
	 */
324
	public function getExpirationDate() {
325
		return $this->expireDate;
326
	}
327
328
	/**
329
	 * @inheritdoc
330
	 */
331
	public function setSharedBy($sharedBy) {
332
		if (!is_string($sharedBy)) {
333
			throw new \InvalidArgumentException();
334
		}
335
		//TODO checks
336
		$this->sharedBy = $sharedBy;
337
338
		return $this;
339
	}
340
341
	/**
342
	 * @inheritdoc
343
	 */
344
	public function getSharedBy() {
345
		//TODO check if set
346
		return $this->sharedBy;
347
	}
348
349
	/**
350
	 * @inheritdoc
351
	 */
352
	public function setShareOwner($shareOwner) {
353
		if (!is_string($shareOwner)) {
354
			throw new \InvalidArgumentException();
355
		}
356
		//TODO checks
357
358
		$this->shareOwner = $shareOwner;
359
		return $this;
360
	}
361
362
	/**
363
	 * @inheritdoc
364
	 */
365
	public function getShareOwner() {
366
		//TODO check if set
367
		return $this->shareOwner;
368
	}
369
370
	/**
371
	 * @inheritdoc
372
	 */
373
	public function setPassword($password) {
374
		$this->password = $password;
375
		return $this;
376
	}
377
378
	/**
379
	 * @inheritdoc
380
	 */
381
	public function getPassword() {
382
		return $this->password;
383
	}
384
385
	/**
386
	 * @inheritdoc
387
	 */
388
	public function setToken($token) {
389
		$this->token = $token;
390
		return $this;
391
	}
392
393
	/**
394
	 * @inheritdoc
395
	 */
396
	public function getToken() {
397
		return $this->token;
398
	}
399
400
	/**
401
	 * Set the parent of this share
402
	 *
403
	 * @param int parent
404
	 * @return \OCP\Share\IShare
405
	 * @deprecated The new shares do not have parents. This is just here for legacy reasons.
406
	 */
407
	public function setParent($parent) {
408
		$this->parent = $parent;
409
		return $this;
410
	}
411
412
	/**
413
	 * Get the parent of this share.
414
	 *
415
	 * @return int
416
	 * @deprecated The new shares do not have parents. This is just here for legacy reasons.
417
	 */
418
	public function getParent() {
419
		return $this->parent;
420
	}
421
422
	/**
423
	 * @inheritdoc
424
	 */
425
	public function setTarget($target) {
426
		$this->target = $target;
427
		return $this;
428
	}
429
430
	/**
431
	 * @inheritdoc
432
	 */
433
	public function getTarget() {
434
		return $this->target;
435
	}
436
437
	/**
438
	 * @inheritdoc
439
	 */
440
	public function setShareTime(\DateTime $shareTime) {
441
		$this->shareTime = $shareTime;
442
		return $this;
443
	}
444
445
	/**
446
	 * @inheritdoc
447
	 */
448
	public function getShareTime() {
449
		return $this->shareTime;
450
	}
451
452
	/**
453
	 * @inheritdoc
454
	 */
455
	public function setMailSend($mailSend) {
456
		$this->mailSend = $mailSend;
457
		return $this;
458
	}
459
460
	/**
461
	 * @inheritdoc
462
	 */
463
	public function getMailSend() {
464
		return $this->mailSend;
465
	}
466
467
	/**
468
	 * @inheritdoc
469
	 */
470
	public function setNodeCacheEntry(ICacheEntry $entry) {
471
		$this->nodeCacheEntry = $entry;
472
	}
473
474
	/**
475
	 * @inheritdoc
476
	 */
477
	public function getNodeCacheEntry() {
478
		return $this->nodeCacheEntry;
479
	}
480
}
481