Completed
Pull Request — master (#551)
by Maxence
02:27
created

Member::getSingleId()   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
declare(strict_types=1);
4
5
6
/**
7
 * Circles - Bring cloud-users closer together.
8
 *
9
 * This file is licensed under the Affero General Public License version 3 or
10
 * later. See the COPYING file.
11
 *
12
 * @author Maxence Lange <[email protected]>
13
 * @copyright 2021
14
 * @license GNU AGPL version 3 or any later version
15
 *
16
 * This program is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License as
18
 * published by the Free Software Foundation, either version 3 of the
19
 * License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28
 *
29
 */
30
31
32
namespace OCA\Circles\Model;
33
34
use daita\MySmallPhpTools\Db\Nextcloud\nc21\INC21QueryRow;
35
use daita\MySmallPhpTools\Model\Nextcloud\nc21\INC21Convert;
36
use daita\MySmallPhpTools\Traits\TArrayTools;
37
use DateTime;
38
use JsonSerializable;
39
use OCA\Circles\Exceptions\MemberNotFoundException;
40
use OCA\Circles\IFederatedUser;
41
42
43
/**
44
 * Class Member
45
 *
46
 * @package OCA\Circles\Model
47
 */
48
class Member extends ManagedModel implements IFederatedUser, INC21Convert, INC21QueryRow, JsonSerializable {
49
50
51
	use TArrayTools;
52
53
54
	const LEVEL_NONE = 0;
55
	const LEVEL_MEMBER = 1;
56
	const LEVEL_MODERATOR = 4;
57
	const LEVEL_ADMIN = 8;
58
	const LEVEL_OWNER = 9;
59
60
	const TYPE_CIRCLE = 16;
61
	const TYPE_USER = 1;
62
	const TYPE_GROUP = 2;
63
	const TYPE_MAIL = 3;
64
	const TYPE_CONTACT = 4;
65
66
	const STATUS_NONMEMBER = 'Unknown';
67
	const STATUS_INVITED = 'Invited';
68
	const STATUS_REQUEST = 'Requesting';
69
	const STATUS_MEMBER = 'Member';
70
	const STATUS_BLOCKED = 'Blocked';
71
	const STATUS_KICKED = 'Kicked';
72
73
	const ID_LENGTH = 15;
74
75
	static $DEF_LEVEL = [
76
		1 => 'Member',
77
		4 => 'Moderator',
78
		8 => 'Admin',
79
		9 => 'Owner'
80
	];
81
82
83
	/** @var string */
84
	private $id = '';
85
86
	/** @var string */
87
	private $circleId = '';
88
89
	/** @var string */
90
	private $singleId = '';
91
92
	/** @var string */
93
	private $userId = '';
94
95
	/** @var int */
96
	private $userType = self::TYPE_USER;
97
98
	/** @var string */
99
	private $instance = '';
100
101
	/** @var int */
102
	private $level = 0;
103
104
	/** @var string */
105
	private $status = 'Unknown';
106
107
	/** @var string */
108
	private $note = '';
109
110
	/** @var string */
111
	private $cachedName = '';
112
113
	/** @var int */
114
	private $cachedUpdate = 0;
115
116
	/** @var string */
117
	private $contactId = '';
118
119
	/** @var string */
120
	private $contactMeta = '';
121
122
	/** @var Circle */
123
	private $circle;
124
125
126
	/** @var int */
127
	private $joined = 0;
128
129
130
	/**
131
	 * Member constructor.
132
	 */
133
	public function __construct() {
134
	}
135
136
137
	/**
138
	 * @param string $id
139
	 *
140
	 * @return $this
141
	 */
142
	public function setId(string $id): self {
143
		$this->id = $id;
144
145
		return $this;
146
	}
147
148
	/**
149
	 * @return string
150
	 */
151
	public function getId(): string {
152
		return $this->id;
153
	}
154
155
156
	/**
157
	 * @param string $circleId
158
	 *
159
	 * @return Member
160
	 */
161
	public function setCircleId(string $circleId): self {
162
		$this->circleId = $circleId;
163
164
		return $this;
165
	}
166
167
	/**
168
	 * @return string
169
	 */
170
	public function getCircleId(): string {
171
		return $this->circleId;
172
	}
173
174
175
	/**
176
	 * This should replace user_id, user_type and instance; and will use the data from Circle with
177
	 * Config=CFG_SINGLE
178
	 *
179
	 * @param string $singleId
180
	 *
181
	 * @return $this
182
	 */
183
	public function setSingleId(string $singleId): self {
184
		$this->singleId = $singleId;
185
186
		return $this;
187
	}
188
189
	/**
190
	 * @return string
191
	 */
192
	public function getSingleId(): string {
193
		return $this->singleId;
194
	}
195
196
197
	/**
198
	 * @param string $userId
199
	 *
200
	 * @return Member
201
	 */
202
	public function setUserId(string $userId): self {
203
		$this->userId = $userId;
204
205
		return $this;
206
	}
207
208
	/**
209
	 * @return string
210
	 */
211
	public function getUserId(): string {
212
		return $this->userId;
213
	}
214
215
216
	/**
217
	 * @param int $userType
218
	 *
219
	 * @return Member
220
	 */
221
	public function setUserType(int $userType): self {
222
		$this->userType = $userType;
223
224
		return $this;
225
	}
226
227
	/**
228
	 * @return int
229
	 */
230
	public function getUserType(): int {
231
		return $this->userType;
232
	}
233
234
235
	/**
236
	 * @param string $instance
237
	 *
238
	 * @return Member
239
	 */
240
	public function setInstance(string $instance): self {
241
		$this->instance = $instance;
242
243
		return $this;
244
	}
245
246
	/**
247
	 * @return string
248
	 */
249
	public function getInstance(): string {
250
		return $this->instance;
251
	}
252
253
254
	/**
255
	 * @param int $level
256
	 *
257
	 * @return Member
258
	 */
259
	public function setLevel(int $level): self {
260
		$this->level = $level;
261
262
		return $this;
263
	}
264
265
	/**
266
	 * @return int
267
	 */
268
	public function getLevel(): int {
269
		return $this->level;
270
	}
271
272
273
	/**
274
	 * @param string $status
275
	 *
276
	 * @return Member
277
	 */
278
	public function setStatus(string $status): self {
279
		$this->status = $status;
280
281
		return $this;
282
	}
283
284
	/**
285
	 * @return string
286
	 */
287
	public function getStatus(): string {
288
		return $this->status;
289
	}
290
291
292
	/**
293
	 * @param string $note
294
	 *
295
	 * @return Member
296
	 */
297
	public function setNote(string $note): self {
298
		$this->note = $note;
299
300
		return $this;
301
	}
302
303
	/**
304
	 * @return string
305
	 */
306
	public function getNote(): string {
307
		return $this->note;
308
	}
309
310
311
	/**
312
	 * @param string $cachedName
313
	 *
314
	 * @return Member
315
	 */
316
	public function setCachedName(string $cachedName): self {
317
		$this->cachedName = $cachedName;
318
319
		return $this;
320
	}
321
322
323
	/**
324
	 * @param int $cachedUpdate
325
	 *
326
	 * @return Member
327
	 */
328
	public function setCachedUpdate(int $cachedUpdate): self {
329
		$this->cachedUpdate = $cachedUpdate;
330
331
		return $this;
332
	}
333
334
	/**
335
	 * @return int
336
	 */
337
	public function getCachedUpdate(): int {
338
		return $this->cachedUpdate;
339
	}
340
341
342
	/**
343
	 * @return string
344
	 */
345
	public function getCachedName(): string {
346
		return $this->cachedName;
347
	}
348
349
350
	/**
351
	 * @param string $contactId
352
	 *
353
	 * @return Member
354
	 */
355
	public function setContactId(string $contactId): self {
356
		$this->contactId = $contactId;
357
358
		return $this;
359
	}
360
361
	/**
362
	 * @return string
363
	 */
364
	public function getContactId(): string {
365
		return $this->contactId;
366
	}
367
368
369
	/**
370
	 * @param string $contactMeta
371
	 *
372
	 * @return Member
373
	 */
374
	public function setContactMeta(string $contactMeta): self {
375
		$this->contactMeta = $contactMeta;
376
377
		return $this;
378
	}
379
380
	/**
381
	 * @return string
382
	 */
383
	public function getContactMeta(): string {
384
		return $this->contactMeta;
385
	}
386
387
388
	/**
389
	 * @param Circle $circle
390
	 *
391
	 * @return self
392
	 */
393
	public function setCircle(Circle $circle): self {
394
		$this->circle = $circle;
395
396
		return $this;
397
	}
398
399
	/**
400
	 * @return Circle
401
	 */
402
	public function getCircle(): Circle {
403
		return $this->circle;
404
	}
405
406
	/**
407
	 * @return bool
408
	 */
409
	public function hasCircle(): bool {
410
		return (!is_null($this->circle));
411
	}
412
413
414
	/**
415
	 * @param int $joined
416
	 *
417
	 * @return Member
418
	 */
419
	public function setJoined(int $joined): self {
420
		$this->joined = $joined;
421
422
		return $this;
423
	}
424
425
	/**
426
	 * @return int
427
	 */
428
	public function getJoined(): int {
429
		return $this->joined;
430
	}
431
432
433
	/**
434
	 * @return bool
435
	 */
436
	public function isMember(): bool {
437
		return ($this->level > 0);
438
	}
439
440
441
	/**
442
	 * @param Member $member
443
	 *
444
	 * @return bool
445
	 */
446
	public function compareWith(Member $member): bool {
447
		if ($this->getId() !== $member->getId()
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !($this->getId() ... $member->getStatus());.
Loading history...
448
			|| $this->getCircleId() !== $member->getCircleId()
449
			|| $this->getSingleId() !== $member->getSingleId()
450
			|| $this->getUserId() !== $member->getUserId()
451
			|| $this->getUserType() <> $member->getUserType()
452
			|| $this->getInstance() !== $member->getInstance()
453
			|| $this->getLevel() <> $member->getLevel()
454
			|| $this->getStatus() !== $member->getStatus()) {
455
			return false;
456
		}
457
458
		return true;
459
	}
460
461
462
	/**
463
	 * @param array $data
464
	 *
465
	 * @return $this
466
	 */
467
	public function import(array $data): INC21Convert {
468
		$this->setId($this->get('id', $data));
469
		$this->setCircleId($this->get('circle_id', $data));
470
		$this->setSingleId($this->get('single_id', $data));
471
		$this->setUserId($this->get('user_id', $data));
472
		$this->setUserType($this->getInt('user_type', $data));
473
		$this->setInstance($this->get('instance', $data));
474
		$this->setLevel($this->getInt('level', $data));
475
		$this->setStatus($this->get('status', $data));
476
		$this->setCachedName($this->get('cached_name', $data));
477
		$this->setCachedUpdate($this->getInt('cached_update', $data));
478
		$this->setNote($this->get('note', $data));
479
		$this->setContactId($this->get('contact_id', $data));
480
		$this->setContactMeta($this->get('contact_meta', $data));
481
		$this->setJoined($this->getInt('joined', $data));
482
483
		return $this;
484
	}
485
486
487
	/**
488
	 * @return string[]
489
	 */
490
	public function jsonSerialize(): array {
491
		$arr = array_filter(
492
			[
493
				'id'            => $this->getId(),
494
				'circle_id'     => $this->getCircleId(),
495
				'single_id'     => $this->getSingleId(),
496
				'user_id'       => $this->getUserId(),
497
				'user_type'     => $this->getUserType(),
498
				'instance'      => $this->getInstance(),
499
				'level'         => $this->getLevel(),
500
				'status'        => $this->getStatus(),
501
				'cached_name'   => $this->getCachedName(),
502
				'cached_update' => $this->getCachedUpdate(),
503
				'note'          => $this->getNote(),
504
				'contact_id'    => $this->getContactId(),
505
				'contact_meta'  => $this->getContactMeta(),
506
				'joined'        => $this->getJoined()
507
			]
508
		);
509
510
		if ($this->hasCircle()) {
511
			$arr['circle'] = $this->getCircle();
512
		}
513
514
		return $arr;
515
	}
516
517
518
	/**
519
	 * @param array $data
520
	 * @param string $prefix
521
	 *
522
	 * @return INC21QueryRow
523
	 * @throws MemberNotFoundException
524
	 */
525
	public function importFromDatabase(array $data, string $prefix = ''): INC21QueryRow {
526
		if (!array_key_exists($prefix . 'member_id', $data)) {
527
			throw new MemberNotFoundException();
528
		};
529
530
		$this->setId($this->get($prefix . 'member_id', $data));
531
		$this->setCircleId($this->get($prefix . 'circle_id', $data));
532
		$this->setSingleId($this->get($prefix . 'single_id', $data));
533
		$this->setUserId($this->get($prefix . 'user_id', $data));
534
		$this->setUserType($this->getInt($prefix . 'user_type', $data));
535
		$this->setInstance($this->get($prefix . 'instance', $data));
536
		$this->setLevel($this->getInt($prefix . 'level', $data));
537
		$this->setStatus($this->get($prefix . 'status', $data));
538
		$this->setCachedName($this->get($prefix . 'cached_name', $data));
539
		$this->setNote($this->get($prefix . 'note', $data));
540
		$this->setContactId($this->get($prefix . 'contact_id', $data));
541
		$this->setContactMeta($this->get($prefix . 'contact_meta', $data));
542
543
		$cachedUpdate = $this->get($prefix . 'cached_update', $data);
544
		if ($cachedUpdate !== '') {
545
			$this->setCachedUpdate(DateTime::createFromFormat('Y-m-d H:i:s', $cachedUpdate)->getTimestamp());
546
		}
547
548
		$joined = $this->get($prefix . 'joined', $data);
549
		if ($joined !== '') {
550
			$this->setJoined(DateTime::createFromFormat('Y-m-d H:i:s', $joined)->getTimestamp());
551
		}
552
553
		if ($this->getInstance() === '') {
554
			$this->setInstance($this->get('_params.local', $data));
555
		}
556
557
		if ($prefix === '') {
558
			$this->getManager()->importCircleFromDatabase($this, $data);
559
		}
560
561
		return $this;
562
	}
563
564
}
565
566