Completed
Push — master ( 33c782...ea898b )
by Maxence
02:48
created

MembersList::displayLeaf()   C

Complexity

Conditions 16
Paths 156

Size

Total Lines 63

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 63
rs 5.1
c 0
b 0
f 0
cc 16
nc 156
nop 2

How to fix   Long Method    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
declare(strict_types=1);
4
5
6
/**
7
 * Circles - Bring cloud-users closer together.
8
 *
9
 * This file is licensed under the Affero General Public License version 3 or
10
 * later. See the COPYING file.
11
 *
12
 * @author Maxence Lange <[email protected]>
13
 * @copyright 2017
14
 * @license GNU AGPL version 3 or any later version
15
 *
16
 * This program is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License as
18
 * published by the Free Software Foundation, either version 3 of the
19
 * License, or (at your option) any later version.
20
 *
21
 * This program is distributed in the hope that it will be useful,
22
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
 * GNU Affero General Public License for more details.
25
 *
26
 * You should have received a copy of the GNU Affero General Public License
27
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28
 *
29
 */
30
31
32
namespace OCA\Circles\Command;
33
34
use ArtificialOwl\MySmallPhpTools\Exceptions\InvalidItemException;
35
use ArtificialOwl\MySmallPhpTools\Exceptions\ItemNotFoundException;
36
use ArtificialOwl\MySmallPhpTools\Exceptions\RequestNetworkException;
37
use ArtificialOwl\MySmallPhpTools\Exceptions\SignatoryException;
38
use ArtificialOwl\MySmallPhpTools\Exceptions\UnknownTypeException;
39
use ArtificialOwl\MySmallPhpTools\Model\Nextcloud\nc22\NC22TreeNode;
40
use ArtificialOwl\MySmallPhpTools\Model\SimpleDataStore;
41
use ArtificialOwl\MySmallPhpTools\Traits\Nextcloud\nc22\TNC22ConsoleTree;
42
use OC\Core\Command\Base;
43
use OCA\Circles\Db\MemberRequest;
44
use OCA\Circles\Exceptions\CircleNotFoundException;
45
use OCA\Circles\Exceptions\FederatedItemException;
46
use OCA\Circles\Exceptions\FederatedUserException;
47
use OCA\Circles\Exceptions\FederatedUserNotFoundException;
48
use OCA\Circles\Exceptions\InitiatorNotFoundException;
49
use OCA\Circles\Exceptions\InvalidIdException;
50
use OCA\Circles\Exceptions\MemberNotFoundException;
51
use OCA\Circles\Exceptions\OwnerNotFoundException;
52
use OCA\Circles\Exceptions\RemoteInstanceException;
53
use OCA\Circles\Exceptions\RemoteNotFoundException;
54
use OCA\Circles\Exceptions\RemoteResourceNotFoundException;
55
use OCA\Circles\Exceptions\RequestBuilderException;
56
use OCA\Circles\Exceptions\SingleCircleNotFoundException;
57
use OCA\Circles\Exceptions\UnknownRemoteException;
58
use OCA\Circles\Exceptions\UserTypeNotFoundException;
59
use OCA\Circles\Model\Circle;
60
use OCA\Circles\Model\Member;
61
use OCA\Circles\Service\CircleService;
62
use OCA\Circles\Service\ConfigService;
63
use OCA\Circles\Service\FederatedUserService;
64
use OCA\Circles\Service\MemberService;
65
use OCA\Circles\Service\RemoteService;
66
use Symfony\Component\Console\Helper\Table;
67
use Symfony\Component\Console\Input\InputArgument;
68
use Symfony\Component\Console\Input\InputInterface;
69
use Symfony\Component\Console\Input\InputOption;
70
use Symfony\Component\Console\Output\ConsoleOutput;
71
use Symfony\Component\Console\Output\OutputInterface;
72
73
74
/**
75
 * Class MembersList
76
 *
77
 * @package OCA\Circles\Command
78
 */
