Completed
Pull Request — master (#347)
by Maxence
01:46
created

BaseMember::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
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 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
188
	public function setMemberId($memberId) {
189
		$this->memberId = $memberId;
190
191
		return $this;
192
	}
193
194
	public function getMemberId() {
195
		return $this->memberId;
196
	}
197
198
199
	public function setDisplayName($display) {
200
		$this->displayName = $display;
201
202
		return $this;
203
	}
204
205
	public function getDisplayName() {
206
		return $this->displayName;
207
	}
208
209
210
	public function setLevel($level) {
211
		$this->level = (int)$level;
212
213
		return $this;
214
	}
215
216
	public function getLevel() {
217
		return $this->level;
218
	}
219
220
221
	public function setNote($note) {
222
		$this->note = $note;
223
224
		return $this;
225
	}
226
227
	public function getNote() {
228
		return $this->note;
229
	}
230
231
232
	public function setContactId($contactId) {
233
		$this->contactId = $contactId;
234
235
		return $this;
236
	}
237
238
	public function getContactId() {
239
		return $this->contactId;
240
	}
241
242
243
	/**
244
	 * @param array $contactMeta
245
	 *
246
	 * @return $this
247
	 */
248
	public function setContactMeta(array $contactMeta): self {
249
		$this->contactMeta = $contactMeta;
250
251
		return $this;
252
	}
253
254
	/**
255
	 * @return array
256
	 */
257
	public function getContactMeta(): array {
258
		return $this->contactMeta;
259
	}
260
261
	/**
262
	 * @param string $k
263
	 * @param string $v
264
	 *
265
	 * @return $this
266
	 */
267
	public function addContactMeta(string $k, string $v): self {
268
		$this->contactMeta[$k] = $v;
269
270
		return $this;
271
	}
272
273
	/**
274
	 * @param string $k
275
	 * @param string $v
276
	 *
277
	 * @return $this
278
	 */
279
	public function addContactMetaArray(string $k, string $v): self {
280
		if (!array_key_exists($k, $this->contactMeta)) {
281
			$this->contactMeta[$k] = [];
282
		}
283
284
		$this->contactMeta[$k][] = $v;
285
286
		return $this;
287
	}
288
289
	/**
290
	 * @param string $k
291
	 * @param array $v
292
	 *
293
	 * @return $this
294
	 */
295
	public function setContactMetaArray(string $k, array $v): self {
296
		$this->contactMeta[$k] = $v;
297
298
		return $this;
299
	}
300
301
302
	/**
303
	 * @param $status
304
	 *
305
	 * @return $this
306
	 */
307
	public function setStatus($status) {
308
		if (is_null($status)) {
309
			$this->status = self::STATUS_NONMEMBER;
310
		} else {
311
			$this->status = $status;
312
		}
313
314
		return $this;
315
	}
316
317
	public function getStatus() {
318
		return $this->status;
319
	}
320
321
322
	public function setJoined($joined) {
323
		$this->joined = $joined;
324
325
		return $this;
326
	}
327
328
	public function getJoined() {
329
		return $this->joined;
330
	}
331
332
333
	public function getJoinedSince(): int {
334
		return $this->joinedSince;
335
	}
336
337
	public function setJoinedSince(int $since) {
338
		$this->joinedSince = $since;
339
	}
340
341
342
	public function isLevel($level) {
343
		return ($this->getLevel() >= $level);
344
	}
345
346
347
	public function isAlmostMember() {
348
		return ($this->getStatus() === Member::STATUS_INVITED
349
				|| $this->getStatus() === Member::STATUS_REQUEST);
350
	}
351
352
353
	protected function setAsAMember($level = 1) {
354
		$this->setStatus(Member::STATUS_MEMBER);
355
		$this->setLevel($level);
356
	}
357
358
359
	/**
360
	 * @param $arr
361
	 *
362
	 * @return null|Member
363
	 */
364
	public static function fromArray($arr) {
365
		if ($arr === null) {
366
			return null;
367
		}
368
369
		$member = new Member();
370
		$member->setCircleId($arr['circle_id']);
371
		$member->setLevel($arr['level']);
372
373
		$member->setType(MiscService::get($arr, 'user_type'));
374
		$member->setType(MiscService::get($arr, 'type', $member->getType()));
375
376
		$member->setUserId($arr['user_id']);
377
		$member->setStatus($arr['status']);
378
		$member->setNote($arr['note']);
379
		$member->setJoined($arr['joined']);
380
381
		return $member;
382
	}
383
384
385
	/**
386
	 * @param $json
387
	 *
388
	 * @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...
389
	 */
390
	public static function fromJSON($json) {
391
		return self::fromArray(json_decode($json, true));
392
	}
393
394
395
	public function jsonSerialize() {
396
		return [
397
			'circle_id'    => $this->getCircleId(),
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