Completed
Push — master ( fb5007...9bba10 )
by Maxence
02:15
created

BaseMember::getCircleContactGroupName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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