79
class MembersList extends Base {
80
81
82
	use TNC22ConsoleTree;
83
84
85
	/** @var MemberRequest */
86
	private $memberRequest;
87
88
	/** @var FederatedUserService */
89
	private $federatedUserService;
90
91
	/** @var RemoteService */
92
	private $remoteService;
93
94
	/** @var CircleService */
95
	private $circleService;
96
97
	/** @var MemberService */
98
	private $memberService;
99
100
	/** @var ConfigService */
101
	private $configService;
102
103
104
	/** @var InputInterface */
105
	private $input;
106
107
	/** @var string */
108
	private $treeType = '';
109
110
	/**
111
	 * MembersList constructor.
112
	 *
113
	 * @param MemberRequest $memberRequest
114
	 * @param FederatedUserService $federatedUserService
115
	 * @param RemoteService $remoteService
116
	 * @param CircleService $circleService
117
	 * @param MemberService $memberService
118
	 * @param ConfigService $configService
119
	 */
120
	public function __construct(
121
		MemberRequest $memberRequest, FederatedUserService $federatedUserService,
122
		RemoteService $remoteService, CircleService $circleService, MemberService $memberService,
123
		ConfigService $configService
124
	) {
125
		parent::__construct();
126
127
		$this->memberRequest = $memberRequest;
128
		$this->federatedUserService = $federatedUserService;
129
		$this->remoteService = $remoteService;
130
		$this->circleService = $circleService;
131
		$this->memberService = $memberService;
132
		$this->configService = $configService;
133
	}
134
135
136
	protected function configure() {
137
		parent::configure();
138
		$this->setName('circles:members:list')
139
			 ->setDescription('listing Members from a Circle')
140
			 ->addArgument('circle_id', InputArgument::REQUIRED, 'ID of the circle')
141
			 ->addOption('instance', '', InputOption::VALUE_REQUIRED, 'Instance of the circle', '')
142
			 ->addOption('inherited', '', InputOption::VALUE_NONE, 'Display all inherited members')
143
			 ->addOption('initiator', '', InputOption::VALUE_REQUIRED, 'set an initiator to the request', '')
144
			 ->addOption('initiator-type', '', InputOption::VALUE_REQUIRED, 'set initiator type', '0')
145
			 ->addOption('display-name', '', InputOption::VALUE_NONE, 'display the displayName')
146
			 ->addOption('tree', '', InputOption::VALUE_OPTIONAL, 'display members as a tree', false);
147
	}
148
149
150
	/**
151
	 * @param InputInterface $input
152
	 * @param OutputInterface $output
153
	 *
154
	 * @return int
155
	 * @throws CircleNotFoundException
156
	 * @throws FederatedUserException
157
	 * @throws FederatedUserNotFoundException
158
	 * @throws InitiatorNotFoundException
159
	 * @throws InvalidIdException
160
	 * @throws InvalidItemException
161
	 * @throws MemberNotFoundException
162
	 * @throws OwnerNotFoundException
163
	 * @throws RemoteInstanceException
164
	 * @throws RemoteNotFoundException
165
	 * @throws RemoteResourceNotFoundException
166
	 * @throws RequestNetworkException
167
	 * @throws SignatoryException
168
	 * @throws UnknownRemoteException
169
	 * @throws UserTypeNotFoundException
170
	 * @throws FederatedItemException
171
	 * @throws SingleCircleNotFoundException
172
	 * @throws RequestBuilderException
173
	 */
174
	protected function execute(InputInterface $input, OutputInterface $output): int {
175
		$this->input = $input;
176
		$circleId = $input->getArgument('circle_id');
177
		$instance = $input->getOption('instance');
178
		$initiator = $input->getOption('initiator');
179
		$initiatorType = Member::parseTypeString($input->getOption('initiator-type'));
180
		$inherited = $input->getOption('inherited');
181
182
		$tree = null;
183
		if ($input->getOption('tree') !== false) {
184
			$this->treeType = (is_null($input->getOption('tree'))) ? 'all' : $input->getOption('tree');
185
186
			$this->federatedUserService->commandLineInitiator($initiator, $initiatorType, $circleId, true);
187
			$circle = $this->circleService->getCircle($circleId);
188
189
			$output->writeln('<info>Name</info>: ' . $circle->getName());
190
			$owner = $circle->getOwner();
191
			$output->writeln('<info>Owner</info>: ' . $owner->getUserId() . '@' . $owner->getInstance());
192
			$type = implode(", ", Circle::getCircleFlags($circle, Circle::FLAGS_LONG));
193
			$output->writeln('<info>Config</info>: ' . $type);
194
			$output->writeln(' ');
195
196
			$tree = new NC22TreeNode(null, new SimpleDataStore(['circle' => $circle]));
197
			$inherited = false;
198
		}
199
200
		if ($inherited) {
201
			$this->federatedUserService->commandLineInitiator($initiator, $initiatorType, $circleId, true);
202
			$circle = $this->circleService->getCircle($circleId);
203
			$members = $circle->getInheritedMembers(true);
204
		} else {
205
			$members = $this->getMembers($circleId, $instance, $initiator, $initiatorType, $tree);
206
		}
207
208
		if (!is_null($tree)) {
209
			$this->drawTree(
210
				$tree, [$this, 'displayLeaf'],
211
				[
212
					'height'       => 3,
213
					'node-spacing' => 1,
214
					'item-spacing' => 0,
215
				]
216
			);
217
218
			return 0;
219
		}
220
221
		if (strtolower($input->getOption('output')) === 'json') {
222
			$output->writeln(json_encode($members, JSON_PRETTY_PRINT));
223
224
			return 0;
225
		}
226
227
		$output = new ConsoleOutput();
228
		$output = $output->section();
229
230
		$table = new Table($output);
231
		$table->setHeaders(
232
			[
233
				'Circle Id', 'Circle Name', 'Member Id', 'Single Id', 'Type', 'Source',
234
				'Username', 'Instance', 'Level', 'Invited By'
235
			]
236
		);
237
		$table->render();
238
239
		foreach ($members as $member) {
240
			if ($member->getCircleId() === $circleId) {
241
				$level = $member->getLevel();
242
			} else {
243
				$level = $member->getInheritanceFrom()->getLevel();
244
			}
245
246
			$table->appendRow(
247
				[
248
					$member->getCircleId(),
249
					$member->getCircle()->getDisplayName(),
250
					$member->getId(),
251
					$member->getSingleId(),
252
					Member::$TYPE[$member->getUserType()],
253
					$member->hasBasedOn() ? Circle::$DEF_SOURCE[$member->getBasedOn()->getSource()] : '',
254
					$this->configService->displayFederatedUser(
255
						$member, $this->input->getOption('display-name')
256
					),
257
					$this->configService->displayInstance($member->getInstance()),
258
					($level > 0) ? Member::$DEF_LEVEL[$level] :
259
						'(' . strtolower($member->getStatus()) . ')',
260
					($member->hasInvitedBy()) ? $this->configService->displayFederatedUser(
261
						$member->getInvitedBy(), $this->input->getOption('display-name')
262
					) : 'Unknown'
263
				]
264
			);
265
		}
266
267
		return 0;
268
	}
269
270
271
	/**
272
	 * @param string $circleId
273
	 * @param string $instance
274
	 * @param string $initiator
275
	 * @param int $initiatorType
276
	 * @param NC22TreeNode|null $tree
277
	 * @param array $knownIds
278
	 *
279
	 * @return array
280
	 * @throws CircleNotFoundException
281
	 * @throws FederatedItemException
282
	 * @throws FederatedUserException
283
	 * @throws FederatedUserNotFoundException
284
	 * @throws InitiatorNotFoundException
285
	 * @throws InvalidIdException
286
	 * @throws InvalidItemException
287
	 * @throws MemberNotFoundException
288
	 * @throws OwnerNotFoundException
289
	 * @throws RemoteInstanceException
290
	 * @throws RemoteNotFoundException
291
	 * @throws RemoteResourceNotFoundException
292
	 * @throws RequestBuilderException
293
	 * @throws RequestNetworkException
294
	 * @throws SignatoryException
295
	 * @throws SingleCircleNotFoundException
296
	 * @throws UnknownRemoteException
297
	 * @throws UserTypeNotFoundException
298
	 */
299
	private function getMembers(
300
		string $circleId,
301
		string $instance,
302
		string $initiator,
303
		int $initiatorType,
304
		?NC22TreeNode $tree,
305
		array $knownIds = []
306
	): array {
307
		if (in_array($circleId, $knownIds)) {
308
			return [];
309
		}
310
		$knownIds[] = $circleId;
311
312
		if (!$this->configService->isLocalInstance($instance)) {
313
			$data = [];
314
			if ($initiator) {
315
				$data['initiator'] = $this->federatedUserService->getFederatedUser(
316
					$initiator,
317
					Member::TYPE_USER
318
				);
319
			}
320
321
			try {
322
				$members = $this->remoteService->getMembersFromInstance($circleId, $instance, $data);
323
			} catch (RemoteInstanceException $e) {
324
				return [];
325
			}
326
		} else {
327
			$this->federatedUserService->commandLineInitiator($initiator, $initiatorType, $circleId, true);
328
			$members = $this->memberService->getMembers($circleId);
329
		}
330
331
		if (!is_null($tree)) {
332
			foreach ($members as $member) {
333
				if ($member->getUserType() === Member::TYPE_CIRCLE) {
334
					if (!$this->configService->isLocalInstance($member->getInstance())) {
335
						$data = [];
336
						if ($initiator) {
337
							$data['initiator'] = $this->federatedUserService->getFederatedUser(
338
								$initiator,
339
								Member::TYPE_USER
340
							);
341
						}
342
343
						$circle = null;
344
						try {
345
							$circle = $this->remoteService->getCircleFromInstance(
346
								$member->getSingleId(), $member->getInstance(), $data
347
							);
348
						} catch (CircleNotFoundException | RemoteInstanceException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
349
						}
350
					} else {
351
						$this->federatedUserService->commandLineInitiator(
352
							$initiator,
353
							$initiatorType,
354
							$member->getSingleId(),
355
							true
356
						);
357
						$circle = $this->circleService->getCircle($member->getSingleId(), 0);
358
					}
359
					$node = new NC22TreeNode(
360
						$tree, new SimpleDataStore(
361
								 [
362
									 'circle'  => $circle,
363
									 'member'  => $member,
364
									 'cycling' => in_array($member->getSingleId(), $knownIds),
365
								 ]
366
							 )
367
					);
368
369
					$this->getMembers(
370
						$member->getSingleId(),
371
						$member->getInstance(),
372
						$initiator,
373
						$initiatorType,
374
						$node,
375
						$knownIds
376
					);
377
				} else {
378
					if ($this->treeType !== 'circles-only') {
379
						new NC22TreeNode(
380
							$tree, new SimpleDataStore(
381
									 [
382
										 'member'  => $member,
383
										 'cycling' => in_array($member->getSingleId(), $knownIds)
384
									 ]
385
								 )
386
						);
387
					}
388
				}
389
			}
390
		}
391
392
		return $members;
393
	}
394
395
396
	/**
397
	 * @param SimpleDataStore $data
398
	 * @param int $lineNumber
399
	 *
400
	 * @return string
401
	 * @throws OwnerNotFoundException
402
	 */
403
	public function displayLeaf(SimpleDataStore $data, int $lineNumber): string {
404
		if ($lineNumber === 3) {
405
			return ($data->gBool('cycling')) ? '<comment>(loop detected)</comment>' : '';
406
		}
407
408
		try {
409
			$line = '';
410
			$circle = null;
411
			if ($data->hasKey('circle')) {
412
				/** @var Circle $circle */
413
				$circle = $data->gObj('circle', Circle::class);
414
			}
415
416
			if ($data->hasKey('member')) {
417
				/** @var Member $member */
418
				$member = $data->gObj('member', Member::class);
419
420
				if ($lineNumber === 1) {
421
					$line .= '<info>' . $member->getSingleId() . '</info>';
422
					if (!$this->configService->isLocalInstance($member->getInstance())) {
423
						$line .= '@' . $member->getInstance();
424
					}
425
					$line .= ' (' . Member::$DEF_LEVEL[$member->getLevel()] . ')';
426
427
					$line .= ' <info>MemberId</info>: ' . $member->getId();
428
					$line .= ' <info>Name</info>: ' . $this->configService->displayFederatedUser(
429
							$member,
430
							$this->input->getOption('display-name')
431
						);
432
					$source = ($member->hasBasedOn()) ? $member->getBasedOn()->getSource() : '';
433
					$line .= ' <info>Source</info>: ' . Circle::$DEF_SOURCE[$source];
434
				}
435
436
				if ($lineNumber === 2) {
437
					if (is_null($circle)) {
438
						if ($member->getUserType() === Member::TYPE_CIRCLE) {
439
							$line .= '<comment>(out of bounds)</comment>';
440
						}
441
442
						return $line;
443
					}
444
					$owner = $circle->getOwner();
445
					$line .= '<info>Owner</info>: ' . $owner->getUserId() . '@' . $owner->getInstance();
446
					$type = implode(", ", Circle::getCircleFlags($circle, Circle::FLAGS_LONG));
447
					$line .= ($type === '') ? '' : ' <info>Config</info>: ' . $type;
448
				}
449
450
			} else {
451
				if ($lineNumber === 1 && !is_null($circle)) {
452
					$line .= '<info>' . $circle->getSingleId() . '</info>';
453
					if (!$this->configService->isLocalInstance($circle->getInstance())) {
454
						$line .= '@' . $circle->getInstance();
455
					}
456
				}
457
			}
458
459
			return $line;
460
461
		} catch (InvalidItemException | ItemNotFoundException | UnknownTypeException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
462
		}
463
464
		return '';
465
	}
466
467
}
468
469