Completed
Push — master ( 473696...473696 )
by Maxence
05:29 queued 02:51
created

BaseCircle::setType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
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 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
	public function __construct($l10n, $type = -1, $name = '') {
83
		$this->l10n = $l10n;
84
85
		if ($type > -1) {
86
			$this->type = $type;
87
		}
88
		if ($name !== '') {
89
			$this->name = $name;
90
		}
91
	}
92
93
94
	public function setId($id) {
95
		$this->id = $id;
96
97
		return $this;
98
	}
99
100
	public function getId() {
101
		return $this->id;
102
	}
103
104
105
	/**
106
	 * @param string $uniqueId
107
	 *
108
	 * @return $this
109
	 */
110
	public function setUniqueId($uniqueId) {
111
		$this->uniqueId = (string)$uniqueId;
112
113
		return $this;
114
	}
115
116
	/**
117
	 * @return string
118
	 */
119
	public function getUniqueId() {
120
		return $this->uniqueId;
121
	}
122
123
	public function generateUniqueId() {
124
		$uniqueId = bin2hex(openssl_random_pseudo_bytes(24));
125
		$this->setUniqueId($uniqueId);
126
	}
127
128
	public function setName($name) {
129
		$this->name = $name;
130
131
		return $this;
132
	}
133
134
	public function getName() {
135
		return $this->name;
136
	}
137
138
139
	public function getOwner() {
140
		return $this->owner;
141
	}
142
143
	public function setOwner($owner) {
144
		$this->owner = $owner;
145
146
		return $this;
147
	}
148
149
150
	/**
151
	 * @return Member
152
	 */
153
	public function getUser() {
154
		return $this->user;
155
	}
156
157
	public function setUser($user) {
158
		$this->user = $user;
159
160
		return $this;
161
	}
162
163
164
	public function setDescription($description) {
165
		$this->description = $description;
166
167
		return $this;
168
	}
169
170
	public function getDescription() {
171
		return $this->description;
172
	}
173
174
175
	public function setSettings($settings) {
176
		if (is_array($settings)) {
177
			$this->settings = $settings;
178
		} else if (is_string($settings)) {
179
			$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...
180
		}
181
182
		return $this;
183
	}
184
185
	public function getSettings($json = false) {
186
187
		if ($json) {
188
			return json_encode($this->settings);
189
		}
190
191
		$settings = $this->settings;
192
		$ak = array_keys(self::CIRCLES_SETTINGS_DEFAULT);
193
		foreach ($ak AS $k) {
194
			if (!key_exists($k, $settings)) {
195
				$settings[$k] = self::CIRCLES_SETTINGS_DEFAULT[$k];
196
			}
197
		}
198
199
		return $settings;
200
	}
201
202
203
	public function setSetting($k, $v) {
204
		$this->settings[$k] = $v;
205
	}
206
207
208
	/**
209
	 * @param string $k
210
	 *
211
	 * @return string|null
212
	 */
213
	public function getSetting($k) {
214
		if (key_exists($k, $this->settings)) {
215
			return $this->settings[$k];
216
		}
217
		if (key_exists($k, (array)self::CIRCLES_SETTINGS_DEFAULT)) {
218
			return self::CIRCLES_SETTINGS_DEFAULT[$k];
219
		}
220
221
		return null;
222
	}
223
224
225
	public function setType($type) {
226
		$this->type = self::typeInt($type);
227
228
		return $this;
229
	}
230
231
	public function getType() {
232
		return $this->type;
233
	}
234
235
236
	public function setCreation($creation) {
237
		$this->creation = $creation;
238
239
		return $this;
240
	}
241
242
	public function getCreation() {
243
		return $this->creation;
244
	}
245
246
247
	public function setMembers($members) {
248
		$this->members = $members;
249
250
		return $this;
251
	}
252
253
	public function getMembers() {
254
		return $this->members;
255
	}
256
257
258
	public function getRemote() {
259
		return $this->links;
260
	}
261
262
	public function addRemote($link) {
263
		array_push($this->links, $link);
264
	}
265
266
	public function getRemoteFromToken($token) {
267
		foreach ($this->links AS $link) {
268
			if ($link->getToken() === $token) {
269
				return $link;
270
			}
271
		}
272
273
		return null;
274
	}
275
276
	public function getRemoteFromAddressAndId($address, $id) {
277
		foreach ($this->links AS $link) {
278
			if ($link->getAddress() === $address && $link->getUniqueId() === $id) {
279
				return $link;
280
			}
281
		}
282
283
		return null;
284
	}
285
286
287
	public static function typeInt($type) {
288
289
		if (is_numeric($type)) {
290
			return (int)$type;
291
		}
292
293 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...
294
			case 'Personal':
295
				return self::CIRCLES_PERSONAL;
296
			case 'Private':
297
				return self::CIRCLES_PRIVATE;
298
			case 'Hidden':
299
				return self::CIRCLES_HIDDEN;
300
			case 'Public':
301
				return self::CIRCLES_PUBLIC;
302
		}
303
304
		return 0;
305
	}
306
}