Completed
Pull Request — master (#384)
by Tortue
02:31
created

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