Completed
Pull Request — master (#362)
by Maxence
01:44
created

Circle::getLinksFromArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
rs 10
cc 2
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
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
	/**
69
	 * @param bool $fullJson
70
	 *
71
	 * @return $this
72
	 */
73
	public function setFullJson(bool $fullJson): self {
74
		$this->fullJson = $fullJson;
75
76
		return $this;
77
	}
78
79
80
	public function jsonSerialize() {
81
		$json = [
82
			'id'               => $this->getId(),
83
			'name'             => $this->getName(),
84
			'owner'            => $this->getOwner(),
85
			'user'             => $this->getViewer(),
86
			'group'            => $this->getGroupViewer(),
87
			'viewer'           => $this->getHigherViewer(),
88
			'description'      => $this->getDescription(),
89
			'settings'         => $this->getSettings(),
90
			'type'             => $this->getType(),
91
			'creation'         => $this->getCreation(),
92
			'type_string'      => $this->getTypeString(),
93
			'type_long_string' => $this->getTypeLongString(),
94
			'unique_id'        => $this->getUniqueId($this->fullJson),
95
			'members'          => $this->getMembers(),
96
			'groups'           => $this->getGroups(),
97
			'links'            => $this->getLinks()
98
		];
99
100
		if ($this->lightJson) {
101
			$json['members'] = [];
102
			$json['description'] = '';
103
			$json['links'] = [];
104
			$json['groups'] = [];
105
			$json['settings'] = [];
106
		}
107
108
		return $json;
109
	}
110
111
112
	public function getArray($full = false, $light = false) {
113
		$json = $this->getJson($full, $light);
114
115
		return json_decode($json, true);
116
	}
117
118
119
	public function getJson($full = false, $light = false) {
120
		$this->fullJson = $full;
121
		$this->lightJson = $light;
122
		$json = json_encode($this);
123
		$this->fullJson = false;
124
		$this->lightJson = false;
125
126
		return $json;
127
	}
128
129
130
	/**
131
	 * set all infos from an Array.
132
	 *
133
	 * @param $arr
134
	 *
135
	 * @return $this
136
	 */
137
	public static function fromArray($arr) {
138
		if ($arr === null || empty($arr)) {
139
			return new Circle();
140
		}
141
142
		$circle = new Circle($arr['type'], $arr['name']);
143
144
		$circle->setId($arr['id']);
145
		$circle->setUniqueId($arr['unique_id']);
146
		$circle->setDescription($arr['description']);
147
148
		$circle->setSettings(self::getSettingsFromArray($arr));
149
		$circle->setLinks(self::getLinksFromArray($arr));
150
		$circle->setCreation($arr['creation']);
151
152
		$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...
153
		$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...
154
155
		return $circle;
156
	}
157
158
159
	/**
160
	 * @param array $arr
161
	 * @param $key
162
	 * @param int $type
163
	 *
164
	 * @return null|Member
165
	 */
166
	private static function getMemberFromArray($arr, $key, $type = Member::TYPE_USER) {
167
168
		// TODO: 0.15.0 - remove condition is null
169
		if (key_exists($key, $arr) && $arr[$key] !== null) {
170
			$viewer = Member::fromArray($arr[$key]);
171
			$viewer->setType($type);
172
173
			return $viewer;
174
		}
175
176
		return null;
177
178
	}
179
180
181
	/**
182
	 * @param array $arr
183
	 *
184
	 * @return array
185
	 */
186
	private static function getLinksFromArray($arr) {
187
		$links = [];
188
		if (key_exists('links', $arr)) {
189
			$links = $arr['links'];
190
		}
191
192
		return $links;
193
	}
194
195
196
	/**
197
	 * @param array $arr
198
	 *
199
	 * @return array
200
	 */
201
	private static function getSettingsFromArray($arr) {
202
		$settings = [];
203
		if (key_exists('settings', $arr)) {
204
			$settings = $arr['settings'];
205
		}
206
207
		return $settings;
208
	}
209
210
211
	/**
212
	 * @param $json
213
	 *
214
	 * @return Circle
215
	 */
216
	public static function fromJSON($json) {
217
		return self::fromArray(json_decode($json, true));
218
	}
219
220
221
	/**
222
	 * @throws CircleTypeNotValidException
223
	 */
224
	public function cantBePersonal() {
225
		if ($this->getType() === self::CIRCLES_PERSONAL) {
226
			throw new CircleTypeNotValidException(
227
				$this->l10n->t("This feature is not available for personal circles")
228
			);
229
		}
230
	}
231
232
233
	/**
234
	 * @throws FederatedCircleNotAllowedException
235
	 */
236
	public function hasToBeFederated() {
237
		if ($this->getSetting('allow_links') !== 'true') {
238
			throw new FederatedCircleNotAllowedException(
239
				$this->l10n->t('The circle is not federated')
240
			);
241
		}
242
	}
243
244
	/**
245
	 * @param $type
246
	 *
247
	 * @return string
248
	 */
249
	public static function typeLongString($type) {
250
		switch ($type) {
251
			case self::CIRCLES_PERSONAL:
252
				return 'Personal circle';
253
			case self::CIRCLES_SECRET:
254
				return 'Secret circle';
255
			case self::CIRCLES_CLOSED:
256
				return 'Closed circle';
257
			case self::CIRCLES_PUBLIC:
258
				return 'Public circle';
259
			case self::CIRCLES_ALL:
260
				return 'All circles';
261
		}
262
263
		return 'none';
264
	}
265
266
267
}
268
269
270