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

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