Completed
Push — master ( dc9b8a...1415c3 )
by Maxence
02:30
created

BaseCircle::getSetting()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
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\L10N\L10N;
30
31
class BaseCircle {
32
33
	const CIRCLES_SETTINGS_DEFAULT = [
34
		'allow_links'       => 'false',
35
		'allow_links_auto'  => 'false',
36
		'allow_links_files' => 'false'
37
	];
38
39
	const CIRCLES_PERSONAL = 1;
40
	const CIRCLES_HIDDEN = 2;
41
	const CIRCLES_PRIVATE = 4;
42
	const CIRCLES_PUBLIC = 8;
43
44
	const CIRCLES_ALL = 15;
45
46
	/** @var int */
47
	private $id;
48
49
	/** @var L10N */
50
	protected $l10n;
51
52
	/** @var string */
53
	private $uniqueId;
54
55
	/** @var string */
56
	private $name;
57
58
	/** @var Member */
59
	private $owner;
60
61
	/** @var Member */
62
	private $user;
63
64
	/** @var string */
65
	private $description = '';
66
67
	/** @var array */
68
	private $settings = [];
69
70
	/** @var int */
71
	private $type;
72
73
	/** @var string */
74
	private $creation;
75
76
	/** @var Member[] */
77
	private $members;
78
79
	/** @var Member[] */
80
	private $groups;
81
82
	/** @var FederatedLink[] */
83
	private $links;
84
85
	/** @var FederatedLink[] */
86
	private $remote;
87
88
	public function __construct($l10n, $type = -1, $name = '') {
89
		$this->l10n = $l10n;
90
91
		if ($type > -1) {
92
			$this->type = $type;
93
		}
94
		if ($name !== '') {
95
			$this->name = $name;
96
		}
97
	}
98
99
100
	public function setId($id) {
101
		$this->id = $id;
102
103
		return $this;
104
	}
105
106
	public function getId() {
107
		return $this->id;
108
	}
109
110
111
	/**
112
	 * @param string $uniqueId
113
	 *
114
	 * @return $this
115
	 */
116
	public function setUniqueId($uniqueId) {
117
		$this->uniqueId = (string)$uniqueId;
118
119
		return $this;
120
	}
121
122
	/**
123
	 * @param bool $full
124
	 *
125
	 * @return string
126
	 */
127
	public function getUniqueId($full = false) {
128
		if ($full) {
129
			return $this->uniqueId;
130
		}
131
132
		return substr($this->uniqueId, 0, 14);
133
	}
134
135
136
	public function generateUniqueId() {
137
		$uniqueId = bin2hex(openssl_random_pseudo_bytes(24));
138
		$this->setUniqueId($uniqueId);
139
	}
140
141
	public function setName($name) {
142
		$this->name = $name;
143
144
		return $this;
145
	}
146
147
	public function getName() {
148
		return $this->name;
149
	}
150
151
152
	public function getOwner() {
153
		return $this->owner;
154
	}
155
156
	public function setOwner($owner) {
157
		$this->owner = $owner;
158
159
		return $this;
160
	}
161
162
163
	/**
164
	 * @return Member
165
	 */
166
	public function getViewer() {
167
		return $this->user;
168
	}
169
170
	public function setViewer($user) {
171
		$this->user = $user;
172
173
		return $this;
174
	}
175
176
177
	public function setDescription($description) {
178
		$this->description = $description;
179
180
		return $this;
181
	}
182
183
	public function getDescription() {
184
		return $this->description;
185
	}
186
187
188
	public function setSettings($settings) {
189
		if (is_array($settings)) {
190
			$this->settings = $settings;
191
		} else if (is_string($settings)) {
192
			$this->settings = (array)json_decode($settings, true);
193
		}
194
195
		return $this;
196
	}
197
198
	public function getSettings($json = false) {
199
200
		if ($json) {
201
			return json_encode($this->settings);
202
		}
203
204
		$settings = $this->settings;
205
		if ($settings === null) {
206
			$settings = [];
207
		}
208
209
		$ak = array_keys(self::CIRCLES_SETTINGS_DEFAULT);
210
		foreach ($ak AS $k) {
211
			if (!key_exists($k, $settings)) {
212
				$settings[$k] = self::CIRCLES_SETTINGS_DEFAULT[$k];
213
			}
214
		}
215
216
		return $settings;
217
	}
218
219
220
	public function setSetting($k, $v) {
221
		switch ($k) {
222
			case 'circle_name':
223
				$this->setName($v);
224
				break;
225
226
			case 'circle_desc':
227
				$this->setDescription($v);
228
				break;
229
230
			default:
231
				$this->settings[$k] = $v;
232
				break;
233
		}
234
	}
235
236
237
	/**
238
	 * @param string $k
239
	 *
240
	 * @return string|null
241
	 */
242
	public function getSetting($k) {
243
		if (key_exists($k, $this->settings)) {
244
			return $this->settings[$k];
245
		}
246
		if (key_exists($k, (array)self::CIRCLES_SETTINGS_DEFAULT)) {
247
			return self::CIRCLES_SETTINGS_DEFAULT[$k];
248
		}
249
250
		return null;
251
	}
252
253
254
	public function setType($type) {
255
		$this->type = self::typeInt($type);
256
257
		return $this;
258
	}
259
260
	public function getType() {
261
		return $this->type;
262
	}
263
264
265
	public function setCreation($creation) {
266
		$this->creation = $creation;
267
268
		return $this;
269
	}
270
271
	public function getCreation() {
272
		return $this->creation;
273
	}
274
275
276
	public function setMembers($members) {
277
		$this->members = $members;
278
279
		return $this;
280
	}
281
282
	public function getMembers() {
283
		return $this->members;
284
	}
285
286
287
	public function setGroups($groups) {
288
		$this->groups = $groups;
289
290
		return $this;
291
	}
292
293
	public function getGroups() {
294
		return $this->groups;
295
	}
296
297
298
	public function setLinks($links) {
299
		$this->links = $links;
300
301
		return $this;
302
	}
303
304
	public function getLinks() {
305
		return $this->links;
306
	}
307
308
309
//	public function getRemote() {
0 ignored issues
show
Unused Code Comprehensibility introduced by
49% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
310
//		return $this->remote;
311
//	}
312
//
313
//	public function addRemote($link) {
314
//		array_push($this->remote, $link);
315
//	}
316
//
317
//	public function getRemoteFromToken($token) {
318
//		foreach ($this->links AS $link) {
319
//			if ($link->getToken() === $token) {
320
//				return $link;
321
//			}
322
//		}
323
//
324
//		return null;
325
//	}
326
//
327
//	public function getRemoteFromAddressAndId($address, $id) {
328
//		foreach ($this->links AS $link) {
329
//			if ($link->getAddress() === $address && $link->getUniqueId() === $id) {
330
//				return $link;
331
//			}
332
//		}
333
//
334
//		return null;
335
//	}
336
337
338
	public static function typeInt($type) {
339
340
		if (is_numeric($type)) {
341
			return (int)$type;
342
		}
343
344 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...
345
			case 'Personal':
346
				return self::CIRCLES_PERSONAL;
347
			case 'Private':
348
				return self::CIRCLES_PRIVATE;
349
			case 'Hidden':
350
				return self::CIRCLES_HIDDEN;
351
			case 'Public':
352
				return self::CIRCLES_PUBLIC;
353
		}
354
355
		return 0;
356
	}
357
}