Completed
Push — master ( c57b95...1d3e11 )
by Maxence
30s queued 14s
created

MembersList::displayLeaf()   F

Complexity

Conditions 17
Paths 252

Size

Total Lines 61

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 61
rs 3.7333
c 0
b 0
f 0
cc 17
nc 252
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 daita\MySmallPhpTools\Exceptions\InvalidItemException;
35
use daita\MySmallPhpTools\Exceptions\ItemNotFoundException;
36
use daita\MySmallPhpTools\Exceptions\RequestNetworkException;
37
use daita\MySmallPhpTools\Exceptions\SignatoryException;
38
use daita\MySmallPhpTools\Exceptions\UnknownTypeException;
39
use daita\MySmallPhpTools\Model\Nextcloud\nc22\NC22TreeNode;
40
use daita\MySmallPhpTools\Model\SimpleDataStore;
41
use daita\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
	/**
108
	 * MembersList constructor.
109
	 *
110
	 * @param MemberRequest $memberRequest
111
	 * @param FederatedUserService $federatedUserService
112
	 * @param RemoteService $remoteService
113
	 * @param CircleService $circleService
114
	 * @param MemberService $memberService
115
	 * @param ConfigService $configService
116
	 */
117
	public function __construct(
118
		MemberRequest $memberRequest, FederatedUserService $federatedUserService,
119
		RemoteService $remoteService, CircleService $circleService, MemberService $memberService,
120
		ConfigService $configService
121
	) {
122
		parent::__construct();
123
124
		$this->memberRequest = $memberRequest;
125
		$this->federatedUserService = $federatedUserService;
126
		$this->remoteService = $remoteService;
127
		$this->circleService = $circleService;
128
		$this->memberService = $memberService;
129
		$this->configService = $configService;
130
	}
131
132
133
	protected function configure() {
134
		parent::configure();
135
		$this->setName('circles:members:list')
136
			 ->setDescription('listing Members from a Circle')
137
			 ->addArgument('circle_id', InputArgument::REQUIRED, 'ID of the circle')
138
			 ->addOption('instance', '', InputOption::VALUE_REQUIRED, 'Instance of the circle', '')
139
			 ->addOption('inherited', '', InputOption::VALUE_NONE, 'Display all inherited members')
140
			 ->addOption('initiator', '', InputOption::VALUE_REQUIRED, 'set an initiator to the request', '')
141
			 ->addOption('display-name', '', InputOption::VALUE_NONE, 'display the displayName')
142
			 ->addOption('tree', '', InputOption::VALUE_NONE, 'display members as a tree');
143
	}
144
145
146
	/**
147
	 * @param InputInterface $input
148
	 * @param OutputInterface $output
149
	 *
150
	 * @return int
151
	 * @throws CircleNotFoundException
152
	 * @throws FederatedUserException
153
	 * @throws FederatedUserNotFoundException
154
	 * @throws InitiatorNotFoundException
155
	 * @throws InvalidIdException
156
	 * @throws InvalidItemException
157
	 * @throws MemberNotFoundException
158
	 * @throws OwnerNotFoundException
159
	 * @throws RemoteInstanceException
160
	 * @throws RemoteNotFoundException
161
	 * @throws RemoteResourceNotFoundException
162
	 * @throws RequestNetworkException
163
	 * @throws SignatoryException
164
	 * @throws UnknownRemoteException
165
	 * @throws UserTypeNotFoundException
166
	 * @throws FederatedItemException
167
	 * @throws SingleCircleNotFoundException
168
	 * @throws RequestBuilderException
169
	 */
