Completed
Pull Request — master (#551)
by Maxence
01:54
created

Member::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
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\IMember;
41
42
43
/**
44
 * Class Member
45
 *
46
 * @package OCA\Circles\Model
47
 */
48
class Member extends ManagedModel implements IMember, 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 = 14;
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 $userId;
91
92
	/** @var int */
93
	private $userType;
94
95
	/** @var string */
96
	private $instance;
97
98
	/** @var int */
99
	private $level = 0;
100
101
	/** @var string */
102
	private $status = 'Unknown';
103
104
	/** @var string */
105
	private $note = '';
106
107
	/** @var string */
108
	private $cachedName = '';
109
110
	/** @var int */
111
	private $cachedUpdate = 0;
112
113
	/** @var string */
114
	private $contactId = '';
115
116
	/** @var string */
117
	private $contactMeta = '';
118
119
	/** @var int */
120
	private $joined = 0;
121
122
123
	/**
124
	 * Member constructor.
125
	 *
126
	 * @param string $userId
127
	 * @param int $type
128
	 * @param string $instance
129
	 */
130
	public function __construct(string $userId = '', int $type = self::TYPE_USER, $instance = '') {
131
		$this->userId = $userId;
132
		$this->userType = $type;
133
		$this->instance = $instance;
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
	 * @param string $userId
177
	 *
178
	 * @return Member
179
	 */
180
	public function setUserId(string $userId): self {
181
		$this->userId = $userId;
182
183
		return $this;
184
	}
185
186
	/**
187
	 * @return string
188
	 */
189
	public function getUserId(): string {
190
		return $this->userId;
191
	}
192
193
194
	/**
195
	 * @param int $userType
196
	 *
197
	 * @return Member
198
	 */
199
	public function setUserType(int $userType): self {
200
		$this->userType = $userType;
201
202
		return $this;
203
	}
204
205
	/**
206
	 * @return int
207
	 */
208
	public function getUserType(): int {
209
		return $this->userType;
210
	}
211
212
213
	/**
214
	 * @param string $instance
215
	 *
216
	 * @return Member
217
	 */
218
	public function setInstance(string $instance): self {
219
		$this->instance = $instance;
220
221
		return $this;
222
	}
223
224
	/**
225
	 * @return string
226
	 */
227
	public function getInstance(): string {
228
		return $this->instance;
229
	}
230
231
232
	/**
233
	 * @param int $level
234
	 *
235
	 * @return Member
236
	 */
237
	public function setLevel(int $level): self {
238
		$this->level = $level;
239
240
		return $this;
241
	}
242
243
	/**
244
	 * @return int
245
	 */
246
	public function getLevel(): int {
247
		return $this->level;
248
	}
249
250
251
	/**
252
	 * @param string $status
253
	 *
254
	 * @return Member
255
	 */
256
	public function setStatus(string $status): self {
257
		$this->status = $status;
258
259
		return $this;
260
	}
261
262
	/**
263
	 * @return string
264
	 */
265
	public function getStatus(): string {
266
		return $this->status;
267
	}
268
269
270
	/**
271
	 * @param string $note
272
	 *
273
	 * @return Member
274
	 */
275
	public function setNote(string $note): self {
276
		$this->note = $note;
277
278
		return $this;
279
	}
280
281
	/**
282
	 * @return string
283
	 */
284
	public function getNote(): string {
285
		return $this->note;
286
	}
287
288
289
	/**
290
	 * @param string $cachedName
291
	 *
292
	 * @return Member
293
	 */
294
	public function setCachedName(string $cachedName): self {
295
		$this->cachedName = $cachedName;
296
297
		return $this;
298
	}
299
300
301
	/**
302
	 * @param int $cachedUpdate
303
	 *
304
	 * @return Member
305
	 */
306
	public function setCachedUpdate(int $cachedUpdate): self {
307
		$this->cachedUpdate = $cachedUpdate;
308
309
		return $this;
310
	}
311
312
	/**
313
	 * @return int
314
	 */
315
	public function getCachedUpdate(): int {
316
		return $this->cachedUpdate;
317
	}
318
319
320
	/**
321
	 * @return string
322
	 */
323
	public function getCachedName(): string {
324
		return $this->cachedName;
325
	}
326
327
328
	/**
329
	 * @param string $contactId
330
	 *
331
	 * @return Member
332
	 */
333
	public function setContactId(string $contactId): self {
334
		$this->contactId = $contactId;
335
336
		return $this;
337
	}
338
339
	/**
340
	 * @return string
341
	 */
342
	public function getContactId(): string {
343
		return $this->contactId;
344
	}
345
346
347
	/**
348
	 * @param string $contactMeta
349
	 *
350
	 * @return Member
351
	 */
352
	public function setContactMeta(string $contactMeta): self {
353
		$this->contactMeta = $contactMeta;
354
355
		return $this;
356
	}
357
358
	/**
359
	 * @return string
360
	 */
361
	public function getContactMeta(): string {
362
		return $this->contactMeta;
363
	}
364
365
366
	/**
367
	 * @param int $joined
368
	 *
369
	 * @return Member
370
	 */
371
	public function setJoined(int $joined): self {
372
		$this->joined = $joined;
373
374
		return $this;
375
	}
376
377
	/**
378
	 * @return int
379
	 */
380
	public function getJoined(): int {
381
		return $this->joined;
382
	}
383
384
385
	/**
386
	 * @return bool
387
	 */
388
	public function isMember(): bool {
389
		return ($this->level > 0);
390
	}
391
392
393
	/**
394
	 * @param Member $member
395
	 *
396
	 * @return bool
397
	 */
398
	public function compareWith(Member $member): bool {
399
		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->getInstance());.
Loading history...
400
			|| $this->getCircleId() !== $member->getCircleId()
401
			|| $this->getUserId() !== $member->getUserId()
402
			|| $this->getUserType() <> $member->getUserType()
403
			|| $this->getLevel() <> $member->getLevel()
404
			|| $this->getStatus() !== $member->getStatus()
405
			|| $this->getInstance() !== $member->getInstance()) {
406
			return false;
407
		}
408
409
		return true;
410
	}
