Completed
Push — master ( fff755...8f8f7b )
by Maxence
03:48
created

Circle::fromJSON()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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