Completed
Push — master ( 48e0cc...46633a )
by Maxence
02:57
created

Circle::fromJSON()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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