Completed
Pull Request — master (#479)
by Maxence
02:08
created

BaseCircle::hasViewer()   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
 * 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
		'password_single'      => '',
38
		'allow_links'          => 'false',
39
		'allow_links_auto'     => 'false',
40
		'allow_links_files'    => 'false'
41
	];
42
43
	const CIRCLES_PERSONAL = 1;
44
	const CIRCLES_SECRET = 2;
45
	const CIRCLES_CLOSED = 4;
46
	const CIRCLES_PUBLIC = 8;
47
48
	const CIRCLES_ALL = 15;
49
50
	const SHORT_UNIQUE_ID_LENGTH = 14;
51
52
	/** @var int */
53
	private $id;
54
55
	/** @var IL10N */
56
	protected $l10n;
57
58
	/** @var string */
59
	private $uniqueId = '';
60
61
	/** @var string */
62
	private $name;
63
64
	/** @var string */
65
	private $altName = '';
66
67
	/** @var Member */
68
	private $owner;
69
70
	/** @var Member */
71
	private $viewer = null;
72
73
	/** @var Member */
74
	private $viewerGroup;
75
76
	/** @var string */
77
	private $description = '';
78
79
	/** @var array */
80
	private $settings = [];
81
82
	/** @var string */
83
	private $passwordSingle = '';
84
85
	/** @var int */
86
	private $type;
87
88
	/** @var string */
89
	private $contactGroupName = '';
90
91
	/** @var int */
92
	private $contactAddressBook = 0;
93
94
95
	/** @var string */
96
	private $creation;
97
98
	/** @var Member[] */
99
	private $members;
100
101
	/** @var Member[] */
102
	private $groups;
103
104
	/** @var FederatedLink[] */
105
	private $links;
106
107
	public function __construct($type = -1, $name = '') {
108
		$this->l10n = OC::$server->getL10N(Application::APP_NAME);
109
110
		if ($type > -1) {
111
			$this->type = $type;
112
		}
113
		if ($name !== '') {
114
			$this->name = $name;
115
		}
116
	}
117
118
	/**
119
	 * @param integer $id
120
	 *
121
	 * @return BaseCircle
122
	 */
123
	public function setId($id) {
124
		$this->id = $id;
125
126
		return $this;
127
	}
128
129
	/**
130
	 * @return integer
131
	 */
132
	public function getId() {
133
		return $this->id;
134
	}
135
136
137
	/**
138
	 * @param string $uniqueId
139
	 *
140
	 * @return $this
141
	 */
142
	public function setUniqueId($uniqueId) {
143
		$this->uniqueId = (string)$uniqueId;
144
145
		return $this;
146
	}
147
148
	/**
149
	 * @param bool $full
150
	 *
151
	 * @return string
152
	 */
153
	public function getUniqueId($full = false) {
154
		if ($full) {
155
			return $this->uniqueId;
156
		}
157
158
		return substr($this->uniqueId, 0, self::SHORT_UNIQUE_ID_LENGTH);
159
	}
160
161
	public function generateUniqueId() {
162
		$uniqueId = bin2hex(openssl_random_pseudo_bytes(24));
163
		$this->setUniqueId($uniqueId);
164
		$this->setId($this->getUniqueId());
165
	}
166
167
	/**
168
	 * @param string $name
169
	 *
170
	 * @return BaseCircle
171
	 */
172
	public function setName($name) {
173
		$this->name = $name;
174
175
		return $this;
176
	}
177
178
	/**
179
	 * @param bool $real
180
	 *
181
	 * @return string
182
	 */
183
	public function getName(bool $real = false) {
184
		if (!$real && $this->altName !== '') {
185
			return $this->altName;
186
		}
187
188
		return $this->name;
189
	}
190
191
	/**
192
	 * @param string $name
193
	 *
194
	 * @return BaseCircle
195
	 */
196
	public function setAltName($name) {
197
		$this->altName = $name;
198
199
		return $this;
200
	}
201
202
	/**
203
	 * @return string
204
	 */
205
	public function getAltName() {
206
		return $this->altName;
207
	}
208
209
	/**
210
	 * @return Member
211
	 */
212
	public function getOwner() {
213
		return $this->owner;
214
	}
215
216
	/**
217
	 * @param Member $owner
218
	 *
219
	 * @return BaseCircle
220
	 */
