Completed
Push — master ( 8d67fa...90f02e )
by Maxence
03:18
created

BaseCircle::getLinks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
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\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 FederatedLink[] */
80
	private $links;
81
82
	/** @var FederatedLink[] */
83
	private $remote;
84
85
	public function __construct($l10n, $type = -1, $name = '') {
86
		$this->l10n = $l10n;
87
88
		if ($type > -1) {
89
			$this->type = $type;
90
		}
91
		if ($name !== '') {
92
			$this->name = $name;
93
		}
94
	}
95
96
97
	public function setId($id) {
98
		$this->id = $id;
99
100
		return $this;
101
	}
102
103
	public function getId() {
104
		return $this->id;
105
	}
106
107
108
	/**
109
	 * @param string $uniqueId
110
	 *
111
	 * @return $this
112
	 */
113
	public function setUniqueId($uniqueId) {
114
		$this->uniqueId = (string)$uniqueId;
115
116
		return $this;
117
	}
118
119
	/**
120
	 * @return string
121
	 */
122
	public function getUniqueId() {
123
		return $this->uniqueId;
124
	}
125
126
	public function generateUniqueId() {
127
		$uniqueId = bin2hex(openssl_random_pseudo_bytes(24));
128
		$this->setUniqueId($uniqueId);
129
	}
130
131
	public function shortenUniqueId() {
132
		$this->setUniqueId(substr($this->getUniqueId(), 0, 8));
133
	}
134
135
	public function setName($name) {
136
		$this->name = $name;
137
138
		return $this;
139
	}
140
141
	public function getName() {
142
		return $this->name;
143
	}
144
145
146
	public function getOwner() {
147
		return $this->owner;
148
	}
149
150
	public function setOwner($owner) {
151
		$this->owner = $owner;
152
153
		return $this;
154
	}
155
156
157
	/**
158
	 * @return Member
159
	 */
160
	public function getUser() {
161
		return $this->user;
162
	}
163
164
	public function setUser($user) {
165
		$this->user = $user;
166
167
		return $this;
168
	}
169
170
171
	public function setDescription($description) {
172
		$this->description = $description;
173
174
		return $this;
175
	}
176
177
	public function getDescription() {
178
		return $this->description;
179
	}
180
181
182
	public function setSettings($settings) {
183
		if (is_array($settings)) {
184
			$this->settings = $settings;
185
		} else if (is_string($settings)) {
186
			$this->settings = json_decode($settings, true);
0 ignored issues
show
Documentation Bug introduced by
It seems like json_decode($settings, true) of type * is incompatible with the declared type array of property $settings.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
187
		}
188
189
		return $this;
190
	}
191
192
	public function getSettings($json = false) {
193
194
		if ($json) {
195
			return json_encode($this->settings);
196
		}
197
198
		$settings = $this->settings;
199
		if ($settings === null) {
200
			$settings = [];
201
		}
202
203
		$ak = array_keys(self::CIRCLES_SETTINGS_DEFAULT);
204
		foreach ($ak AS $k) {
205
			if (!key_exists($k, $settings)) {
206
				$settings[$k] = self::CIRCLES_SETTINGS_DEFAULT[$k];
207
			}
208
		}
209
210
		return $settings;
211
	}
212
213
214
	public function setSetting($k, $v) {
215
		$this->settings[$k] = $v;
216
	}
217
218
219
	/**
220
	 * @param string $k
221
	 *
222
	 * @return string|null
223
	 */
224
	public function getSetting($k) {
225
		if (key_exists($k, $this->settings)) {
226
			return $this->settings[$k];
227
		}
228
		if (key_exists($k, (array)self::CIRCLES_SETTINGS_DEFAULT)) {
229
			return self::CIRCLES_SETTINGS_DEFAULT[$k];
230
		}
231
232
		return null;
233
	}
234
235
236
	public function setType($type) {
237
		$this->type = self::typeInt($type);
238
239
		return $this;
240
	}
241
242
	public function getType() {
243
		return $this->type;
244
	}
245
246
247
	public function setCreation($creation) {
248
		$this->creation = $creation;
249
250
		return $this;
251
	}
252
253
	public function getCreation() {
254
		return $this->creation;
255
	}
256
257
258
	public function setMembers($members) {
259
		$this->members = $members;
260
261
		return $this;
262
	}
263
264
	public function getMembers() {
265
		return $this->members;
266
	}
267
268
269
	public function setLinks($links) {
270
		$this->links = $links;
271
272
		return $this;
273
	}
274
275
	public function getLinks() {
276
		return $this->links;
277
	}
278
279
280
//	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...
281
//		return $this->remote;
282
//	}
283
//
284
//	public function addRemote($link) {
285
//		array_push($this->remote, $link);
286
//	}
287
//
288
//	public function getRemoteFromToken($token) {
289
//		foreach ($this->links AS $link) {
290
//			if ($link->getToken() === $token) {
291
//				return $link;
292
//			}
293
//		}
294
//
295
//		return null;
296
//	}
297
//
298
//	public function getRemoteFromAddressAndId($address, $id) {
299
//		foreach ($this->links AS $link) {
300
//			if ($link->getAddress() === $address && $link->getUniqueId() === $id) {
301
//				return $link;
302
//			}
303
//		}
304
//
305
//		return null;
306
//	}
307
308
309
	public static function typeInt($type) {
310
311
		if (is_numeric($type)) {
312
			return (int)$type;
313
		}
314
315 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...
316
			case 'Personal':
317
				return self::CIRCLES_PERSONAL;
318
			case 'Private':
319
				return self::CIRCLES_PRIVATE;
320
			case 'Hidden':
321
				return self::CIRCLES_HIDDEN;
322
			case 'Public':
323
				return self::CIRCLES_PUBLIC;
324
		}
325
326
		return 0;
327
	}
328
}