Completed
Push — master ( 853863...462815 )
by Maxence
02:39
created

Circle::getLinksFromArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 5
nc 2
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\Exceptions\CircleTypeNotValidException;
30
use OCA\Circles\Exceptions\FederatedCircleNotAllowedException;
31
32
class Circle extends BaseCircle implements \JsonSerializable {
33
34
	/** @var bool */
35
	private $fullJson = false;
36
37
	/** @var bool */
38
	private $lightJson = false;
39
40
41
	public function getTypeString() {
42 View Code Duplication
		switch ($this->getType()) {
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...
43
			case self::CIRCLES_PERSONAL:
44
				return 'Personal';
45
			case self::CIRCLES_SECRET:
46
				return 'Secret';
47
			case self::CIRCLES_CLOSED:
48
				return 'Closed';
49
			case self::CIRCLES_PUBLIC:
50
				return 'Public';
51
			case self::CIRCLES_ALL:
52
				return 'All';
53
		}
54
55
		return 'none';
56
	}
57
58
	public function getTypeLongString() {
59
		return self::typeLongString($this->getType());
60
	}
61
62
63
	public function getInfo() {
64
		return $this->getTypeLongString();
65
	}
66
67
68
	public function jsonSerialize() {
69
		$json = array(
70
			'id'               => $this->getId(),
71
			'name'             => $this->getName(),
72
			'owner'            => $this->getOwner(),
73
			'user'             => $this->getViewer(),
74
			'group'            => $this->getGroupViewer(),
75
			'viewer'           => $this->getHigherViewer(),
76
			'description'      => $this->getDescription(),
77
			'settings'         => $this->getSettings(),
78
			'type'             => $this->getType(),
79
			'creation'         => $this->getCreation(),
80
			'type_string'      => $this->getTypeString(),
81
			'type_long_string' => $this->getTypeLongString(),
82
			'unique_id'        => $this->getUniqueId($this->fullJson),
83
			'members'          => $this->getMembers(),
84
			'groups'           => $this->getGroups(),
85
			'links'            => $this->getLinks()
86
		);
87
88
		if ($this->lightJson) {
89
			$json['members'] = [];
90
			$json['description'] = '';
91
			$json['links'] = [];
92
			$json['groups'] = [];
93
			$json['settings'] = [];
94
		}
95
96
		return $json;
97
	}
98
99
100
	public function getArray($full = false, $light = false) {
101
		$json = $this->getJson($full, $light);
102
103
		return json_decode($json, true);
104
	}
105
106
107
	public function getJson($full = false, $light = false) {
108
		$this->fullJson = $full;
109
		$this->lightJson = $light;
110
		$json = json_encode($this);
111
		$this->fullJson = false;
112
		$this->lightJson = false;
113
114
		return $json;
115
	}
116
117
118
	/**
119
	 * set all infos from an Array.
120
	 *
121
	 * @param $arr
122
	 *
123
	 * @return $this
124
	 */
125
	public static function fromArray($arr) {
126
		$circle = new Circle($arr['type'], $arr['name']);
127
128
		$circle->setId($arr['id']);
129
		$circle->setUniqueId($arr['unique_id']);
130
		$circle->setDescription($arr['description']);
131
132
		$circle->setSettings(self::getSettingsFromArray($arr));
133
		$circle->setLinks(self::getLinksFromArray($arr));
134
		$circle->setCreation($arr['creation']);
135
136
		$circle->setViewer(self::getMemberFromArray($arr, 'user'));
137
		$circle->setOwner(self::getMemberFromArray($arr, 'owner'));
138
139
		return $circle;
140
	}
141
142
143
	/**
144
	 * @param array $arr
145
	 * @param $key
146
	 * @param int $type
147
	 *
148
	 * @return null|Member
149
	 */
150
	private static function getMemberFromArray($arr, $key, $type = Member::TYPE_USER) {
151
152
		// TODO: 0.15.0 - remove condition is null
153
		if (key_exists($key, $arr) && $arr[$key] !== null) {
154
			$viewer = Member::fromArray($arr[$key]);
155
			$viewer->setType($type);
156
157
			return $viewer;
158
		}
159
160
		return null;
161
162
	}
163
164
165
	/**
166
	 * @param array $arr
167
	 *
168
	 * @return array
169
	 */
170
	private static function getLinksFromArray($arr) {
171
		$links = [];
172
		if (key_exists('links', $arr)) {
173
			$links = $arr['links'];
174
		}
175
176
		return $links;
177
	}
178
179
180
	/**
181
	 * @param array $arr
182
	 *
183
	 * @return array
184
	 */
185
	private static function getSettingsFromArray($arr) {
186
		$settings = [];
187
		if (key_exists('settings', $arr)) {
188
			$settings = $arr['settings'];
189
		}
190
191
		return $settings;
192
	}
193
194
195
	/**
196
	 * @param $json
197
	 *
198
	 * @return Circle
199
	 */
200
	public static function fromJSON($json) {
201
		return self::fromArray(json_decode($json, true));
202
	}
203
204
205
	/**
206
	 * @throws CircleTypeNotValidException
207
	 */
208
	public function cantBePersonal() {
209
		if ($this->getType() === self::CIRCLES_PERSONAL) {
210
			throw new CircleTypeNotValidException(
211
				$this->l10n->t("This feature is not available for personal circles")
212
			);
213
		}
214
	}
215
216
217
	/**
218
	 * @throws FederatedCircleNotAllowedException
219
	 */
220
	public function hasToBeFederated() {
221
		if ($this->getSetting('allow_links') !== 'true') {
222
			throw new FederatedCircleNotAllowedException(
223
				$this->l10n->t('The circle is not Federated')
224
			);
225
		}
226
	}
227
228
	/**
229
	 * @param $type
230
	 *
231
	 * @return string
232
	 */
233
	public static function typeLongString($type) {
234
		switch ($type) {
235
			case self::CIRCLES_PERSONAL:
236
				return 'Personal circle';
237
			case self::CIRCLES_SECRET:
238
				return 'Secret circle';
239
			case self::CIRCLES_CLOSED:
240
				return 'Closed circle';
241
			case self::CIRCLES_PUBLIC:
242
				return 'Public circle';
243
			case self::CIRCLES_ALL:
244
				return 'All circles';
245
		}
246
247
		return 'none';
248
	}
249
250
251
}
252
253
254