221
	public function setOwner($owner) {
222
		$this->owner = $owner;
223
224
		return $this;
225
	}
226
227
228
	/**
229
	 * @return Member
230
	 */
231
	public function getViewer() {
232
		return $this->viewer;
233
	}
234
235
	/**
236
	 * @param Member $user
237
	 *
238
	 * @return BaseCircle
239
	 */
240
	public function setViewer($user) {
241
		$this->viewer = $user;
242
243
		return $this;
244
	}
245
246
247
	public function hasViewer(): bool {
248
		return ($this->viewer !== null);
249
	}
250
251
252
	/**
253
	 * @return Member
254
	 */
255
	public function getGroupViewer() {
256
		return $this->viewerGroup;
257
	}
258
259
	/**
260
	 * @param Member $group
261
	 *
262
	 * @return BaseCircle
263
	 */
264
	public function setGroupViewer($group) {
265
		$this->viewerGroup = $group;
266
267
		return $this;
268
	}
269
270
	/**
271
	 * @return Member
272
	 */
273
	public function getHigherViewer() {
274
		if ($this->getGroupViewer() === null) {
275
			return $this->getViewer();
276
		}
277
278
		if ($this->getViewer() === null) {
279
			return $this->getGroupViewer();
280
		}
281
282
		if ($this->getGroupViewer()
283
				 ->getLevel() > $this->getViewer()
284
									 ->getLevel()
285
		) {
286
			return $this->getGroupViewer();
287
		}
288
289
		return $this->getViewer();
290
	}
291
292
293
	/**
294
	 * @param string $description
295
	 *
296
	 * @return BaseCircle
297
	 */
298
	public function setDescription($description) {
299
		$this->description = $description;
300
301
		return $this;
302
	}
303
304
	/**
305
	 * @return string
306
	 */
307
	public function getDescription() {
308
		return $this->description;
309
	}
310
311
312
	/**
313
	 * @param int $contactAddressBook
314
	 *
315
	 * @return BaseCircle
316
	 */
317
	public function setContactAddressBook(int $contactAddressBook) {
318
		$this->contactAddressBook = $contactAddressBook;
319
320
		return $this;
321
	}
322
323
	/**
324
	 * @return int
325
	 */
326
	public function getContactAddressBook() {
327
		return $this->contactAddressBook;
328
	}
329
330
331
	/**
332
	 * @param string $contactGroupName
333
	 *
334
	 * @return BaseCircle
335
	 */
336
	public function setContactGroupName($contactGroupName) {
337
		$this->contactGroupName = $contactGroupName;
338
339
		return $this;
340
	}
341
342
	/**
343
	 * @return string
344
	 */
345
	public function getContactGroupName() {
346
		return $this->contactGroupName;
347
	}
348
349
350
	/**
351
	 * @param string|array $settings
352
	 * @param bool $all
353
	 *
354
	 * @return $this
355
	 */
356
	public function setSettings($settings, bool $all = false) {
357
		if (is_array($settings)) {
358
			$this->settings = $settings;
359
		} else if (is_string($settings)) {
360
			$this->settings = (array)json_decode($settings, true);
361
		}
362
363
		if (array_key_exists('password_single', $this->settings)) {
364
			$this->setPasswordSingle($this->settings['password_single']);
365
			if (!$all) {
366
				$this->settings['password_single'] = '';
367
			}
368
		}
369
370
		return $this;
371
	}
372
373
	/**
374
	 * @param bool $json
375
	 *
376
	 * @return array|string
377
	 */
378
	public function getSettings($json = false) {
379
380
		if ($json) {
381
			return json_encode($this->settings);
382
		}
383
384
		$settings = $this->settings;
385
		if ($settings === null) {
386
			$settings = [];
387
		}
388
389
		$ak = array_keys(self::CIRCLES_SETTINGS_DEFAULT);
390
		foreach ($ak as $k) {
391
			if (!key_exists($k, $settings)) {
392
				$settings[$k] = self::CIRCLES_SETTINGS_DEFAULT[$k];
393
			}
394
		}
395
396
		return $settings;
397
	}
398
399
400
	/**
401
	 * @param string $k
402
	 * @param mixed $v
403
	 */
