Completed
Pull Request — master (#6)
by Maxence
03:50 queued 47s
created

Circle::getOwnerMemberFromArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 6
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
class Circle implements \JsonSerializable {
30
31
	const CIRCLES_PERSONAL = 1;
32
	const CIRCLES_HIDDEN = 2;
33
	const CIRCLES_PRIVATE = 4;
34
	const CIRCLES_PUBLIC = 8;
35
36
	const CIRCLES_ALL = 15;
37
38
	private $id;
39
	private $name;
40
41
	/** @var Member */
42
	private $owner;
43
44
	/** @var Member */
45
	private $user;
46
	private $description;
47
	private $type;
48
	private $typeString;
49
	private $typeLongString;
50
	private $creation;
51
	private $members;
52
	private $info;
53
54
	public function __construct() {
55
56
	}
57
58
59
	public function setId($id) {
60
		$this->id = $id;
61
62
		return $this;
63
	}
64
65
	public function getId() {
66
		return $this->id;
67
	}
68
69
70
	public function setName($name) {
71
		$this->name = $name;
72
73
		return $this;
74
	}
75
76
	public function getName() {
77
		return $this->name;
78
	}
79
80
81
	public function getOwner() {
82
		return $this->owner;
83
	}
84
85
	public function setOwner($owner) {
86
		$this->owner = $owner;
87
	}
88
89
90
	public function getUser() {
91
		return $this->user;
92
	}
93
94
	public function setUser($user) {
95
		$this->user = $user;
96
	}
97
98
99
	public function setDescription($description) {
100
		$this->description = $description;
101
102
		return $this;
103
	}
104
105
	public function getDescription() {
106
		return $this->description;
107
	}
108
109
110
	public function setType($type) {
111
		$this->type = (int)$type;
112
		$this->setTypeString(self::TypeString($type));
113
		$this->setTypeLongString(self::TypeLongString($type));
114
		$this->setInfo($this->getTypeLongString());
115
116
		return $this;
117
	}
118
119
	public function getType() {
120
		return $this->type;
121
	}
122
123
	public function setTypeString($str) {
124
		$this->typeString = $str;
125
126
		return $this;
127
	}
128
129
	public function getTypeString() {
130
		return $this->typeString;
131
	}
132
133
	public function setTypeLongString($str) {
134
		$this->typeLongString = $str;
135
	}
136
137
	public function getTypeLongString() {
138
		return $this->typeLongString;
139
	}
140
141
142
	public function setInfo($str) {
143
		$this->info = $str;
144
	}
145
146
	public function getInfo() {
147
		return $this->info;
148
	}
149
150
151
	public function setCreation($creation) {
152
		$this->creation = $creation;
153
154
		return $this;
155
	}
156
157
	public function getCreation() {
158
		return $this->creation;
159
	}
160
161
162
	public function setMembers($members) {
163
		$this->members = $members;
164
165
		return $this;
166
	}
167
168
	public function getMembers() {
169
		return $this->members;
170
	}
171
172
173
	public function toString() {
174
		return "toString ?";
175
	}
176
177
	public function jsonSerialize() {
178
		return array(
179
			'id'          => $this->getId(),
180
			'name'        => $this->getName(),
181
			'owner'       => $this->getOwner(),
182
			'user'        => $this->getUser(),
183
			'description' => $this->getDescription(),
184
			'type'        => $this->getTypeString(),
185
			'creation'    => $this->getCreation(),
186
			'members'     => $this->getMembers()
187
		);
188
	}
189
190
	public static function fromArray($arr) {
191
		$circle = new Circle();
192
		$circle->setId($arr['id']);
193
194
		$circle->setName($arr['name']);
195
		$circle->setDescription($arr['description']);
196
		$circle->setType($arr['type']);
197
		$circle->setCreation($arr['creation']);
198
//		if (key_exists('count', $arr)) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% 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...
199
//			$circle->setCount($arr['count']);
200
//		}
201
202
		$circle->setOwner(self::getOwnerMemberFromArray($arr));
203
		$circle->setUser(self::getUserMemberFromArray($arr));
204
205
		return $circle;
206
	}
207
208
209
	/**
210
	 * return Owner Infos from Array
211
	 *
212
	 * @param $array
213
	 *
214
	 * @return null|Member
215
	 */
216
	private static function getOwnerMemberFromArray($array) {
217
		if (key_exists('owner', $array)) {
218
			$owner = new Member();
219
			$owner->setUserId($array['owner']);
220
221
			return $owner;
222
		}
223
224
		return null;
225
	}
226
227
	/**
228
	 * return User Infos from Array
229
	 *
230
	 * @param $array
231
	 *
232
	 * @return null|Member
233
	 */
234
	private static function getUserMemberFromArray($array) {
235
236
		if (key_exists('status', $array)
237
			&& key_exists('level', $array)
238
			&& key_exists('joined', $array)
239
		) {
240
			$user = new Member();
241
			$user->setStatus($array['status']);
242
			$user->setLevel($array['level']);
243
			$user->setJoined($array['joined']);
244
245
			return $user;
246
		}
247
248
		return null;
249
	}
250
251
252
	public static function TypeString($type) {
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
253
		switch ($type) {
254
			case self::CIRCLES_PERSONAL:
255
				return 'Personal';
256
			case self::CIRCLES_HIDDEN:
257
				return 'Hidden';
258
			case self::CIRCLES_PRIVATE:
259
				return 'Private';
260
			case self::CIRCLES_PUBLIC:
261
				return 'Public';
262
			case self::CIRCLES_ALL:
263
				return 'All';
264
		}
265
266
		return 'none';
267
	}
268
269
	/**
270
	 * @param $type
271
	 *
272
	 * @return string
273
	 */
274
	public static function TypeLongString($type) {
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
275
		switch ($type) {
276
			case self::CIRCLES_PERSONAL:
277
				return 'Personal Circle';
278
			case self::CIRCLES_HIDDEN:
279
				return 'Hidden Circle';
280
			case self::CIRCLES_PRIVATE:
281
				return 'Private Circle';
282
			case self::CIRCLES_PUBLIC:
283
				return 'Public Circle';
284
			case self::CIRCLES_ALL:
285
				return 'All Circles';
286
		}
287
288
		return 'none';
289
	}
290
291
}
292
293
294