Completed
Push — master ( 2079de...41fdc1 )
by Maxence
02:55
created

Circle::cantBePersonal()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
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
	 * @deprecated
124
	 *
125
	 * @return $this
126
	 */
127
	public static function fromArray($arr) {
128
		$circle = new Circle();
129
130
		$circle->setId($arr['id']);
131
		$circle->setName($arr['name']);
132
		$circle->setUniqueId($arr['unique_id']);
133
		$circle->setDescription($arr['description']);
134
		if (key_exists('links', $arr)) {
135
			$circle->setLinks($arr['links']);
136
		}
137
		if (key_exists('settings', $arr)) {
138
			$circle->setSettings($arr['settings']);
139
		}
140
		$circle->setType($arr['type']);
141
		$circle->setCreation($arr['creation']);
142
143
		// TODO: 0.15.0 - remove condition is null
144 View Code Duplication
		if (key_exists('user', $arr) && $arr['user'] !== null) {
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...
145
			$viewer = Member::fromArray($arr['user']);
146
			$viewer->setType(Member::TYPE_USER);
147
			$circle->setViewer($viewer);
148
		}
149
150 View Code Duplication
		if (key_exists('owner', $arr) && $arr['owner'] !== null) {
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...
151
			$owner = Member::fromArray($arr['owner']);
152
			$owner->setType(Member::TYPE_USER);
153
			$circle->setOwner($owner);
154
		}
155
156
		return $circle;
157
	}
158
159
160
	/**
161
	 * @param $json
162
	 *
163
	 * @deprecated
164
	 * @return Circle
165
	 */
166
	public static function fromJSON($json) {
167
		return self::fromArray(json_decode($json, true));
0 ignored issues
show
Deprecated Code introduced by
The method OCA\Circles\Model\Circle::fromArray() has been deprecated.

This method has been deprecated.

Loading history...
168
	}
169
170
171
	/**
172
	 * @throws CircleTypeNotValidException
173
	 */
174
	public function cantBePersonal() {
175
		if ($this->getType() === self::CIRCLES_PERSONAL) {
176
			throw new CircleTypeNotValidException(
177
				$this->l10n->t("This feature is not available for personal circles")
178
			);
179
		}
180
	}
181
182
183
	/**
184
	 * @throws FederatedCircleNotAllowedException
185
	 */
186
	public function hasToBeFederated() {
187
		if ($this->getSetting('allow_links') !== 'true') {
188
			throw new FederatedCircleNotAllowedException(
189
				$this->l10n->t('The circle is not Federated')
190
			);
191
		}
192
	}
193
194
	/**
195
	 * @param $type
196
	 *
197
	 * @return string
198
	 */
199
	public static function typeLongString($type) {
200
		switch ($type) {
201
			case self::CIRCLES_PERSONAL:
202
				return 'Personal circle';
203
			case self::CIRCLES_SECRET:
204
				return 'Secret circle';
205
			case self::CIRCLES_CLOSED:
206
				return 'Closed circle';
207
			case self::CIRCLES_PUBLIC:
208
				return 'Public circle';
209
			case self::CIRCLES_ALL:
210
				return 'All circles';
211
		}
212
213
		return 'none';
214
	}
215
216
217
}
218
219
220