Completed
Push — master ( 912623...0260a2 )
by Maxence
13s queued 11s
created

BaseCircle::setGroups()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 10
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 OC;
30
use OCA\Circles\AppInfo\Application;
31
use OCP\IL10N;
32
33
class BaseCircle {
34
35
	const CIRCLES_SETTINGS_DEFAULT = [
36
		'password_enforcement' => 'false',
37
		'allow_links'          => 'false',
38
		'allow_links_auto'     => 'false',
39
		'allow_links_files'    => 'false'
40
	];
41
42
	const CIRCLES_PERSONAL = 1;
43
	const CIRCLES_SECRET = 2;
44
	const CIRCLES_CLOSED = 4;
45
	const CIRCLES_PUBLIC = 8;
46
47
	const CIRCLES_ALL = 15;
48
49
	const SHORT_UNIQUE_ID_LENGTH = 14;
50
51
	/** @var int */
52
	private $id;
53
54
	/** @var IL10N */
55
	protected $l10n;
56
57
	/** @var string */
58
	private $uniqueId = '';
59
60
	/** @var string */
61
	private $name;
62
63
	/** @var string */
64
	private $altName = '';
65
66
	/** @var Member */
67
	private $owner;
68
69
	/** @var Member */
70
	private $viewer = null;
71
72
	/** @var Member */
73
	private $viewerGroup;
74
75
	/** @var string */
76
	private $description = '';
77
78
	/** @var array */
79
	private $settings = [];
80
81
	/** @var int */
82
	private $type;
83
84
	/** @var string */
85
	private $contactGroupName = '';
86
87
	/** @var int */
88
	private $contactAddressBook = 0;
89
90
91
	/** @var string */
92
	private $creation;
93
94
	/** @var Member[] */
95
	private $members;
96
97
	/** @var Member[] */
98
	private $groups;
99
100
	/** @var FederatedLink[] */
101
	private $links;
102
103
	public function __construct($type = -1, $name = '') {
104
		$this->l10n = OC::$server->getL10N(Application::APP_NAME);
105
106
		if ($type > -1) {
107
			$this->type = $type;
108
		}
109
		if ($name !== '') {
110
			$this->name = $name;
111
		}
112
	}
113
114
	/**
115
	 * @param integer $id
116
	 *
117
	 * @return BaseCircle
118
	 */
119
	public function setId($id) {
120
		$this->id = $id;
121
122
		return $this;
123
	}
124
125
	/**
126
	 * @return integer
127
	 */
128
	public function getId() {
129
		return $this->id;
130
	}
131
132
133
	/**
134
	 * @param string $uniqueId
135
	 *
136
	 * @return $this
137
	 */
138
	public function setUniqueId($uniqueId) {
139
		$this->uniqueId = (string)$uniqueId;
140
141
		return $this;
142
	}
143
144
	/**
145
	 * @param bool $full
146
	 *
147
	 * @return string
148
	 */
149
	public function getUniqueId($full = false) {
150
		if ($full) {
151
			return $this->uniqueId;
152
		}
153
154
		return substr($this->uniqueId, 0, self::SHORT_UNIQUE_ID_LENGTH);
155
	}
156
157
	public function generateUniqueId() {
158
		$uniqueId = bin2hex(openssl_random_pseudo_bytes(24));
159
		$this->setUniqueId($uniqueId);
160
		$this->setId($this->getUniqueId());
161
	}
162
163
	/**
164
	 * @param string $name
165
	 *
166
	 * @return BaseCircle
167
	 */
168
	public function setName($name) {
169
		$this->name = $name;
170
171
		return $this;
172
	}
173
174
	/**
175
	 * @param bool $real
176
	 *
177
	 * @return string
178
	 */
179
	public function getName(bool $real = false) {
180
		if (!$real && $this->altName !== '') {
181
			return $this->altName;
182
		}
183
184
		return $this->name;
185
	}
186
187
	/**
188
	 * @param string $name
189
	 *
190
	 * @return BaseCircle
191
	 */
192
	public function setAltName($name) {
193
		$this->altName = $name;
194
195
		return $this;
196
	}
197
198
	/**
199
	 * @return string
200
	 */
201
	public function getAltName() {
202
		return $this->altName;
203
	}
204
205
	/**
206
	 * @return Member
207
	 */
208
	public function getOwner() {
209
		return $this->owner;
210
	}
211
212
	/**
213
	 * @param Member $owner
214
	 *
215
	 * @return BaseCircle
216
	 */
217
	public function setOwner($owner) {
218
		$this->owner = $owner;
219
220
		return $this;
221
	}
222
223
224
	/**
225
	 * @return Member
226
	 */
227
	public function getViewer() {
228
		return $this->viewer;
229
	}
230
231
	/**
232
	 * @param Member $user
233
	 *
234
	 * @return BaseCircle
235
	 */
236
	public function setViewer($user) {
237
		$this->viewer = $user;
238
239
		return $this;
240
	}
241
242
243
	public function hasViewer(): bool {
244
		return ($this->viewer !== null);
245
	}
246
247
248
	/**
249
	 * @return Member
250
	 */
251
	public function getGroupViewer() {
252
		return $this->viewerGroup;
253
	}
254
255
	/**
256
	 * @param Member $group
257
	 *
258
	 * @return BaseCircle
259
	 */
260
	public function setGroupViewer($group) {
261
		$this->viewerGroup = $group;
262
263
		return $this;
264
	}
265
266
	/**
267
	 * @return Member
268
	 */
269
	public function getHigherViewer() {
270
		if ($this->getGroupViewer() === null) {
271
			return $this->getViewer();
272
		}
273
274
		if ($this->getViewer() === null) {
275
			return $this->getGroupViewer();
276
		}
277
278
		if ($this->getGroupViewer()
279
				 ->getLevel() > $this->getViewer()
280
									 ->getLevel()
281
		) {
282
			return $this->getGroupViewer();
283
		}
284
285
		return $this->getViewer();
286
	}
287
288
289
	/**
290
	 * @param string $description
291
	 *
292
	 * @return BaseCircle
293
	 */
294
	public function setDescription($description) {
295
		$this->description = $description;
296
297
		return $this;
298
	}
299
300
	/**
301
	 * @return string
302
	 */
303
	public function getDescription() {
304
		return $this->description;
305
	}
306
307
308
	/**
309
	 * @param int $contactAddressBook
310
	 *
311
	 * @return BaseCircle
312
	 */
313
	public function setContactAddressBook(int $contactAddressBook) {
314
		$this->contactAddressBook = $contactAddressBook;
315
316
		return $this;
317
	}
318
319
	/**
320
	 * @return int
321
	 */
322
	public function getContactAddressBook() {
323
		return $this->contactAddressBook;
324
	}
325
326
327
	/**
328
	 * @param string $contactGroupName
329
	 *
330
	 * @return BaseCircle
331
	 */
332
	public function setContactGroupName($contactGroupName) {
333
		$this->contactGroupName = $contactGroupName;
334
335
		return $this;
336
	}
337
338
	/**
339
	 * @return string
340
	 */
341
	public function getContactGroupName() {
342
		return $this->contactGroupName;
343
	}
344
345
346
	/**
347
	 * @param string|array $settings
348
	 *
349
	 * @return $this
350
	 */
351
	public function setSettings($settings) {
352
		if (is_array($settings)) {
353
			$this->settings = $settings;
354
		} else if (is_string($settings)) {
355
			$this->settings = (array)json_decode($settings, true);
356
		}
357
358
		return $this;
359
	}
360
361
	/**
362
	 * @param bool $json
363
	 *
364
	 * @return array|string
365
	 */
366
	public function getSettings($json = false) {
367
368
		if ($json) {
369
			return json_encode($this->settings);
370
		}
371
372
		$settings = $this->settings;
373
		if ($settings === null) {
374
			$settings = [];
375
		}
376
377
		$ak = array_keys(self::CIRCLES_SETTINGS_DEFAULT);
378
		foreach ($ak as $k) {
379
			if (!key_exists($k, $settings)) {
380
				$settings[$k] = self::CIRCLES_SETTINGS_DEFAULT[$k];
381
			}
382
		}
383
384
		return $settings;
385
	}
386
387
388
	/**
389
	 * @param string $k
390
	 * @param mixed $v
391
	 */
392
	public function setSetting($k, $v) {
393
		switch ($k) {
394
			case 'circle_name':
395
				$this->setName($v);
396
				break;
397
398
			case 'circle_alt_name':
399
				$this->setAltName($v);
400
				break;
401
402
			case 'circle_desc':
403
				$this->setDescription($v);
404
				break;
405
406
			default:
407
				$this->settings[$k] = $v;
408
				break;
409
		}
410
	}
411
412
413
	/**
414
	 * @param string $k
415
	 *
416
	 * @return string|null
417
	 */
418
	public function getSetting($k) {
419
		if (key_exists($k, $this->settings)) {
420
			return $this->settings[$k];
421
		}
422
		if (key_exists($k, (array)self::CIRCLES_SETTINGS_DEFAULT)) {
423
			return self::CIRCLES_SETTINGS_DEFAULT[$k];
424
		}
425
426
		return null;
427
	}
428
429
	/**
430
	 *
431
	 * @param string $type
432
	 *
433
	 * @return \OCA\Circles\Model\BaseCircle
434
	 */
435
	public function setType($type) {
436
		$this->type = self::typeInt($type);
437
438
		return $this;
439
	}
440
441
	/**
442
	 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be integer?

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...
443
	 */
444
	public function getType() {
445
		return $this->type;
446
	}
447
448
	/**
449
	 * @param string $creation
450
	 *
451
	 * @return \OCA\Circles\Model\BaseCircle
452
	 */
453
	public function setCreation($creation) {
454
		$this->creation = $creation;
455
456
		return $this;
457
	}
458
459
	/**
460
	 * @return string
461
	 */
462
	public function getCreation() {
463
		return $this->creation;
464
	}
465
466
	/**
467
	 * @param array $members
468
	 *
469
	 * @return BaseCircle
470
	 */
471
	public function setMembers($members) {
472
		$this->members = $members;
473
474
		return $this;
475
	}
476
477
	/**
478
	 * @return Member[]
479
	 */
480
	public function getMembers() {
481
		return $this->members;
482
	}
483
484
	/**
485
	 * @param array $groups
486
	 *
487
	 * @return BaseCircle
488
	 */
489
	public function setGroups($groups) {
490
		$this->groups = $groups;
491
492
		return $this;
493
	}
494
495
	/**
496
	 * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use Member[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
497
	 */
498
	public function getGroups() {
499
		return $this->groups;
500
	}
501
502
	/**
503
	 * @param array $links
504
	 *
505
	 * @return BaseCircle
506
	 */
507
	public function setLinks($links) {
508
		$this->links = $links;
509
510
		return $this;
511
	}
512
513
	/**
514
	 * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use FederatedLink[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
515
	 */
516
	public function getLinks() {
517
		return $this->links;
518
	}
519
520
521
//	public function getRemote() {
522
//		return $this->remote;
523
//	}
524
//
525
//	public function addRemote($link) {
526
//		array_push($this->remote, $link);
527
//	}
528
//
529
//	public function getRemoteFromToken($token) {
530
//		foreach ($this->links AS $link) {
531
//			if ($link->getToken() === $token) {
532
//				return $link;
533
//			}
534
//		}
535
//
536
//		return null;
537
//	}
538
//
539
//	public function getRemoteFromAddressAndId($address, $id) {
540
//		foreach ($this->links AS $link) {
541
//			if ($link->getAddress() === $address && $link->getUniqueId() === $id) {
542
//				return $link;
543
//			}
544
//		}
545
//
546
//		return null;
547
//	}
548
549
	/**
550
	 * @param integer|string $type
551
	 *
552
	 * @return integer
553
	 */
554
	public static function typeInt($type) {
555
556
		if (is_numeric($type)) {
557
			return (int)$type;
558
		}
559
560 View Code Duplication
		switch ($type) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
561
			case 'Personal':
562
				return self::CIRCLES_PERSONAL;
563
			case 'Closed':
564
				return self::CIRCLES_CLOSED;
565
			case 'Secret':
566
				return self::CIRCLES_SECRET;
567
			case 'Public':
568
				return self::CIRCLES_PUBLIC;
569
		}
570
571
		return 0;
572
	}
573
}
574