Completed
Push — master ( 4f3689...17b456 )
by Maxence
02:12 queued 11s
created

BaseMember::getTypeString()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.4888
cc 5
nc 5
nop 0
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 $instance = '';
91
92
	/** @var string */
93
	private $joined = '';
94
95
	/** @var int */
96
	private $joinedSince;
97
98
	/** @var bool */
99
	protected $broadcasting = true;
100
101
	/**
102
	 * BaseMember constructor.
103
	 *
104
	 * @param string $circleUniqueId
105
	 * @param string $userId
106
	 * @param int $type
107
	 */
108
	public function __construct($userId = '', $type = 0, $circleUniqueId = '') {
109
		$this->l10n = \OC::$server->getL10N(Application::APP_NAME);
110
111
		$this->setType($type);
112
		$this->setUserId($userId);
113
		$this->setCircleId($circleUniqueId);
114
		$this->setLevel(Member::LEVEL_NONE);
115
		$this->setStatus(Member::STATUS_NONMEMBER);
116
	}
117
118
119
	/**
120
	 * @param string $circleUniqueId
121
	 *
122
	 * @return $this
123
	 */
124
	public function setCircleId($circleUniqueId) {
125
		$this->circleUniqueId = $circleUniqueId;
126
127
		return $this;
128
	}
129
130
	/**
131
	 * @return string
132
	 */
133
	public function getCircleId() {
134
		return $this->circleUniqueId;
135
	}
136
137
138
	/**
139
	 * @param string $circleContactGroupName
140
	 *
141
	 * @return $this
142
	 */
143
	public function setCircleContactGroupName($circleContactGroupName): self {
144
		$this->circleContactGroupName = $circleContactGroupName;
145
146
		return $this;
147
	}
148
149
	/**
150
	 * @return string
151
	 */
152
	public function getCircleContactGroupName(): string {
153
		return $this->circleUniqueId;
154
	}
155
156
157
	/**
158
	 * @return int
159
	 */
160
	public function getType() {
161
		return $this->type;
162
	}
163
164
	public function setType($type) {
165
		$this->type = (int)$type;
166
	}
167
168
169
	public function getViewerType() {
170
		if ($this->getType() === 2) {
171
			return 'group';
172
		} else {
173
			return 'user';
174
		}
175
	}
176
177
178
	public function setUserId($userId) {
179
		$this->userId = $userId;
180
		$this->setDisplayName(MiscService::getDisplay($userId, $this->getType()));
181
182
		return $this;
183
	}
184
185
	public function getUserId() {
186
		return $this->userId;
187
	}
188
189
190
	public function setMemberId($memberId) {
191
		$this->memberId = $memberId;
192
193
		return $this;
194
	}
195
196
	public function getMemberId() {
197
		return $this->memberId;
198
	}
199
200
201
	public function setDisplayName($display) {
202
		$this->displayName = $display;
203
204
		return $this;
205
	}
206
207
	public function getDisplayName() {
208
		return $this->displayName;
209
	}
210
211
212
	public function setLevel($level) {
213
		$this->level = (int)$level;
214
215
		return $this;
216
	}
217
218
	public function getLevel() {
219
		return $this->level;
220
	}
221
222
223
	public function setNote($note) {
224
		$this->note = $note;
225
226
		return $this;
227
	}
228
229
	public function getNote() {
230
		return $this->note;
231
	}
232
233
234
	public function setInstance($instance) {
235
		$this->instance = $instance;
236
237
		return $this;
238
	}
239
240
	public function getInstance() {
241
		return $this->instance;
242
	}
243
244
245
	public function setContactId($contactId) {
246
		$this->contactId = $contactId;
247
248
		return $this;
249
	}
250
251
	public function getContactId() {
252
		return $this->contactId;
253
	}
254
255
256
	/**
257
	 * @param array $contactMeta
258
	 *
259
	 * @return $this
260
	 */
261
	public function setContactMeta(array $contactMeta): self {
262
		$this->contactMeta = $contactMeta;
263
264
		return $this;
265
	}
266
267
	/**
268
	 * @return array
269
	 */
270
	public function getContactMeta(): array {
271
		return $this->contactMeta;
272
	}
273
274
	/**
275
	 * @param string $k
276
	 * @param string $v
277
	 *
278
	 * @return $this
279
	 */
280
	public function addContactMeta(string $k, string $v): self {
281
		$this->contactMeta[$k] = $v;
282
283
		return $this;
284
	}
285
286
	/**
287
	 * @param string $k
288
	 * @param string $v
289
	 *
290
	 * @return $this
291
	 */
292
	public function addContactMetaArray(string $k, string $v): self {
293
		if (!array_key_exists($k, $this->contactMeta)) {
294
			$this->contactMeta[$k] = [];
295
		}
296
297
		$this->contactMeta[$k][] = $v;
298
299
		return $this;
300
	}