411
412
413
	/**
414
	 * @param IMember $member
415
	 *
416
	 * @return self
417
	 */
418
	public function importFromIMember(IMember $member): IMember {
419
		$this->getManager()->importFromIMember($this, $member);
420
421
		return $this;
422
	}
423
424
425
	/**
426
	 * @param array $data
427
	 *
428
	 * @return $this
429
	 */
430
	public function import(array $data): INC21Convert {
431
		$this->setId($this->get('id', $data));
432
		$this->setCircleId($this->get('circle_id', $data));
433
		$this->setUserId($this->get('user_id', $data));
434
		$this->setUserType($this->getInt('user_type', $data));
435
		$this->setInstance($this->get('instance', $data));
436
		$this->setLevel($this->getInt('level', $data));
437
		$this->setStatus($this->get('status', $data));
438
		$this->setCachedName($this->get('cached_name', $data));
439
		$this->setCachedUpdate($this->getInt('cached_update', $data));
440
		$this->setNote($this->get('note', $data));
441
		$this->setContactId($this->get('contact_id', $data));
442
		$this->setContactMeta($this->get('contact_meta', $data));
443
		$this->setJoined($this->getInt('joined', $data));
444
445
		return $this;
446
	}
447
448
449
	/**
450
	 * @return string[]
451
	 */
452 View Code Duplication
	public function jsonSerialize(): array {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
453
		return array_filter(
454
			[
455
				'id'            => $this->getId(),
456
				'circle_id'     => $this->getCircleId(),
457
				'user_id'       => $this->getUserId(),
458
				'user_type'     => $this->getUserType(),
459
				'instance'      => $this->getInstance(),
460
				'level'         => $this->getLevel(),
461
				'status'        => $this->getStatus(),
462
				'cached_name'   => $this->getCachedName(),
463
				'cached_update' => $this->getCachedUpdate(),
464
				'note'          => $this->getNote(),
465
				'contact_id'    => $this->getContactId(),
466
				'contact_meta'  => $this->getContactMeta(),
467
				'joined'        => $this->getJoined()
468
			]
469
		);
470
	}
471
472
473
	/**
474
	 * @param array $data
475
	 * @param string $prefix
476
	 *
477
	 * @return INC21QueryRow
478
	 * @throws MemberNotFoundException
479
	 */
480
	public function importFromDatabase(array $data, string $prefix = ''): INC21QueryRow {
481
		if (!array_key_exists($prefix . 'member_id', $data)) {
482
			throw new MemberNotFoundException();
483
		};
484
485
		$this->setId($this->get($prefix . 'member_id', $data));
486
		$this->setCircleId($this->get($prefix . 'circle_id', $data));
487
		$this->setUserId($this->get($prefix . 'user_id', $data));
488
		$this->setUserType($this->getInt($prefix . 'user_type', $data));
489
		$this->setInstance($this->get($prefix . 'instance', $data));
490
		$this->setLevel($this->getInt($prefix . 'level', $data));
491
		$this->setStatus($this->get($prefix . 'status', $data));
492
		$this->setCachedName($this->get($prefix . 'cached_name', $data));
493
		$this->setNote($this->get($prefix . 'note', $data));
494
		$this->setContactId($this->get($prefix . 'contact_id', $data));
495
		$this->setContactMeta($this->get($prefix . 'contact_meta', $data));
496
497
		$cachedUpdate = $this->get($prefix . 'cached_update', $data);
498
		if ($cachedUpdate !== '') {
499
			$this->setCachedUpdate(DateTime::createFromFormat('Y-m-d H:i:s', $cachedUpdate)->getTimestamp());
500
		}
501
502
		$joined = $this->get($prefix . 'joined', $data);
503
		if ($joined !== '') {
504
			$this->setJoined(DateTime::createFromFormat('Y-m-d H:i:s', $joined)->getTimestamp());
505
		}
506
507
		if ($this->getInstance() === '') {
508
			$this->setInstance($this->get('_params.local', $data));
509
		}
510
511
		return $this;
512
	}
513
514
}
515
516