Completed
Pull Request — master (#347)
by Maxence
02:26
created

BaseMember::getLevelString()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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