301
302
	/**
303
	 * @param string $k
304
	 * @param array $v
305
	 *
306
	 * @return $this
307
	 */
308
	public function setContactMetaArray(string $k, array $v): self {
309
		$this->contactMeta[$k] = $v;
310
311
		return $this;
312
	}
313
314
315
	/**
316
	 * @param $status
317
	 *
318
	 * @return $this
319
	 */
320
	public function setStatus($status) {
321
		if (is_null($status)) {
322
			$this->status = self::STATUS_NONMEMBER;
323
		} else {
324
			$this->status = $status;
325
		}
326
327
		return $this;
328
	}
329
330
	public function getStatus() {
331
		return $this->status;
332
	}
333
334
335
	public function setJoined($joined) {
336
		$this->joined = $joined;
337
338
		return $this;
339
	}
340
341
	public function getJoined() {
342
		return $this->joined;
343
	}
344
345
346
	public function getJoinedSince(): int {
347
		return $this->joinedSince;
348
	}
349
350
	public function setJoinedSince(int $since) {
351
		$this->joinedSince = $since;
352
	}
353
354
355
	public function isLevel($level) {
356
		return ($this->getLevel() >= $level);
357
	}
358
359
360
	public function isAlmostMember() {
361
		return ($this->getStatus() === Member::STATUS_INVITED
362
				|| $this->getStatus() === Member::STATUS_REQUEST);
363
	}
364
365
366
	protected function setAsAMember($level = 1) {
367
		$this->setStatus(Member::STATUS_MEMBER);
368
		$this->setLevel($level);
369
	}
370
371
372
	/**
373
	 * @param $arr
374
	 *
375
	 * @return null|Member
376
	 */
377
	public static function fromArray($arr) {
378
		if ($arr === null) {
379
			return null;
380
		}
381
382
		$member = new Member();
383
		$member->setCircleId($arr['circle_id']);
384
		$member->setMemberId($arr['member_id']);
385
		$member->setLevel($arr['level']);
386
387
		$member->setType(MiscService::get($arr, 'user_type'));
388
		$member->setType(MiscService::get($arr, 'type', $member->getType()));
389
390
		$member->setInstance($arr['instance']);
391
		$member->setUserId($arr['user_id']);
392
		$member->setStatus($arr['status']);
393
		$member->setInstance($arr['instance']);
394
		$member->setNote($arr['note']);
395
		$member->setJoined($arr['joined']);
396
397
		return $member;
398
	}
399
400
401
	/**
402
	 * @param $json
403
	 *
404
	 * @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...
405
	 */
406
	public static function fromJSON($json) {
407
		return self::fromArray(json_decode($json, true));
408
	}
409
410
411
	public function jsonSerialize() {
412
		return [
413
			'circle_id'    => $this->getCircleId(),
414
			'member_id'    => $this->getMemberId(),
415
			'user_id'      => $this->getUserId(),
416
			'user_type'    => $this->getType(),
417
			'display_name' => $this->getDisplayName(),
418
			'contact_id'   => $this->getContactId(),
419
			'level'        => $this->getLevel(),
420
			'level_string' => $this->getLevelString(),
421
			'status'       => $this->getStatus(),
422
			'instance'     => $this->getInstance(),
423
			'note'         => $this->getNote(),
424
			'joined'       => $this->getJoined()
425
		];
426
	}
427
428
	public function getLevelString() {
429
		switch ($this->getLevel()) {
430
			case self::LEVEL_NONE:
431
				return 'Not a member';
432
			case self::LEVEL_MEMBER:
433
				return 'Member';
434
			case self::LEVEL_MODERATOR:
435
				return 'Moderator';
436
			case self::LEVEL_ADMIN:
437
				return 'Admin';
438
			case self::LEVEL_OWNER:
439
				return 'Owner';
440
		}
441
442
		return 'none';
443
	}
444
445
446
	public function getTypeString() {
447
		switch ($this->getType()) {
448
			case self::TYPE_USER:
449
				return 'Local Member';
450
			case self::TYPE_GROUP:
451
				return 'Group';
452
			case self::TYPE_MAIL:
453
				return 'Mail address';
454
			case self::TYPE_CONTACT:
455
				return 'Contact';
456
		}
457
458
		return 'none';
459
	}
460
461
	public function getTypeName() {
462
		switch ($this->getType()) {
463
			case self::TYPE_USER:
464
			case self::TYPE_MAIL:
465
			case self::TYPE_CONTACT:
466
				return 'user';
467
			case self::TYPE_GROUP:
468
				return 'user-group';
469
		}
470
471
		return 'none';
472
	}
473
}
474