404
	public function setSetting($k, $v) {
405
		switch ($k) {
406
			case 'circle_name':
407
				$this->setName($v);
408
				break;
409
410
			case 'circle_alt_name':
411
				$this->setAltName($v);
412
				break;
413
414
			case 'circle_desc':
415
				$this->setDescription($v);
416
				break;
417
418
			default:
419
				$this->settings[$k] = $v;
420
				break;
421
		}
422
	}
423
424
425
	/**
426
	 * @param string $k
427
	 *
428
	 * @return string|null
429
	 */
430
	public function getSetting($k) {
431
		if (key_exists($k, $this->settings)) {
432
			return $this->settings[$k];
433
		}
434
		if (key_exists($k, (array)self::CIRCLES_SETTINGS_DEFAULT)) {
435
			return self::CIRCLES_SETTINGS_DEFAULT[$k];
436
		}
437
438
		return null;
439
	}
440
441
442
	/**
443
	 * @return string
444
	 */
445
	public function getPasswordSingle(): string {
446
		return $this->passwordSingle;
447
	}
448
449
	/**
450
	 * @param string $passwordSingle
451
	 */
452
	public function setPasswordSingle(string $passwordSingle): void {
453
		$this->passwordSingle = $passwordSingle;
454
	}
455
456
457
	/**
458
	 *
459
	 * @param string $type
460
	 *
461
	 * @return \OCA\Circles\Model\BaseCircle
462
	 */
463
	public function setType($type) {
464
		$this->type = self::typeInt($type);
465
466
		return $this;
467
	}
468
469
	/**
470
	 * @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...
471
	 */
472
	public function getType() {
473
		return $this->type;
474
	}
475
476
	/**
477
	 * @param string $creation
478
	 *
479
	 * @return \OCA\Circles\Model\BaseCircle
480
	 */
481
	public function setCreation($creation) {
482
		$this->creation = $creation;
483
484
		return $this;
485
	}
486
487
	/**
488
	 * @return string
489
	 */
490
	public function getCreation() {
491
		return $this->creation;
492
	}
493
494
	/**
495
	 * @param array $members
496
	 *
497
	 * @return BaseCircle
498
	 */
499
	public function setMembers($members) {
500
		$this->members = $members;
501
502
		return $this;
503
	}
504
505
	/**
506
	 * @return Member[]
507
	 */
508
	public function getMembers() {
509
		return $this->members;
510
	}
511
512
	/**
513
	 * @param array $groups
514
	 *
515
	 * @return BaseCircle
516
	 */
517
	public function setGroups($groups) {
518
		$this->groups = $groups;
519
520
		return $this;
521
	}
522
523
	/**
524
	 * @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...
525
	 */
526
	public function getGroups() {
527
		return $this->groups;
528
	}
529
530
	/**
531
	 * @param array $links
532
	 *
533
	 * @return BaseCircle
534
	 */
535
	public function setLinks($links) {
536
		$this->links = $links;
537
538
		return $this;
539
	}
540
541
	/**
542
	 * @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...
543
	 */
544
	public function getLinks() {
545
		return $this->links;
546
	}
547
548
549
//	public function getRemote() {
550
//		return $this->remote;
551
//	}
552
//
553
//	public function addRemote($link) {
554
//		array_push($this->remote, $link);
555
//	}
556
//
557
//	public function getRemoteFromToken($token) {
558
//		foreach ($this->links AS $link) {
559
//			if ($link->getToken() === $token) {
560
//				return $link;
561
//			}
562
//		}
563
//
564
//		return null;
565
//	}
566
//
567
//	public function getRemoteFromAddressAndId($address, $id) {
568
//		foreach ($this->links AS $link) {
569
//			if ($link->getAddress() === $address && $link->getUniqueId() === $id) {
570
//				return $link;
571
//			}
572
//		}
573
//
574
//		return null;
575
//	}
576
577
	/**
578
	 * @param integer|string $type
579
	 *
580
	 * @return integer
581
	 */
582
	public static function typeInt($type) {
583
584
		if (is_numeric($type)) {
585
			return (int)$type;
586
		}
587
588 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...
589
			case 'Personal':
590
				return self::CIRCLES_PERSONAL;
591
			case 'Closed':
592
				return self::CIRCLES_CLOSED;
593
			case 'Secret':
594
				return self::CIRCLES_SECRET;
595
			case 'Public':
596
				return self::CIRCLES_PUBLIC;
597
		}
598
599
		return 0;
600
	}
601
}
602