Completed
Pull Request — master (#546)
by Maxence
03:01
created

Circle::getId()   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\Exceptions\InvalidItemException;
36
use daita\MySmallPhpTools\Model\Nextcloud\nc21\INC21Convert;
37
use daita\MySmallPhpTools\Traits\Nextcloud\nc21\TNC21Convert;
38
use daita\MySmallPhpTools\Traits\TArrayTools;
39
use DateTime;
40
use JsonSerializable;
41
42
43
/**
44
 * Class Circle
45
 *
46
 * @package OCA\Circles\Model
47
 */
48
class Circle extends ManagedModel implements INC21Convert, INC21QueryRow, JsonSerializable {
49
50
51
	use TArrayTools;
52
	use TNC21Convert;
53
54
55
	const TYPE_SINGLE = 1;        // Single member
56
	const TYPE_PERSONAL = 2;      // Personal circle, only owner can see it
57
	const TYPE_VISIBLE = 4;       // Visible to everyone, if not visible, people have to know its name to be able to join it
58
	const TYPE_LOCKED = 8;        // Invite only
59
	const TYPE_FAST_INVITE = 16;  // Fast invite, no need confirmation
60
	const TYPE_REQUEST = 32;      // Request need to be confirmed by moderator
61
	const TYPE_PROTECTED = 64;    // Password protected to join/request
62
	const TYPE_HIDDEN = 128;      // Fully hidden, only backend Circles
63
	const TYPE_FEDERATED = 256;   // Federated
64
65
	static $DEF = [
66
		1   => '*S|Single',
67
		2   => 'PU|Personal Use',
68
		4   => 'V|Visible',
69
		8   => 'IO|Invite Only',
70
		16  => 'FI|Fast Invite',
71
		32  => 'JR|Join Request',
72
		64  => 'PP|Password Protected',
73
		128 => '*H|Hidden',
74
		256 => 'F|Federated'
75
	];
76
77
78
	/** @var string */
79
	private $id = '';
80
81
	/** @var int */
82
	private $type = 0;
83
84
	/** @var string */
85
	private $name = '';
86
87
	/** @var string */
88
	private $altName = '';
89
90
	/** @var Member */
91
	private $owner;
92
93
	/** @var array */
94
	private $members = [];
95
96
	/** @var array */
97
	private $settings = [];
98
99
	/** @var string */
100
	private $description = '';
101
102
	/** @var string */
103
	private $contactAddressBook = '';
104
105
	/** @var string */
106
	private $contactGroupName = '';
107
108
	/** @var bool */
109
	private $hidden = false;
110
111
	/** @var int */
112
	private $creation = 0;
113
114
115
	public function __construct() {
116
	}
117
118
	/**
119
	 * @param string $id
120
	 *
121
	 * @return self
122
	 */
123
	public function setId(string $id): self {
124
		$this->id = $id;
125
126
		return $this;
127
	}
128
129
	/**
130
	 * @return string
131
	 */
132
	public function getId(): string {
133
		return $this->id;
134
	}
135
136
137
	/**
138
	 * @param int $type
139
	 *
140
	 * @return self
141
	 */
142
	public function setType(int $type): self {
143
		$this->type = $type;
144
145
		$this->hidden = false;
146
		foreach (array_keys(self::$DEF) as $def) {
147
			if ($this->isType($def) && substr(self::$DEF[$def], 0, 1) === '*') {
148
				$this->hidden = true;
149
				break;
150
			}
151
		}
152
153
		return $this;
154
	}
155
156
	/**
157
	 * @return int
158
	 */
159
	public function getType(): int {
160
		return $this->type;
161
	}
162
163
	/**
164
	 * @param int $type
165
	 *
166
	 * @return bool
167
	 */
168
	public function isType(int $type): bool {
169
		return (($this->getType() & $type) !== 0);
170
	}
171
172
173
	/**
174
	 * @param string $name
175
	 *
176
	 * @return self
177
	 */
178
	public function setName(string $name): self {
179
		$this->name = $name;
180
181
		return $this;
182
	}
183
184
	/**
185
	 * @return string
186
	 */
187
	public function getName(): string {
188
		return $this->name;
189
	}
190
191
192
	/**
193
	 * @param string $altName
194
	 *
195
	 * @return self
196
	 */
197
	public function setAltName(string $altName): self {
198
		$this->altName = $altName;
199
200
		return $this;
201
	}
202
203
	/**
204
	 * @return string
205
	 */
206
	public function getAltName(): string {
207
		return $this->altName;
208
	}
209
210
211
	/**
212
	 * @param Member $owner
213
	 */
214
	public function setOwner(Member $owner): void {
215
		$this->owner = $owner;
216
	}
217
218
	/**
219
	 * @return Member
220
	 */
221
	public function getOwner(): Member {
222
		return $this->owner;
223
	}
224
225
	/**
226
	 * @return bool
227
	 */
228
	public function hasOwner(): bool {
229
		return ($this->owner !== null);
230
	}
231
232
233
	/**
234
	 * @param array $members
235
	 *
236
	 * @return self
237
	 */
238
	public function setMembers(array $members): self {
239
		$this->members = $members;
240
241
		return $this;
242
	}
243
244
	/**
245
	 * @return array
246
	 */
247
	public function getMembers(): array {
248
		$this->getManager()->getMembers($this);
249
250
		return $this->members;
251
	}
252
253
254
	/**
255
	 * @param array $settings
256
	 *
257
	 * @return self
258
	 */
259
	public function setSettings(array $settings): self {
260
		$this->settings = $settings;
261
262
		return $this;
263
	}
264
265
	/**
266
	 * @return array
267
	 */
268
	public function getSettings(): array {
269
		return $this->settings;
270
	}
271
272
273
	/**
274
	 * @param string $description
275
	 *
276
	 * @return self
277
	 */
278
	public function setDescription(string $description): self {
279
		$this->description = $description;
280
281
		return $this;
282
	}
283
284
	/**
285
	 * @return string
286
	 */
287
	public function getDescription(): string {
288
		return $this->description;
289
	}
290
291
292
	/**
293
	 * @param string $contactAddressBook
294
	 *
295
	 * @return self
296
	 */
297
	public function setContactAddressBook(string $contactAddressBook): self {
298
		$this->contactAddressBook = $contactAddressBook;
299
300
		return $this;
301
	}
302
303
	/**
304
	 * @return string
305
	 */
306
	public function getContactAddressBook(): string {
307
		return $this->contactAddressBook;
308
	}
309
310
311
	/**
312
	 * @param string $contactGroupName
313
	 *
314
	 * @return self
315
	 */
316
	public function setContactGroupName(string $contactGroupName): self {
317
		$this->contactGroupName = $contactGroupName;
318
319
		return $this;
320
	}
321
322
	/**
323
	 * @return string
324
	 */
325
	public function getContactGroupName(): string {
326
		return $this->contactGroupName;
327
	}
328
329
330
	/**
331
	 * @param bool $hidden
332
	 *
333
	 * @return Circle
334
	 */
335
	public function setHidden(bool $hidden): self {
336
		$this->hidden = $hidden;
337
338
		return $this;
339
	}
340
341
	/**
342
	 * @return bool
343
	 */
344
	public function isHidden(): bool {
345
		return $this->hidden;
346
	}
347
348
349
	/**
350
	 * @param int $creation
351
	 *
352
	 * @return self
353
	 */
354
	public function setCreation(int $creation): self {
355
		$this->creation = $creation;
356
357
		return $this;
358
	}
359
360
	/**
361
	 * @return int
362
	 */
363
	public function getCreation(): int {
364
		return $this->creation;
365
	}
366
367
368
	/**
369
	 * @param array $data
370
	 *
371
	 * @return $this
372
	 */
373
	public function import(array $data): INC21Convert {
374
		$this->setId($this->get('id', $data))
375
			 ->setName($this->get('name', $data))
376
			 ->setAltName($this->get('alt_name', $data))
377
			 ->setType($this->getInt('type', $data))
378
			 ->setSettings($this->getArray('settings', $data))
379
//			 ->setContactAddressBook($this->get('contact_addressbook', $data))
380
//			 ->setContactGroupName($this->get('contact_groupname', $data))
381
			 ->setDescription($this->get('description', $data))
382
			 ->setCreation($this->getInt('creation', $data));
383
384
		try {
385
			/** @var Member $owner */
386
			$owner = $this->convert($this->getArray('owner', $data), Member::class);
387
			$this->setOwner($owner);
388
		} catch (InvalidItemException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
389
		}
390
391
		return $this;
392
	}
393
394
395
	/**
396
	 * @return array
397
	 */
398
	public function jsonSerialize(): array {
399
		$arr = [
400
			'id'          => $this->getId(),
401
			'name'        => $this->getName(),
402
			'alt_name'    => $this->getAltName(),
403
			'type'        => $this->getType(),
404
			'description' => $this->getDescription(),
405
			'settings'    => $this->getSettings(),
406
			'hidden'      => $this->isHidden(),
407
			'creation'    => $this->getCreation()
408
		];
409
410
		if ($this->hasOwner()) {
411
			$arr['owner'] = $this->getOwner();
412
		}
413
414
		return $arr;
415
	}
416
417
418
	/**
419
	 * @param array $data
420
	 *
421
	 * @return INC21QueryRow
422
	 */
423
	public function importFromDatabase(array $data): INC21QueryRow {
424
		$this->setId($this->get('unique_id', $data))
425
			 ->setName($this->get('name', $data))
426
			 ->setAltName($this->get('alt_name', $data))
427
			 ->setType($this->getInt('type', $data))
428
			 ->setSettings($this->getArray('settings', $data))
429
			 ->setContactAddressBook($this->get('contact_addressbook', $data))
430
			 ->setContactGroupName($this->get('contact_groupname', $data))
431
			 ->setDescription($this->get('description', $data));
432
433
		$creation = $this->get('creation', $data);
434
		$this->setCreation(DateTime::createFromFormat('Y-m-d H:i:s', $creation)->getTimestamp());
435
436
		$this->getManager()->importOwnerFromDatabase($this, $data);
437
438
		return $this;
439
	}
440
441
}
442