170
	protected function execute(InputInterface $input, OutputInterface $output): int {
171
		$this->input = $input;
172
		$circleId = $input->getArgument('circle_id');
173
		$instance = $input->getOption('instance');
174
		$initiator = $input->getOption('initiator');
175
		$inherited = $input->getOption('inherited');
176
177
		$tree = null;
178
		if ($input->getOption('tree')) {
179
			$this->federatedUserService->commandLineInitiator($initiator, $circleId, true);
180
			$circle = $this->circleService->getCircle($circleId);
181
182
			$output->writeln('<info>Name</info>: ' . $circle->getName());
183
			$owner = $circle->getOwner();
184
			$output->writeln('<info>Owner</info>: ' . $owner->getUserId() . '@' . $owner->getInstance());
185
			$type = implode(", ", Circle::getCircleFlags($circle, Circle::FLAGS_LONG));
186
			$output->writeln('<info>Config</info>: ' . $type);
187
			$output->writeln(' ');
188
189
			$tree = new NC22TreeNode(null, new SimpleDataStore(['circle' => $circle]));
190
			$inherited = false;
191
		}
192
193
		if ($inherited) {
194
			$this->federatedUserService->commandLineInitiator($initiator, $circleId, true);
195
			$circle = $this->circleService->getCircle($circleId);
196
			$members = $circle->getInheritedMembers(true);
197
		} else {
198
			$members = $this->getMembers($circleId, $instance, $initiator, $tree);
199
		}
200
201
		if (!is_null($tree)) {
202
			$this->drawTree(
203
				$tree, [$this, 'displayLeaf'],
204
				[
205
					'height'       => 3,
206
					'node-spacing' => 1,
207
					'item-spacing' => 0,
208
				]
209
			);
210
211
			return 0;
212
		}
213
214
		if (strtolower($input->getOption('output')) === 'json') {
215
			$output->writeln(json_encode($members, JSON_PRETTY_PRINT));
216
217
			return 0;
218
		}
219
220
		$output = new ConsoleOutput();
221
		$output = $output->section();
222
223
		$table = new Table($output);
224
		$table->setHeaders(
225
			[
226
				'Circle Id', 'Circle Name', 'Member Id', 'Single Id', 'Type', 'Source', 'Username',
227
				'Instance', 'Level'
228
			]
229
		);
230
		$table->render();
231
232
		$local = $this->configService->getFrontalInstance();
233
		foreach ($members as $member) {
234
			if ($member->getCircleId() === $circleId) {
235
				$level = $member->getLevel();
236
			} else {
237
				$level = $member->getInheritanceFrom()->getLevel();
238
			}
239
240
			$table->appendRow(
241
				[
242
					$member->getCircleId(),
243
					$member->getCircle()->getDisplayName(),
244
					$member->getId(),
245
					$member->getSingleId(),
246
					Member::$TYPE[$member->getUserType()],
247
					$member->hasBasedOn() ? Circle::$DEF_SOURCE[$member->getBasedOn()->getSource()] : '',
248
					($this->input->getOption('display-name')) ?
249
						$member->getBasedOn()->getDisplayName() : $member->getUserId(),
250
					($member->getInstance() === $local) ? '' : $member->getInstance(),
251
					($level > 0) ? Member::$DEF_LEVEL[$level] :
252
						'(' . strtolower($member->getStatus()) . ')'
253
				]
254
			);
255
		}
256
257
		return 0;
258
	}
259
260
261
	/**
262
	 * @param string $circleId
263
	 * @param string $instance
264
	 * @param string $initiator
265
	 * @param NC22TreeNode|null $tree
266
	 * @param array $knownIds
267
	 *
268
	 * @return array
269
	 * @throws CircleNotFoundException
270
	 * @throws FederatedItemException
271
	 * @throws FederatedUserException
272
	 * @throws FederatedUserNotFoundException
273
	 * @throws InitiatorNotFoundException
274
	 * @throws InvalidIdException
275
	 * @throws InvalidItemException
276
	 * @throws MemberNotFoundException
277
	 * @throws OwnerNotFoundException
278
	 * @throws RemoteInstanceException
279
	 * @throws RemoteNotFoundException
280
	 * @throws RemoteResourceNotFoundException
281
	 * @throws RequestNetworkException
282
	 * @throws SignatoryException
283
	 * @throws SingleCircleNotFoundException
284
	 * @throws UnknownRemoteException
285
	 * @throws UserTypeNotFoundException
286
	 * @throws RequestBuilderException
287
	 */
