Completed
Pull Request — master (#362)
by Maxence
02:14
created

BaseCircle::getOwner()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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