Completed
Push — master ( 537a78...b29bba )
by Maxence
03:35
created

Circle::getJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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