Completed
Pull Request — master (#461)
by Maxence
01:52
created

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