Completed
Pull Request — master (#384)
by Tortue
02:31
created

BaseMember   D

Complexity

Total Complexity 58

Size/Duplication

Total Lines 423
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 58
lcom 2
cbo 2
dl 0
loc 423
rs 4.5599
c 0
b 0
f 0

40 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A setCircleId() 0 5 1
A getCircleId() 0 3 1
A setCircleContactGroupName() 0 5 1
A getCircleContactGroupName() 0 3 1
A getType() 0 3 1
A setType() 0 3 1
A getViewerType() 0 7 2
A setUserId() 0 6 1
A getUserId() 0 3 1
A getLevelString() 0 16 6
A getTypeString() 0 14 5
A setMemberId() 0 5 1
A getMemberId() 0 3 1
A setDisplayName() 0 5 1
A getDisplayName() 0 3 1
A setLevel() 0 5 1
A getLevel() 0 3 1
A setNote() 0 5 1
A getNote() 0 3 1
A setContactId() 0 5 1
A getContactId() 0 3 1
A setContactMeta() 0 5 1
A getContactMeta() 0 3 1
A addContactMeta() 0 5 1
A addContactMetaArray() 0 9 2
A setContactMetaArray() 0 5 1
A setStatus() 0 9 2
A getStatus() 0 3 1
A setJoined() 0 5 1
A getJoined() 0 3 1
A getJoinedSince() 0 3 1
A setJoinedSince() 0 3 1
A isLevel() 0 3 1
A isAlmostMember() 0 4 2
A setAsAMember() 0 4 1
A fromArray() 0 19 2
A fromJSON() 0 3 1
A jsonSerialize() 0 15 1
A getTypeName() 0 12 5

How to fix   Complexity   

Complex Class

Complex classes like BaseMember 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 BaseMember, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2017
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
namespace OCA\Circles\Model;
28
29
use OCA\Circles\AppInfo\Application;
30
use OCA\Circles\Service\MiscService;
31
use OCP\IL10N;
32
33
class BaseMember implements \JsonSerializable {
34
35
	const LEVEL_NONE = 0;
36
	const LEVEL_MEMBER = 1;
37
	const LEVEL_MODERATOR = 4;
38
	const LEVEL_ADMIN = 8;
39
	const LEVEL_OWNER = 9;
40
41
	const STATUS_NONMEMBER = 'Unknown';
42
	const STATUS_INVITED = 'Invited';
43
	const STATUS_REQUEST = 'Requesting';
44
	const STATUS_MEMBER = 'Member';
45
	const STATUS_BLOCKED = 'Blocked';
46
	const STATUS_KICKED = 'Kicked';
47
48
	const TYPE_USER = 1;
49
	const TYPE_GROUP = 2;
50
	const TYPE_MAIL = 3;
51
	const TYPE_CONTACT = 4;
52
53
	/** @var string */
54
	private $circleUniqueId;
55
56
	/** @var string */
57
	private $circleContactGroupName;
58
59
	/** @var IL10N */
60
	protected $l10n;
61
62
	/** @var string */
63
	private $userId = '';
64
65
	/** @var string */
66
	private $memberId = '';
67
68
	/** @var int */
69
	private $type = self::TYPE_USER;
70
71
	/** @var string */
72
	private $displayName;
73
74
	/** @var int */
75
	private $level;
76
77
	/** @var string */
78
	private $status;
79
80
	/** @var string */
81
	private $contactId = '';
82
83
	/** @var array */
84
	private $contactMeta = [];
85
86
	/** @var string */
87
	private $note;
88
89
	/** @var string */
90
	private $joined;
91
92
	/** @var int */
93
	private $joinedSince;
94
95
	/** @var bool */
96
	protected $broadcasting = true;
97
98
	/**
99
	 * BaseMember constructor.
100
	 *
101
	 * @param string $circleUniqueId
102
	 * @param string $userId
103
	 * @param int $type
104
	 */
105
	public function __construct($userId = '', $type = 0, $circleUniqueId = '') {
106
		$this->l10n = \OC::$server->getL10N(Application::APP_NAME);
107
108
		$this->setType($type);
109
		$this->setUserId($userId);
110
		$this->setCircleId($circleUniqueId);
111
		$this->setLevel(Member::LEVEL_NONE);
112
		$this->setStatus(Member::STATUS_NONMEMBER);
113
	}
114
115
116
	/**
117
	 * @param string $circleUniqueId
118
	 *
119
	 * @return $this
120
	 */
121
	public function setCircleId($circleUniqueId) {
122
		$this->circleUniqueId = $circleUniqueId;
123
124
		return $this;
125
	}
126
127
	/**
128
	 * @return string
129
	 */
130
	public function getCircleId() {
131
		return $this->circleUniqueId;
132
	}
133
134
135
	/**
136
	 * @param string $circleContactGroupName
137
	 *
138
	 * @return $this
139
	 */
140
	public function setCircleContactGroupName($circleContactGroupName): self {
141
		$this->circleContactGroupName = $circleContactGroupName;
142
143
		return $this;
144
	}
145
146
	/**
147
	 * @return string
148
	 */
149
	public function getCircleContactGroupName(): string {
150
		return $this->circleUniqueId;
151
	}
152
153
154
	/**
155
	 * @return int
156
	 */
157
	public function getType() {
158
		return $this->type;
159
	}
160
161
	public function setType($type) {
162
		$this->type = (int)$type;
163
	}
164
165
166
	public function getViewerType() {
167
		if ($this->getType() === 2) {
168
			return 'group';
169
		} else {
170
			return 'user';
171
		}
172
	}
173
174
175
	public function setUserId($userId) {
176
		$this->userId = $userId;
177
		$this->setDisplayName(MiscService::getDisplay($userId, $this->getType()));
178
179
		return $this;
180
	}
181
182
	public function getUserId() {
183
		return $this->userId;
184
	}
185
186
187
	public function setMemberId($memberId) {
188
		$this->memberId = $memberId;
189
190
		return $this;
191
	}
192
193
	public function getMemberId() {
194
		return $this->memberId;
195
	}
196
197
198
	public function setDisplayName($display) {
199
		$this->displayName = $display;
200
201
		return $this;
202
	}
203
204
	public function getDisplayName() {
205
		return $this->displayName;
206
	}
207
208
209
	public function setLevel($level) {
210
		$this->level = (int)$level;
211
212
		return $this;
213
	}
214
215
	public function getLevel() {
216
		return $this->level;
217
	}
218
219
220
	public function setNote($note) {
221
		$this->note = $note;
222
223
		return $this;
224
	}
225
226
	public function getNote() {
227
		return $this->note;
228
	}
229
230
231
	public function setContactId($contactId) {
232
		$this->contactId = $contactId;
233
234
		return $this;
235
	}
236
237
	public function getContactId() {
238
		return $this->contactId;
239
	}
240
241
242
	/**
243
	 * @param array $contactMeta
244
	 *
245
	 * @return $this
246
	 */
247
	public function setContactMeta(array $contactMeta): self {
248
		$this->contactMeta = $contactMeta;
249
250
		return $this;
251
	}
252
253
	/**
254
	 * @return array
255
	 */
256
	public function getContactMeta(): array {
257
		return $this->contactMeta;
258
	}
259
260
	/**
261
	 * @param string $k
262
	 * @param string $v
263
	 *
264
	 * @return $this
265
	 */
266
	public function addContactMeta(string $k, string $v): self {
267
		$this->contactMeta[$k] = $v;
268
269
		return $this;
270
	}
271
272
	/**
273
	 * @param string $k
274
	 * @param string $v
275
	 *
276
	 * @return $this
277
	 */
278
	public function addContactMetaArray(string $k, string $v): self {
279
		if (!array_key_exists($k, $this->contactMeta)) {
280
			$this->contactMeta[$k] = [];
281
		}
282
283
		$this->contactMeta[$k][] = $v;
284
285
		return $this;
286
	}
287
288
	/**
289
	 * @param string $k
290
	 * @param array $v
291
	 *
292
	 * @return $this
293
	 */
294
	public function setContactMetaArray(string $k, array $v): self {
295
		$this->contactMeta[$k] = $v;
296
297
		return $this;
298
	}
299
300
301
	/**
302
	 * @param $status
303
	 *
304
	 * @return $this
305
	 */
306
	public function setStatus($status) {
307
		if (is_null($status)) {
308
			$this->status = self::STATUS_NONMEMBER;
309
		} else {
310
			$this->status = $status;
311
		}
312
313
		return $this;
314
	}
315
316
	public function getStatus() {
317
		return $this->status;
318
	}
319
320
321
	public function setJoined($joined) {
322
		$this->joined = $joined;
323
324
		return $this;
325
	}
326
327
	public function getJoined() {
328
		return $this->joined;
329
	}
330
331
332
	public function getJoinedSince(): int {
333
		return $this->joinedSince;
334
	}
335
336
	public function setJoinedSince(int $since) {
337
		$this->joinedSince = $since;
338
	}
339
340
341
	public function isLevel($level) {
342
		return ($this->getLevel() >= $level);
343
	}
344
345
346
	public function isAlmostMember() {
347
		return ($this->getStatus() === Member::STATUS_INVITED
348
				|| $this->getStatus() === Member::STATUS_REQUEST);
349
	}
350
351
352
	protected function setAsAMember($level = 1) {
353
		$this->setStatus(Member::STATUS_MEMBER);
354
		$this->setLevel($level);
355
	}
356
357
358
	/**
359
	 * @param $arr
360
	 *
361
	 * @return null|Member
362
	 */
363
	public static function fromArray($arr) {
364
		if ($arr === null) {
365
			return null;
366
		}
367
368
		$member = new Member();
369
		$member->setCircleId($arr['circle_id']);
370
		$member->setLevel($arr['level']);
371
372
		$member->setType(MiscService::get($arr, 'user_type'));
373
		$member->setType(MiscService::get($arr, 'type', $member->getType()));
374
375
		$member->setUserId($arr['user_id']);
376
		$member->setStatus($arr['status']);
377
		$member->setNote($arr['note']);
378
		$member->setJoined($arr['joined']);
379
380
		return $member;
381
	}
382
383
384
	/**
385
	 * @param $json
386
	 *
387
	 * @return Member
0 ignored issues
show
Documentation introduced by
Should the return type not be Member|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
388
	 */
389
	public static function fromJSON($json) {
390
		return self::fromArray(json_decode($json, true));
391
	}
392
393
394
	public function jsonSerialize() {
395
		return [
396
			'circle_id'    => $this->getCircleId(),
397
			'member_id'    => $this->getMemberId(),
398
			'user_id'      => $this->getUserId(),
399
			'user_type'    => $this->getType(),
400
			'display_name' => $this->getDisplayName(),
401
			'contact_id'   => $this->getContactId(),
402
			'level'        => $this->getLevel(),
403
			'level_string' => $this->getLevelString(),
404
			'status'       => $this->getStatus(),
405
			'note'         => $this->getNote(),
406
			'joined'       => $this->getJoined()
407
		];
408
	}
409
410
	public function getLevelString() {
411
		switch ($this->getLevel()) {
412
			case self::LEVEL_NONE:
413
				return 'Not a member';
414
			case self::LEVEL_MEMBER:
415
				return 'Member';
416
			case self::LEVEL_MODERATOR:
417
				return 'Moderator';
418
			case self::LEVEL_ADMIN:
419
				return 'Admin';
420
			case self::LEVEL_OWNER:
421
				return 'Owner';
422
		}
423
424
		return 'none';
425
	}
426
427
428
	public function getTypeString() {
429
		switch ($this->getType()) {
430
			case self::TYPE_USER:
431
				return 'Local Member';
432
			case self::TYPE_GROUP:
433
				return 'Group';
434
			case self::TYPE_MAIL:
435
				return 'Mail address';
436
			case self::TYPE_CONTACT:
437
				return 'Contact';
438
		}
439
440
		return 'none';
441
	}
442
443
	public function getTypeName() {
444
		switch ($this->getType()) {
445
			case self::TYPE_USER:
446
			case self::TYPE_MAIL:
447
			case self::TYPE_CONTACT:
448
				return 'user';
449
			case self::TYPE_GROUP:
450
				return 'user-group';
451
		}
452
453
		return 'none';
454
	}
455
}
456