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 JsonSerializable; |
31
|
|
|
use OCA\Circles\Exceptions\CircleTypeNotValidException; |
32
|
|
|
use OCA\Circles\Exceptions\FederatedCircleNotAllowedException; |
33
|
|
|
|
34
|
|
|
class DeprecatedCircle extends BaseCircle implements JsonSerializable { |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
use TArrayTools; |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** @var bool */ |
41
|
|
|
private $fullJson = false; |
42
|
|
|
|
43
|
|
|
/** @var bool */ |
44
|
|
|
private $lightJson = false; |
45
|
|
|
|
46
|
|
|
|
47
|
|
|
public function getTypeString() { |
48
|
|
View Code Duplication |
switch ($this->getType()) { |
|
|
|
|
49
|
|
|
case self::CIRCLES_PERSONAL: |
50
|
|
|
return 'Personal'; |
51
|
|
|
case self::CIRCLES_SECRET: |
52
|
|
|
return 'Secret'; |
53
|
|
|
case self::CIRCLES_CLOSED: |
54
|
|
|
return 'Closed'; |
55
|
|
|
case self::CIRCLES_PUBLIC: |
56
|
|
|
return 'Public'; |
57
|
|
|
case self::CIRCLES_ALL: |
58
|
|
|
return 'All'; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return 'none'; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getTypeLongString() { |
65
|
|
|
return self::typeLongString($this->getType()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
public function getInfo() { |
70
|
|
|
return $this->getTypeLongString(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param bool $fullJson |
76
|
|
|
* |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
|
|
public function setFullJson(bool $fullJson): self { |
80
|
|
|
$this->fullJson = $fullJson; |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
public function jsonSerialize() { |
87
|
|
|
$json = [ |
88
|
|
|
'id' => $this->getId(), |
89
|
|
|
'name' => $this->getName(true), |
90
|
|
|
'alt_name' => $this->getAltName(), |
91
|
|
|
'owner' => $this->getOwner(), |
92
|
|
|
'user' => $this->getViewer(), |
93
|
|
|
'group' => $this->getGroupViewer(), |
94
|
|
|
'viewer' => $this->getHigherViewer(), |
95
|
|
|
'description' => $this->getDescription(), |
96
|
|
|
'settings' => $this->getSettings(), |
97
|
|
|
'type' => $this->getType(), |
98
|
|
|
'creation' => $this->getCreation(), |
99
|
|
|
'type_string' => $this->getTypeString(), |
100
|
|
|
'type_long_string' => $this->getTypeLongString(), |
101
|
|
|
'unique_id' => $this->getUniqueId($this->fullJson), |
102
|
|
|
'members' => $this->getMembers(), |
103
|
|
|
'groups' => $this->getGroups(), |
104
|
|
|
'links' => $this->getLinks() |
105
|
|
|
]; |
106
|
|
|
|
107
|
|
|
if ($this->lightJson) { |
108
|
|
|
$json['members'] = []; |
109
|
|
|
$json['description'] = ''; |
110
|
|
|
$json['links'] = []; |
111
|
|
|
$json['groups'] = []; |
112
|
|
|
$json['settings'] = []; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $json; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
public function getArray($full = false, $light = false) { |
120
|
|
|
$json = $this->getJson($full, $light); |
121
|
|
|
|
122
|
|
|
return json_decode($json, true); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
|
126
|
|
|
public function getJson($full = false, $light = false) { |
127
|
|
|
$this->fullJson = $full; |
128
|
|
|
$this->lightJson = $light; |
129
|
|
|
$json = json_encode($this); |
130
|
|
|
$this->fullJson = false; |
131
|
|
|
$this->lightJson = false; |
132
|
|
|
|
133
|
|
|
return $json; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* set all infos from an Array. |
139
|
|
|
* |
140
|
|
|
* @param $arr |
141
|
|
|
* @param bool $allSettings |
142
|
|
|
* |
143
|
|
|
* @return $this |
144
|
|
|
*/ |
145
|
|
|
public static function fromArray($arr, bool $allSettings = false) { |
146
|
|
|
if ($arr === null || empty($arr)) { |
147
|
|
|
return new DeprecatedCircle(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
$circle = new DeprecatedCircle($arr['type'], $arr['name']); |
151
|
|
|
|
152
|
|
|
if (array_key_exists('alt_name', $arr)) { |
153
|
|
|
$circle->setAltName($arr['alt_name']); |
154
|
|
|
} |
155
|
|
|
$circle->setId($arr['id']); |
156
|
|
|
$circle->setUniqueId($arr['unique_id']); |
157
|
|
|
$circle->setDescription($arr['description']); |
158
|
|
|
|
159
|
|
|
$circle->setSettings(self::getSettingsFromArray($arr), $allSettings); |
160
|
|
|
$circle->setLinks(self::getLinksFromArray($arr)); |
161
|
|
|
$circle->setCreation($arr['creation']); |
162
|
|
|
|
163
|
|
|
$circle->setViewer(self::getMemberFromArray($arr, 'user')); |
|
|
|
|
164
|
|
|
$circle->setOwner(self::getMemberFromArray($arr, 'owner')); |
|
|
|
|
165
|
|
|
|
166
|
|
|
if (array_key_exists('members', $arr) && is_array($arr['members'])) { |
167
|
|
|
$members = []; |
168
|
|
|
foreach ($arr['members'] as $item) { |
169
|
|
|
$members[] = DeprecatedMember::fromArray($item); |
170
|
|
|
} |
171
|
|
|
$circle->setMembers($members); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
return $circle; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @param array $arr |
181
|
|
|
* @param $key |
182
|
|
|
* @param int $type |
183
|
|
|
* |
184
|
|
|
* @return null|DeprecatedMember |
185
|
|
|
*/ |
186
|
|
|
private static function getMemberFromArray($arr, $key, $type = DeprecatedMember::TYPE_USER) { |
187
|
|
|
|
188
|
|
|
// TODO: 0.15.0 - remove condition is null |
189
|
|
|
if (key_exists($key, $arr) && $arr[$key] !== null) { |
190
|
|
|
$viewer = DeprecatedMember::fromArray($arr[$key]); |
191
|
|
|
$viewer->setType($type); |
192
|
|
|
|
193
|
|
|
return $viewer; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return null; |
197
|
|
|
|
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param array $arr |
203
|
|
|
* |
204
|
|
|
* @return array |
205
|
|
|
*/ |
206
|
|
|
private static function getLinksFromArray($arr) { |
207
|
|
|
$links = []; |
208
|
|
|
if (key_exists('links', $arr)) { |
209
|
|
|
$links = $arr['links']; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return $links; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @param array $arr |
218
|
|
|
* |
219
|
|
|
* @return array |
220
|
|
|
*/ |
221
|
|
|
private static function getSettingsFromArray($arr) { |
222
|
|
|
$settings = []; |
223
|
|
|
if (key_exists('settings', $arr)) { |
224
|
|
|
$settings = $arr['settings']; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return $settings; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @param $json |
233
|
|
|
* |
234
|
|
|
* @return DeprecatedCircle |
235
|
|
|
*/ |
236
|
|
|
public static function fromJSON($json) { |
237
|
|
|
return self::fromArray(json_decode($json, true)); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @throws CircleTypeNotValidException |
243
|
|
|
*/ |
244
|
|
|
public function cantBePersonal() { |
245
|
|
|
if ($this->getType() === self::CIRCLES_PERSONAL) { |
246
|
|
|
throw new CircleTypeNotValidException( |
247
|
|
|
$this->l10n->t("This feature is not available for personal circles") |
248
|
|
|
); |
249
|
|
|
} |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @throws FederatedCircleNotAllowedException |
255
|
|
|
*/ |
256
|
|
|
public function hasToBeFederated() { |
257
|
|
|
if ($this->getSetting('allow_links') !== 'true') { |
258
|
|
|
throw new FederatedCircleNotAllowedException( |
259
|
|
|
$this->l10n->t('The circle is not federated') |
260
|
|
|
); |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* @param $type |
266
|
|
|
* |
267
|
|
|
* @return string |
268
|
|
|
*/ |
269
|
|
|
public static function typeLongString($type) { |
270
|
|
|
switch ($type) { |
271
|
|
|
case self::CIRCLES_PERSONAL: |
272
|
|
|
return 'Personal circle'; |
273
|
|
|
case self::CIRCLES_SECRET: |
274
|
|
|
return 'Secret circle'; |
275
|
|
|
case self::CIRCLES_CLOSED: |
276
|
|
|
return 'Closed circle'; |
277
|
|
|
case self::CIRCLES_PUBLIC: |
278
|
|
|
return 'Public circle'; |
279
|
|
|
case self::CIRCLES_ALL: |
280
|
|
|
return 'All circles'; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
return 'none'; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* convert old type to new config (nc21) |
289
|
|
|
* |
290
|
|
|
* @param int $type |
291
|
|
|
* |
292
|
|
|
* @return int |
293
|
|
|
*/ |
294
|
|
|
public static function convertTypeToConfig(int $type): int { |
295
|
|
|
switch($type) { |
296
|
|
|
case DeprecatedCircle::CIRCLES_PERSONAL: |
297
|
|
|
return 2; |
298
|
|
|
case DeprecatedCircle::CIRCLES_SECRET: |
299
|
|
|
return 16; |
300
|
|
|
case DeprecatedCircle::CIRCLES_CLOSED: |
301
|
|
|
return 120; |
302
|
|
|
case DeprecatedCircle::CIRCLES_PUBLIC: |
303
|
|
|
return 8; |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
return 0; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
|
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
|
313
|
|
|
|
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.