288
	private function getMembers(
289
		string $circleId,
290
		string $instance,
291
		string $initiator,
292
		?NC22TreeNode $tree,
293
		array $knownIds = []
294
	): array {
295
		if (in_array($circleId, $knownIds)) {
296
			return [];
297
		}
298
		$knownIds[] = $circleId;
299
300
		if ($instance !== '' && !$this->configService->isLocalInstance($instance)) {
301
			$data = [];
302
			if ($initiator) {
303
				$data['initiator'] = $this->federatedUserService->getFederatedUser($initiator);
304
			}
305
306
			try {
307
				$members = $this->remoteService->getMembersFromInstance($circleId, $instance, $data);
308
			} catch (RemoteInstanceException $e) {
309
				return [];
310
			}
311
		} else {
312
			$this->federatedUserService->commandLineInitiator($initiator, $circleId, true);
313
			$members = $this->memberService->getMembers($circleId);
314
		}
315
316
		if (!is_null($tree)) {
317
			foreach ($members as $member) {
318
				if ($member->getUserType() === Member::TYPE_CIRCLE) {
319
					if (!$this->configService->isLocalInstance($member->getInstance())) {
320
						$data = [];
321
						if ($initiator) {
322
							$data['initiator'] = $this->federatedUserService->getFederatedUser($initiator);
323
						}
324
325
						$circle = null;
326
						try {
327
							$circle = $this->remoteService->getCircleFromInstance(
328
								$member->getSingleId(), $member->getInstance(), $data
329
							);
330
						} 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...
331
						}
332
					} else {
333
						$this->federatedUserService->commandLineInitiator(
334
							$initiator, $member->getSingleId(), true
335
						);
336
						$circle = $this->circleService->getCircle($member->getSingleId(), 0);
337
					}
338
					$node = new NC22TreeNode(
339
						$tree, new SimpleDataStore(
340
								 [
341
									 'circle'  => $circle,
342
									 'member'  => $member,
343
									 'cycling' => in_array($member->getSingleId(), $knownIds),
344
								 ]
345
							 )
346
					);
347
348
					$this->getMembers(
349
						$member->getSingleId(), $member->getInstance(), $initiator, $node, $knownIds
350
					);
351
				} else {
352
					new NC22TreeNode(
353
						$tree, new SimpleDataStore(
354
								 [
355
									 'member'  => $member,
356
									 'cycling' => in_array($member->getSingleId(), $knownIds)
357
								 ]
358
							 )
359
					);
360
				}
361
			}
362
		}
363
364
		return $members;
365
	}
366
367
368
	/**
369
	 * @param SimpleDataStore $data
370
	 * @param int $lineNumber
371
	 *
372
	 * @return string
373
	 * @throws OwnerNotFoundException
374
	 */
375
	public function displayLeaf(SimpleDataStore $data, int $lineNumber): string {
376
		if ($lineNumber === 3) {
377
			return ($data->gBool('cycling')) ? '<comment>(loop detected)</comment>' : '';
378
		}
379
380
		try {
381
			$line = '';
382
			$circle = null;
383
			if ($data->hasKey('circle')) {
384
				/** @var Circle $circle */
385
				$circle = $data->gObj('circle', Circle::class);
386
			}
387
388
			if ($data->hasKey('member')) {
389
				/** @var Member $member */
390
				$member = $data->gObj('member', Member::class);
391
392
				if ($lineNumber === 1) {
393
					$line .= '<info>' . $member->getSingleId() . '</info>';
394
					if (!$this->configService->isLocalInstance($member->getInstance())) {
395
						$line .= '@' . $member->getInstance();
396
					}
397
					$line .= ' (' . Member::$DEF_LEVEL[$member->getLevel()] . ')';
398
399
					$name = ($this->input->getOption('display-name')) ?
400
						$member->getBasedOn()->getDisplayName() : $member->getUserId();
401
					$line .= ' <info>Name</info>: ' . $name;
402
					$source = ($member->hasBasedOn()) ? $member->getBasedOn()->getSource() : '';
403
					$line .= ' <info>Source</info>: ' . Circle::$DEF_SOURCE[$source];
404
				}
405
406
				if ($lineNumber === 2) {
407
					if (is_null($circle)) {
408
						if ($member->getUserType() === Member::TYPE_CIRCLE) {
409
							$line .= '<comment>(out of bounds)</comment>';
410
						}
411
412
						return $line;
413
					}
414
					$owner = $circle->getOwner();
415
					$line .= '<info>Owner</info>: ' . $owner->getUserId() . '@' . $owner->getInstance();
416
					$type = implode(", ", Circle::getCircleFlags($circle, Circle::FLAGS_LONG));
417
					$line .= ($type === '') ? '' : ' <info>Config</info>: ' . $type;
418
				}
419
420
			} else {
421
				if ($lineNumber === 1 && !is_null($circle)) {
422
					$line .= '<info>' . $circle->getSingleId() . '</info>';
423
					if (!$this->configService->isLocalInstance($circle->getInstance())) {
424
						$line .= '@' . $circle->getInstance();
425
					}
426
				}
427
			}
428
429
			return $line;
430
431
		} 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...
432
		}
433
434
		return '';
435
	}
436
437
}
438
439