Completed
Pull Request — master (#384)
by Tortue
02:27
created

Circles::listCircles()   C

Complexity

Conditions 17
Paths 15

Size

Total Lines 49

Duplication

Lines 12
Ratio 24.49 %

Importance

Changes 0
Metric Value
dl 12
loc 49
rs 5.2166
c 0
b 0
f 0
cc 17
nc 15
nop 5

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
 * @author Vinicius Cubas Brand <[email protected]>
10
 * @author Daniel Tygel <[email protected]>
11
 *
12
 * @copyright 2017
13
 * @license GNU AGPL version 3 or any later version
14
 *
15
 * This program is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License
26
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27
 *
28
 */
29
30
namespace OCA\Circles\Api\v1;
31
32
33
use OCA\Circles\AppInfo\Application;
34
use OCA\Circles\Exceptions\ApiVersionIncompatibleException;
35
use OCA\Circles\Model\Circle;
36
use OCA\Circles\Model\FederatedLink;
37
use OCA\Circles\Model\Member;
38
use OCA\Circles\Model\SharingFrame;
39
use OCA\Circles\Service\CirclesService;
40
use OCA\Circles\Service\ConfigService;
41
use OCA\Circles\Service\FederatedLinkService;
42
use OCA\Circles\Service\MembersService;
43
use OCA\Circles\Service\MiscService;
44
use OCA\Circles\Service\SharingFrameService;
45
use OCP\AppFramework\QueryException;
46
use OCP\Util;
47
48
class Circles {
49
50
	const API_VERSION = [0, 10, 0];
51
52
	protected static function getContainer() {
53
		$app = new Application();
54
55
		return $app->getContainer();
56
	}
57
58
59
	/**
60
	 * Circles::version();
61
	 *
62
	 * returns the current version of the API
63
	 *
64
	 * @return int[]
65
	 */
66
	public static function version() {
67
		return self::API_VERSION;
68
	}
69
70
71
	public static function addJavascriptAPI() {
72
		Util::addScript(Application::APP_NAME, 'circles.v1.circles');
73
		Util::addScript(Application::APP_NAME, 'circles.v1.members');
74
		Util::addScript(Application::APP_NAME, 'circles.v1.links');
75
		Util::addScript(Application::APP_NAME, 'circles.v1');
76
	}
77
78
79
	/**
80
	 * Circles::compareVersion();
81
	 *
82
	 * Compare and return true if version is compatible.
83
	 * Exception otherwise.
84
	 *
85
	 * @param array $apiVersion
86
	 *
87
	 * @return bool
88
	 * @throws ApiVersionIncompatibleException
89
	 */
90
	public static function compareVersion($apiVersion) {
91
		if ((int)$apiVersion[0] !== Circles::API_VERSION[0]
92
			|| (int)$apiVersion[1] !== Circles::API_VERSION[1]) {
93
			throw new ApiVersionIncompatibleException('api_not_compatible');
94
		}
95
96
		return true;
97
	}
98
99
100
	/**
101
	 * Circles::createCircle();
102
	 *
103
	 * Create a new circle and make the current user its owner.
104
	 * You must specify type and name. type is one of this value:
105
	 *
106
	 * CIRCLES_PERSONAL is 1 or 'personal'
107
	 * CIRCLES_SECRET is 2 or 'secret'
108
	 * CIRCLES_CLOSED is 4 or 'closed'
109
	 * CIRCLES_PUBLIC is 8 or 'public'
110
	 *
111
	 * @param mixed $type
112
	 * @param string $name
113
	 *
114
	 * @return Circle
115
	 * @throws QueryException
116
	 */
117
	public static function createCircle($type, $name) {
118
		$c = self::getContainer();
119
120
		return $c->query(CirclesService::class)
121
				 ->createCircle($type, $name);
122
	}
123
124
125
	/**
126
	 * Circles::joinCircle();
127
	 *
128
	 * This function will make the current user joining a circle identified by its Id.
129
	 *
130
	 * @param string $circleUniqueId
131
	 *
132
	 * @return Member
133
	 * @throws QueryException
134
	 */
135
	public static function joinCircle($circleUniqueId) {
136
		$c = self::getContainer();
137
138
		return $c->query(CirclesService::class)
139
				 ->joinCircle($circleUniqueId);
140
	}
141
142
143
	/**
144
	 * Circles::leaveCircle();
145
	 *
146
	 * This function will make the current user leaving the circle identified by its Id. Will fail
147
	 * if user is the owner of the circle.
148
	 *
149
	 * @param string $circleUniqueId
150
	 *
151
	 * @return Member
152
	 * @throws QueryException
153
	 */
154
	public static function leaveCircle($circleUniqueId) {
155
		$c = self::getContainer();
156
157
		return $c->query(CirclesService::class)
158
				 ->leaveCircle($circleUniqueId);
159
	}
160
161
162
	/**
163
	 * Circles::listCircles();
164
	 *
165
	 * This function list all circles fitting a search regarding its name and the level and the
166
	 * rights from the current user. In case of Secret circle, name needs to be complete so the
167
	 * circle is included in the list (or if the current user is the owner)
168
	 *
169
	 * example: Circles::listCircles(Circle::CIRCLES_ALL, '', 8, callback); will returns all
170
	 * circles when the current user is at least an Admin.
171
	 *
172
	 * @param mixed $type
173
	 * @param string $name
174
	 * @param int $level
175
	 * @param string $userId
176
	 *
177
	 * @param bool $forceAll
178
	 *
179
	 * @return Circle[]
180
	 * @throws QueryException
181
	 */
182
	public static function listCircles($type, $name = '', $level = 0, $userId = '', $forceAll = false) {
183
		$c = self::getContainer();
184
185
		$configService = $c->query(ConfigService::class);
186
		$circlesAreVisible = $configService->isListedCirclesAllowed();
187
		$circlesAllowed = (int)$configService->getAppValue(ConfigService::CIRCLES_ALLOW_CIRCLES);
188
189
		if (!$circlesAreVisible) {
190
			switch (true) {
191
				case ($type === Circle::CIRCLES_CLOSED):
192
				case ($type === Circle::CIRCLES_PUBLIC):
193
				case (Circle::CIRCLES_CLOSED + Circle::CIRCLES_PUBLIC === $circlesAllowed):
194
					return [];
195
					break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
196
				case ($type === Circle::CIRCLES_ALL &&
197
						$circlesAllowed === Circle::CIRCLES_ALL
198
				):
199
					$type = $type - Circle::CIRCLES_CLOSED - Circle::CIRCLES_PUBLIC;
200
					break;
201
				case ($type > Circle::CIRCLES_CLOSED &&
202
						$type < Circle::CIRCLES_PUBLIC &&
203
						$circlesAllowed === Circle::CIRCLES_ALL
204
				):
205
					$type = $type - Circle::CIRCLES_CLOSED;
206
					break;
207 View Code Duplication
				case ($type > Circle::CIRCLES_PUBLIC &&
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...
208
						$type < (Circle::CIRCLES_PUBLIC + Circle::CIRCLES_CLOSED) &&
209
						$circlesAllowed === Circle::CIRCLES_ALL
210
				):
211
					$type = $type - Circle::CIRCLES_PUBLIC;
212
					break;
213 View Code Duplication
				case ($type > (Circle::CIRCLES_PUBLIC + Circle::CIRCLES_CLOSED) &&
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...
214
						$type < Circle::CIRCLES_ALL &&
215
						$circlesAllowed === Circle::CIRCLES_ALL
216
				):
217
					$type = $type - Circle::CIRCLES_PUBLIC - Circle::CIRCLES_CLOSED;
218
					break;
219
			}
220
		}
221
222
		if ($userId === '') {
223
			$userId = \OC::$server->getUserSession()
224
								  ->getUser()
225
								  ->getUID();
226
		}
227
228
		return $c->query(CirclesService::class)
229
				 ->listCircles($userId, $type, $name, $level, $forceAll);
230
	}
231
232
233
	/**
234
	 * Circles::joinedCircles();
235
	 *
236
	 * Return all the circle the current user is a member.
237
	 *
238
	 * @param string $userId
239
	 * @param bool $forceAll
240
	 *
241
	 * @return Circle[]
242
	 * @throws QueryException
243
	 */
244
	public static function joinedCircles($userId = '', $forceAll = false) {
245
		return self::listCircles(Circle::CIRCLES_ALL, '', Member::LEVEL_MEMBER, $userId, $forceAll);
246
	}
247
248
249
	/**
250
	 * Circles::joinedCircleIds();
251
	 *
252
	 * Return all the circleIds the user is a member, if empty user, using current user.
253
	 *
254
	 * @param $userId
255
	 *
256
	 * @return array
257
	 * @throws QueryException
258
	 */
259
	public static function joinedCircleIds($userId = '') {
260
		$circleIds = [];
261
		$circles = self::listCircles(Circle::CIRCLES_ALL, '', Member::LEVEL_MEMBER, $userId);
262
		foreach ($circles as $circle) {
263
			$circleIds[] = $circle->getUniqueId();
264
		}
265
266
		return $circleIds;
267
	}
268
269
270
	/**
271
	 * Circles::detailsCircle();
272
	 *
273
	 * WARNING - This function is called by the core - WARNING
274
	 *                 Do not change it
275
	 *
276
	 * Returns details on the circle. If the current user is a member, the members list will be
277
	 * return as well.
278
	 *
279
	 * @param string $circleUniqueId
280
	 * @param bool $forceAll
281
	 *
282
	 * @return Circle
283
	 * @throws QueryException
284
	 */
285
	public static function detailsCircle($circleUniqueId, $forceAll = false) {
286
		$c = self::getContainer();
287
288
		return $c->query(CirclesService::class)
289
				 ->detailsCircle($circleUniqueId, $forceAll);
290
	}
291
292
293
	/**
294
	 * Circles::settingsCircle();
295
	 *
296
	 * Save the settings. Settings is an array and current user need to be an admin
297
	 *
298
	 * @param string $circleUniqueId
299
	 * @param array $settings
300
	 *
301
	 * @return Circle
302
	 * @throws QueryException
303
	 */
304
	public static function settingsCircle($circleUniqueId, array $settings) {
305
		$c = self::getContainer();
306
307
		return $c->query(CirclesService::class)
308
				 ->settingsCircle($circleUniqueId, $settings);
309
	}
310
311
312
	/**
313
	 * Circles::destroyCircle();
314
	 *
315
	 * This function will destroy the circle if the current user is the Owner.
316
	 *
317
	 * @param string $circleUniqueId
318
	 *
319
	 * @return mixed
320
	 * @throws QueryException
321
	 */
322
	public static function destroyCircle($circleUniqueId) {
323
		$c = self::getContainer();
324
325
		return $c->query(CirclesService::class)
326
				 ->removeCircle($circleUniqueId);
327
	}
328
329
330
	/**
331
	 * Circles::addMember();
332
	 *
333
	 * This function will add a user as member of the circle. Current user need at least to be
334
	 * Moderator.
335
	 *
336
	 * @param string $circleUniqueId
337
	 * @param string $ident
338
	 * @param int $type
339
	 *
340
	 * @return Member[]
341
	 * @throws QueryException
342
	 */
343
	public static function addMember($circleUniqueId, $ident, $type) {
344
		$c = self::getContainer();
345
346
		return $c->query(MembersService::class)
347
				 ->addMember($circleUniqueId, $ident, $type);
348
	}
349
350
351
	/**
352
	 * Circles::getMember();
353
	 *
354
	 * This function will return information on a member of the circle. Current user need at least
355
	 * to be Member.
356
	 *
357
	 * @param string $circleUniqueId
358
	 * @param string $ident
359
	 * @param int $type
360
	 * @param bool $forceAll
361
	 *
362
	 * @return Member
363
	 * @throws QueryException
364
	 */
365
	public static function getMember($circleUniqueId, $ident, $type, $forceAll = false) {
366
		$c = self::getContainer();
367
368
		return $c->query(MembersService::class)
369
				 ->getMember($circleUniqueId, $ident, $type, $forceAll);
370
	}
371
372
373
	/**
374
	 * Circles::removeMember();
375
	 *
376
	 * This function will remove a member from the circle. Current user needs to be at least
377
	 * Moderator and have a higher level that the targeted member.
378
	 *
379
	 * @param string $circleUniqueId
380
	 * @param string $ident
381
	 * @param int $type
382
	 *
383
	 * @return Member[]
384
	 * @throws QueryException
385
	 */
386
	public static function removeMember($circleUniqueId, $ident, $type) {
387
		$c = self::getContainer();
388
389
		return $c->query(MembersService::class)
390
				 ->removeMember($circleUniqueId, $ident, $type);
391
	}
392
393
394
	/**
395
	 * Circles::levelMember();
396
	 *
397
	 * Edit the level of a member of the circle. The current level of the target needs to be lower
398
	 * than the user that initiate the process (ie. the current user). The new level of the target
399
	 * cannot be the same than the current level of the user that initiate the process (ie. the
400
	 * current user).
401
	 *
402
	 * @param string $circleUniqueId
403
	 * @param string $ident
404
	 * @param int $type
405
	 * @param int $level
406
	 *
407
	 * @return Member[]
408
	 * @throws QueryException
409
	 */
410
	public static function levelMember($circleUniqueId, $ident, $type, $level) {
411
		$c = self::getContainer();
412
413
		return $c->query(MembersService::class)
414
				 ->levelMember($circleUniqueId, $ident, $type, $level);
415
	}
416
417
418
	/**
419
	 * Circles::shareToCircle();
420
	 *
421
	 * This function will share an item (array) to the circle identified by its Id.
422
	 * Source is the app that is sharing the item and type can be used by the app to identified the
423
	 * payload.
424
	 *
425
	 * @param string $circleUniqueId
426
	 * @param string $source
427
	 * @param string $type
428
	 * @param array $payload
429
	 * @param string $broadcaster
430
	 *
431
	 * @return mixed
432
	 * @throws QueryException
433
	 */
434
	public static function shareToCircle(
435
		$circleUniqueId, $source, $type, array $payload, $broadcaster
436
	) {
437
		$c = self::getContainer();
438
439
		$frame = new SharingFrame((string)$source, (string)$type);
440
		$frame->setPayload($payload);
441
442
		return $c->query(SharingFrameService::class)
443
				 ->createFrame($circleUniqueId, $frame, (string)$broadcaster);
444
	}
445
446
447
	/**
448
	 * Circles::getSharesFromCircle();
449
	 *
450
	 * This function will returns all item (array) shared to a specific circle identified by its Id,
451
	 * source and type. Limited to current user session.
452
	 *
453
	 * @param string $circleUniqueId
454
	 *
455
	 * @return mixed
456
	 * @throws QueryException
457
	 */
458
	public static function getSharesFromCircle($circleUniqueId) {
459
		$c = self::getContainer();
460
461
		return $c->query(SharingFrameService::class)
462
				 ->getFrameFromCircle($circleUniqueId);
463
	}
464
465
466
	/**
467
	 * Circles::linkCircle();
468
	 *
469
	 * Initiate a link procedure. Current user must be at least Admin of the circle.
470
	 * circleId is the local circle and remote is the target for the link.
471
	 * Remote format is: <circle_name>@<remote_host> when remote_host must be a valid HTTPS address.
472
	 * Remote format is: <circle_name>@<remote_host> when remote_host must be a valid HTTPS address.
473
	 *
474
	 * @param string $circleUniqueId
475
	 * @param string $remote
476
	 *
477
	 * @return FederatedLink
478
	 * @throws QueryException
479
	 */
480
	public static function linkCircle($circleUniqueId, $remote) {
481
		$c = self::getContainer();
482
483
		return $c->query(FederatedLinkService::class)
484
				 ->linkCircle($circleUniqueId, $remote);
485
	}
486
487
488
	/**
489
	 * Circles::generateLink();
490
	 *
491
	 * Returns the link to get access to a local circle.
492
	 *
493
	 * @param string $circleUniqueId
494
	 *
495
	 * @return string
496
	 */
497
	public static function generateLink($circleUniqueId) {
498
		return \OC::$server->getURLGenerator()
499
						   ->linkToRoute('circles.Navigation.navigate') . '#' . $circleUniqueId;
500
	}
501
502
503
	/**
504
	 * Circles::generateAbsoluteLink();
505
	 *
506
	 * Returns the absolute link to get access to a local circle.
507
	 *
508
	 * @param string $circleUniqueId
509
	 *
510
	 * @return string
511
	 */
512
	public static function generateAbsoluteLink($circleUniqueId) {
513
		return \OC::$server->getURLGenerator()
514
						   ->linkToRouteAbsolute('circles.Navigation.navigate') . '#' . $circleUniqueId;
515
	}
516
517
518
	/**
519
	 * Circles::generateRemoteLink();
520
	 *
521
	 * Returns the link to get access to a remote circle.
522
	 *
523
	 * @param FederatedLink $link
524
	 *
525
	 * @return string
526
	 */
527
	public static function generateRemoteLink(FederatedLink $link) {
528
		return \OC::$server->getURLGenerator()
529
						   ->linkToRoute('circles.Navigation.navigate') . '#' . $link->getUniqueId()
530
			   . '-' . $link->getToken();
531
	}
532
533
534
	/**
535
	 * @param SharingFrame $frame
536
	 *
537
	 * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
538
	 */
539
	public static function generateUserParameter(SharingFrame $frame) {
540
541
		if ($frame->getCloudId() !== null) {
542
			$name = $frame->getAuthor() . '@' . $frame->getCloudId();
543
		} else {
544
			$name = MiscService::getDisplay($frame->getAuthor(), Member::TYPE_USER);
545
		}
546
547
		return [
548
			'type' => 'user',
549
			'id'   => $frame->getAuthor(),
550
			'name' => $name
551
		];
552
	}
553
554
555
	/**
556
	 * @param SharingFrame $frame
557
	 *
558
	 * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
559
	 */
560
	public static function generateCircleParameter(SharingFrame $frame) {
561
		return [
562
			'type' => 'circle',
563
			'id'   => $frame->getCircle()
564
							->getUniqueId(),
565
			'name' => $frame->getCircle()
566
							->getName(),
567
			'link' => self::generateLink(
568
				$frame->getCircle()
569
					  ->getUniqueId()
570
			)
571
		];
572
	}
573
574
	/**
575
	 * Get a list of objects which are shred with $circleUniqueId.
576
	 *
577
	 * @since 0.14.0
578
	 *
579
	 * @param array $circleUniqueIds
580
	 *
581
	 * @return string[] array of object ids or empty array if none found
582
	 * @throws QueryException
583
	 */
584
	public static function getFilesForCircles($circleUniqueIds) {
585
		$c = self::getContainer();
586
587
		return $c->query(CirclesService::class)
588
			->getFilesForCircles($circleUniqueIds);
589
	}
590
}
591