Completed
Push — master ( 138f00...e3741d )
by Maxence
16s queued 10s
created

BaseMember::setUserId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 $cachedName = '';
72
73
	/** @var int */
74
	private $cachedUpdate = 0;
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
164
		return $this;
165
	}
166
167
	public function getUserId() {
168
		return $this->userId;
169
	}
170
171
172
	public function setMemberId($memberId) {
173
		$this->memberId = $memberId;
174
175
		return $this;
176
	}
177
178
	public function getMemberId() {
179
		return $this->memberId;
180
	}
181
182
183
	public function setCachedName($display) {
184
		$this->cachedName = $display;
185
186
		return $this;
187
	}
188
189
	public function getCachedName() {
190
		if ($this->cachedName === '') {
191
			return $this->userId;
192
		}
193
194
		return $this->cachedName;
195
	}
196
197
198
	public function setCachedUpdate(int $time) {
199
		$this->cachedUpdate = $time;
200
201
		return $this;
202
	}
203
204
	public function getCachedUpdate(): int {
205
		return $this->cachedUpdate;
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 setInstance($instance) {
232
		$this->instance = $instance;
233
234
		return $this;
235
	}
236
237
	public function getInstance() {
238
		return $this->instance;
239
	}
240
241
242
	public function setContactId($contactId) {
243
		$this->contactId = $contactId;
244
245
		return $this;
246
	}
247
248
	public function getContactId() {
249
		return $this->contactId;
250
	}
251
252
253
	/**
254
	 * @param array $contactMeta
255
	 *
256
	 * @return $this
257
	 */
258
	public function setContactMeta(array $contactMeta): self {
259
		$this->contactMeta = $contactMeta;
260
261
		return $this;
262
	}
263
264
	/**
265
	 * @return array
266
	 */
267
	public function getContactMeta(): array {
268
		return $this->contactMeta;
269
	}
270
271
	/**
272
	 * @param string $k
273
	 * @param string $v
274
	 *
275
	 * @return $this
276
	 */
277
	public function addContactMeta(string $k, string $v): self {
278
		$this->contactMeta[$k] = $v;
279
280
		return $this;
281
	}
282
283
	/**
284
	 * @param string $k
285
	 * @param string $v
286
	 *
287
	 * @return $this
288
	 */
289
	public function addContactMetaArray(string $k, string $v): self {
290
		if (!array_key_exists($k, $this->contactMeta)) {
291
			$this->contactMeta[$k] = [];
292
		}
293
294
		$this->contactMeta[$k][] = $v;
295
296
		return $this;
297
	}
298
299
	/**
300
	 * @param string $k
301
	 * @param array $v
302
	 *
303
	 * @return $this
304
	 */
305
	public function setContactMetaArray(string $k, array $v): self {
306
		$this->contactMeta[$k] = $v;
307
308
		return $this;
309
	}
310
311
312
	/**
313
	 * @param $status
314
	 *
315
	 * @return $this
316
	 */
317
	public function setStatus($status) {
318
		if (is_null($status)) {
319
			$this->status = self::STATUS_NONMEMBER;
320
		} else {
321
			$this->status = $status;
322
		}
323
324
		return $this;
325
	}
326
327
	public function getStatus() {
328
		return $this->status;
329
	}
330
331
332
	public function setJoined($joined) {
333
		$this->joined = $joined;
334
335
		return $this;
336
	}
337
338
	public function getJoined() {
339
		return $this->joined;
340
	}
341
342
343
	public function getJoinedSince(): int {
344
		return $this->joinedSince;
345
	}
346
347
	public function setJoinedSince(int $since) {
348
		$this->joinedSince = $since;
349
	}
350
351
352
	public function isLevel($level) {
353
		return ($this->getLevel() >= $level);
354
	}
355
356
357
	public function isAlmostMember() {
358
		return ($this->getStatus() === Member::STATUS_INVITED
359
				|| $this->getStatus() === Member::STATUS_REQUEST);
360
	}
361
362
363
	protected function setAsAMember($level = 1) {
364
		$this->setStatus(Member::STATUS_MEMBER);
365
		$this->setLevel($level);
366
	}
367
368
369
	/**
370
	 * @param $arr
371
	 *
372
	 * @return null|Member
373
	 */
374
	public static function fromArray($arr) {
375
		if ($arr === null) {
376
			return null;
377
		}
378
379
		$member = new Member();
380
		$member->setCircleId($arr['circle_id']);
381
		$member->setMemberId($arr['member_id']);
382
		if (array_key_exists('cached_name', $arr)) {
383
			$member->setCachedName($arr['cached_name']);
384
		}
385
386
		$member->setLevel($arr['level']);
387
388
		$member->setType(MiscService::get($arr, 'user_type'));
389
		$member->setType(MiscService::get($arr, 'type', $member->getType()));
390
391
		$member->setInstance($arr['instance']);
392
		$member->setUserId($arr['user_id']);
393
		$member->setStatus($arr['status']);
394
		$member->setInstance($arr['instance']);
395
		$member->setNote($arr['note']);
396
		$member->setJoined($arr['joined']);
397
398
		return $member;
399
	}
400
401
402
	/**
403
	 * @param $json
404
	 *
405
	 * @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...
406
	 */
407
	public static function fromJSON($json) {
408
		return self::fromArray(json_decode($json, true));
409
	}
410
411
412
	public function jsonSerialize() {
413
		return [
414
			'circle_id'    => $this->getCircleId(),
415
			'member_id'    => $this->getMemberId(),
416
			'user_id'      => $this->getUserId(),
417
			'user_type'    => $this->getType(),
418
			'cached_name'  => $this->getCachedName(),
419
			'contact_id'   => $this->getContactId(),
420
			'level'        => $this->getLevel(),
421
			'level_string' => $this->getLevelString(),
422
			'status'       => $this->getStatus(),
423
			'instance'     => $this->getInstance(),
424
			'note'         => $this->getNote(),
425
			'joined'       => $this->getJoined()
426
		];
427
	}
428
429
	public function getLevelString() {
430
		switch ($this->getLevel()) {
431
			case self::LEVEL_NONE:
432
				return 'Not a member';
433
			case self::LEVEL_MEMBER:
434
				return 'Member';
435
			case self::LEVEL_MODERATOR:
436
				return 'Moderator';
437
			case self::LEVEL_ADMIN:
438
				return 'Admin';
439
			case self::LEVEL_OWNER:
440
				return 'Owner';
441
		}
442
443
		return 'none';
444
	}
445
446
447
	public function getTypeString() {
448
		switch ($this->getType()) {
449
			case self::TYPE_USER:
450
				return 'Local Member';
451
			case self::TYPE_GROUP:
452
				return 'Group';
453
			case self::TYPE_MAIL:
454
				return 'Mail address';
455
			case self::TYPE_CONTACT:
456
				return 'Contact';
457
		}
458
459
		return 'none';
460
	}
461
462
	public function getTypeName() {
463
		switch ($this->getType()) {
464
			case self::TYPE_USER:
465
			case self::TYPE_MAIL:
466
			case self::TYPE_CONTACT:
467
				return 'user';
468
			case self::TYPE_GROUP:
469
				return 'user-group';
470
		}
471
472
		return 'none';
473
	}
474
}
475