Completed
Pull Request — master (#347)
by Maxence
02:26
created

BaseCircle::setContactAddressBook()   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
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
	 * @return Member
215
	 */
216
	public function getGroupViewer() {
217
		return $this->viewerGroup;
218
	}
219
220
	/**
221
	 * @param Member $group
222
	 *
223
	 * @return BaseCircle
224
	 */
225
	public function setGroupViewer($group) {
226
		$this->viewerGroup = $group;
227
228
		return $this;
229
	}
230
231
	/**
232
	 * @return Member
233
	 */
234
	public function getHigherViewer() {
235
		if ($this->getGroupViewer() === null) {
236
			return $this->getViewer();
237
		}
238
239
		if ($this->getViewer() === null) {
240
			return $this->getGroupViewer();
241
		}
242
243
		if ($this->getGroupViewer()
244
				 ->getLevel() > $this->getViewer()
245
									 ->getLevel()
246
		) {
247
			return $this->getGroupViewer();
248
		}
249
250
		return $this->getViewer();
251
	}
252
253
254
	/**
255
	 * @param string $description
256
	 *
257
	 * @return BaseCircle
258
	 */
259
	public function setDescription($description) {
260
		$this->description = $description;
261
262
		return $this;
263
	}
264
265
	/**
266
	 * @return string
267
	 */
268
	public function getDescription() {
269
		return $this->description;
270
	}
271
272
273
	/**
274
	 * @param int $contactAddressBook
275
	 *
276
	 * @return BaseCircle
277
	 */
278
	public function setContactAddressBook(int $contactAddressBook) {
279
		$this->contactAddressBook = $contactAddressBook;
280
281
		return $this;
282
	}
283
284
	/**
285
	 * @return int
286
	 */
287
	public function getContactAddressBook() {
288
		return $this->contactAddressBook;
289
	}
290
291
292
	/**
293
	 * @param string $contactGroupName
294
	 *
295
	 * @return BaseCircle
296
	 */
297
	public function setContactGroupName($contactGroupName) {
298
		$this->contactGroupName = $contactGroupName;
299
300
		return $this;
301
	}
302
303
	/**
304
	 * @return string
305
	 */
306
	public function getContactGroupName() {
307
		return $this->contactGroupName;
308
	}
309
310
311
	/**
312
	 * @param string|array $settings
313
	 *
314
	 * @return $this
315
	 */
316
	public function setSettings($settings) {
317
		if (is_array($settings)) {
318
			$this->settings = $settings;
319
		} else if (is_string($settings)) {
320
			$this->settings = (array)json_decode($settings, true);
321
		}
322
323
		return $this;
324
	}
325
326
	/**
327
	 * @param bool $json
328
	 *
329
	 * @return array|string
330
	 */
331
	public function getSettings($json = false) {
332
333
		if ($json) {
334
			return json_encode($this->settings);
335
		}
336
337
		$settings = $this->settings;
338
		if ($settings === null) {
339
			$settings = [];
340
		}
341
342
		$ak = array_keys(self::CIRCLES_SETTINGS_DEFAULT);
343
		foreach ($ak AS $k) {
344
			if (!key_exists($k, $settings)) {
345
				$settings[$k] = self::CIRCLES_SETTINGS_DEFAULT[$k];
346
			}
347
		}
348
349
		return $settings;
350
	}
351
352
353
	/**
354
	 * @param string $k
355
	 * @param mixed $v
356
	 */
357
	public function setSetting($k, $v) {
358
		switch ($k) {
359
			case 'circle_name':
360
				$this->setName($v);
361
				break;
362
363
			case 'circle_desc':
364
				$this->setDescription($v);
365
				break;
366
367
			default:
368
				$this->settings[$k] = $v;
369
				break;
370
		}
371
	}
372
373
374
	/**
375
	 * @param string $k
376
	 *
377
	 * @return string|null
378
	 */
379
	public function getSetting($k) {
380
		if (key_exists($k, $this->settings)) {
381
			return $this->settings[$k];
382
		}
383
		if (key_exists($k, (array)self::CIRCLES_SETTINGS_DEFAULT)) {
384
			return self::CIRCLES_SETTINGS_DEFAULT[$k];
385
		}
386
387
		return null;
388
	}
389
390
	/**
391
	 *
392
	 * @param string $type
393
	 *
394
	 * @return \OCA\Circles\Model\BaseCircle
395
	 */
396
	public function setType($type) {
397
		$this->type = self::typeInt($type);
398
399
		return $this;
400
	}
401
402
	/**
403
	 * @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...
404
	 */
405
	public function getType() {
406
		return $this->type;
407
	}
408
409
	/**
410
	 * @param string $creation
411
	 *
412
	 * @return \OCA\Circles\Model\BaseCircle
413
	 */
414
	public function setCreation($creation) {
415
		$this->creation = $creation;
416
417
		return $this;
418
	}
419
420
	/**
421
	 * @return string
422
	 */
423
	public function getCreation() {
424
		return $this->creation;
425
	}
426
427
	/**
428
	 * @param array $members
429
	 *
430
	 * @return BaseCircle
431
	 */
432
	public function setMembers($members) {
433
		$this->members = $members;
434
435
		return $this;
436
	}
437
438
	/**
439
	 * @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...
440
	 */
441
	public function getMembers() {
442
		return $this->members;
443
	}
444
445
	/**
446
	 * @param array $groups
447
	 *
448
	 * @return BaseCircle
449
	 */
450
	public function setGroups($groups) {
451
		$this->groups = $groups;
452
453
		return $this;
454
	}
455
456
	/**
457
	 * @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...
458
	 */
459
	public function getGroups() {
460
		return $this->groups;
461
	}
462
463
	/**
464
	 * @param array $links
465
	 *
466
	 * @return BaseCircle
467
	 */
468
	public function setLinks($links) {
469
		$this->links = $links;
470
471
		return $this;
472
	}
473
474
	/**
475
	 * @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...
476
	 */
477
	public function getLinks() {
478
		return $this->links;
479
	}
480
481
482
//	public function getRemote() {
483
//		return $this->remote;
484
//	}
485
//
486
//	public function addRemote($link) {
487
//		array_push($this->remote, $link);
488
//	}
489
//
490
//	public function getRemoteFromToken($token) {
491
//		foreach ($this->links AS $link) {
492
//			if ($link->getToken() === $token) {
493
//				return $link;
494
//			}
495
//		}
496
//
497
//		return null;
498
//	}
499
//
500
//	public function getRemoteFromAddressAndId($address, $id) {
501
//		foreach ($this->links AS $link) {
502
//			if ($link->getAddress() === $address && $link->getUniqueId() === $id) {
503
//				return $link;
504
//			}
505
//		}
506
//
507
//		return null;
508
//	}
509
510
	/**
511
	 * @param integer|string $type
512
	 *
513
	 * @return integer
514
	 */
515
	public static function typeInt($type) {
516
517
		if (is_numeric($type)) {
518
			return (int)$type;
519
		}
520
521 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...
522
			case 'Personal':
523
				return self::CIRCLES_PERSONAL;
524
			case 'Closed':
525
				return self::CIRCLES_CLOSED;
526
			case 'Secret':
527
				return self::CIRCLES_SECRET;
528
			case 'Public':
529
				return self::CIRCLES_PUBLIC;
530
		}
531
532
		return 0;
533
	}
534
}
535