Completed
Pull Request — master (#362)
by Maxence
02:14
created

Circle::getInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
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 daita\MySmallPhpTools\Traits\TArrayTools;
30
use OCA\Circles\Exceptions\CircleTypeNotValidException;
31
use OCA\Circles\Exceptions\FederatedCircleNotAllowedException;
32
33
class Circle extends BaseCircle implements \JsonSerializable {
34
35
36
	use TArrayTools;
37
38
39
	/** @var bool */
40
	private $fullJson = false;
41
42
	/** @var bool */
43
	private $lightJson = false;
44
45
46
	public function getTypeString() {
47 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...
48
			case self::CIRCLES_PERSONAL:
49
				return 'Personal';
50
			case self::CIRCLES_SECRET:
51
				return 'Secret';
52
			case self::CIRCLES_CLOSED:
53
				return 'Closed';
54
			case self::CIRCLES_PUBLIC:
55
				return 'Public';
56
			case self::CIRCLES_ALL:
57
				return 'All';
58
		}
59
60
		return 'none';
61
	}
62
63
	public function getTypeLongString() {
64
		return self::typeLongString($this->getType());
65
	}
66
67
68
	public function getInfo() {
69
		return $this->getTypeLongString();
70
	}
71
72
73
	/**
74
	 * @param bool $fullJson
75
	 *
76
	 * @return $this
77
	 */
78
	public function setFullJson(bool $fullJson): self {
79
		$this->fullJson = $fullJson;
80
81
		return $this;
82
	}
83
84
85
	public function jsonSerialize() {
86
		$json = [
87
			'id'               => $this->getId(),
88
			'name'             => $this->getName(),
89
			'owner'            => $this->getOwner(),
90
			'user'             => $this->getViewer(),
91
			'group'            => $this->getGroupViewer(),
92
			'viewer'           => $this->getHigherViewer(),
93
			'description'      => $this->getDescription(),
94
			'settings'         => $this->getSettings(),
95
			'type'             => $this->getType(),
96
			'creation'         => $this->getCreation(),
97
			'type_string'      => $this->getTypeString(),
98
			'type_long_string' => $this->getTypeLongString(),
99
			'unique_id'        => $this->getUniqueId($this->fullJson),
100
			'members'          => $this->getMembers(),
101
			'groups'           => $this->getGroups(),
102
			'links'            => $this->getLinks()
103
		];
104
105
		if ($this->lightJson) {
106
			$json['members'] = [];
107
			$json['description'] = '';
108
			$json['links'] = [];
109
			$json['groups'] = [];
110
			$json['settings'] = [];
111
		}
112
113
		return $json;
114
	}
115
116
117
	public function getArray($full = false, $light = false) {
118
		$json = $this->getJson($full, $light);
119
120
		return json_decode($json, true);
121
	}
122
123
124
	public function getJson($full = false, $light = false) {
125
		$this->fullJson = $full;
126
		$this->lightJson = $light;
127
		$json = json_encode($this);
128
		$this->fullJson = false;
129
		$this->lightJson = false;
130
131
		return $json;
132
	}
133
134
135
	/**
136
	 * set all infos from an Array.
137
	 *
138
	 * @param $arr
139
	 *
140
	 * @return $this
141
	 */
142
	public static function fromArray($arr) {
143
		if ($arr === null || empty($arr)) {
144
			return new Circle();
145
		}
146
147
		$circle = new Circle($arr['type'], $arr['name']);
148
149
		$circle->setId($arr['id']);
150
		$circle->setUniqueId($arr['unique_id']);
151
		$circle->setDescription($arr['description']);
152
153
		$circle->setSettings(self::getSettingsFromArray($arr));
154
		$circle->setLinks(self::getLinksFromArray($arr));
155
		$circle->setCreation($arr['creation']);
156
157
		$circle->setViewer(self::getMemberFromArray($arr, 'user'));
0 ignored issues
show
Bug introduced by
It seems like self::getMemberFromArray($arr, 'user') can be null; however, setViewer() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
158
		$circle->setOwner(self::getMemberFromArray($arr, 'owner'));
0 ignored issues
show
Bug introduced by
It seems like self::getMemberFromArray($arr, 'owner') can be null; however, setOwner() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
159
160
		if (array_key_exists('members', $arr) && is_array($arr['members'])) {
161
			$members = [];
162
			foreach ($arr['members'] as $item) {
163
				$members[] = Member::fromArray($item);
164
			}
165
			$circle->setMembers($members);
166
		}
167
168
169
		return $circle;
170
	}
171
172
173
	/**
174
	 * @param array $arr
175
	 * @param $key
176
	 * @param int $type
177
	 *
178
	 * @return null|Member
179
	 */
180
	private static function getMemberFromArray($arr, $key, $type = Member::TYPE_USER) {
181
182
		// TODO: 0.15.0 - remove condition is null
183
		if (key_exists($key, $arr) && $arr[$key] !== null) {
184
			$viewer = Member::fromArray($arr[$key]);
185
			$viewer->setType($type);
186
187
			return $viewer;
188
		}
189
190
		return null;
191
192
	}
193
194
195
	/**
196
	 * @param array $arr
197
	 *
198
	 * @return array
199
	 */
200
	private static function getLinksFromArray($arr) {
201
		$links = [];
202
		if (key_exists('links', $arr)) {
203
			$links = $arr['links'];
204
		}
205
206
		return $links;
207
	}
208
209
210
	/**
211
	 * @param array $arr
212
	 *
213
	 * @return array
214
	 */
215
	private static function getSettingsFromArray($arr) {
216
		$settings = [];
217
		if (key_exists('settings', $arr)) {
218
			$settings = $arr['settings'];
219
		}
220
221
		return $settings;
222
	}
223
224
225
	/**
226
	 * @param $json
227
	 *
228
	 * @return Circle
229
	 */
230
	public static function fromJSON($json) {
231
		return self::fromArray(json_decode($json, true));
232
	}
233
234
235
	/**
236
	 * @throws CircleTypeNotValidException
237
	 */
238
	public function cantBePersonal() {
239
		if ($this->getType() === self::CIRCLES_PERSONAL) {
240
			throw new CircleTypeNotValidException(
241
				$this->l10n->t("This feature is not available for personal circles")
242
			);
243
		}
244
	}
245
246
247
	/**
248
	 * @throws FederatedCircleNotAllowedException
249
	 */
250
	public function hasToBeFederated() {
251
		if ($this->getSetting('allow_links') !== 'true') {
252
			throw new FederatedCircleNotAllowedException(
253
				$this->l10n->t('The circle is not federated')
254
			);
255
		}
256
	}
257
258
	/**
259
	 * @param $type
260
	 *
261
	 * @return string
262
	 */
263
	public static function typeLongString($type) {
264
		switch ($type) {
265
			case self::CIRCLES_PERSONAL:
266
				return 'Personal circle';
267
			case self::CIRCLES_SECRET:
268
				return 'Secret circle';
269
			case self::CIRCLES_CLOSED:
270
				return 'Closed circle';
271
			case self::CIRCLES_PUBLIC:
272
				return 'Public circle';
273
			case self::CIRCLES_ALL:
274
				return 'All circles';
275
		}
276
277
		return 'none';
278
	}
279
280
281
}
282
283
284