Completed
Pull Request — master (#551)
by Maxence
02:35
created

MembersList   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 111
Duplicated Lines 9.01 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 5
dl 10
loc 111
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 10 10 1
A configure() 0 9 1
B execute() 0 46 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\RequestNetworkException;
35
use daita\MySmallPhpTools\Exceptions\SignatoryException;
36
use OC\Core\Command\Base;
37
use OC\User\NoUserException;
38
use OCA\Circles\Exceptions\CircleNotFoundException;
39
use OCA\Circles\Exceptions\InitiatorNotFoundException;
40
use OCA\Circles\Exceptions\OwnerNotFoundException;
41
use OCA\Circles\Exceptions\RemoteInstanceException;
42
use OCA\Circles\Exceptions\RemoteNotFoundException;
43
use OCA\Circles\Exceptions\RemoteResourceNotFoundException;
44
use OCA\Circles\Exceptions\UnknownRemoteException;
45
use OCA\Circles\Model\Member;
46
use OCA\Circles\Service\ConfigService;
47
use OCA\Circles\Service\FederatedUserService;
48
use OCA\Circles\Service\MemberService;
49
use OCA\Circles\Service\RemoteService;
50
use Symfony\Component\Console\Helper\Table;
51
use Symfony\Component\Console\Input\InputArgument;
52
use Symfony\Component\Console\Input\InputInterface;
53
use Symfony\Component\Console\Input\InputOption;
54
use Symfony\Component\Console\Output\ConsoleOutput;
55
use Symfony\Component\Console\Output\OutputInterface;
56
57
58
/**
59
 * Class MembersList
60
 *
61
 * @package OCA\Circles\Command
62
 */
63
class MembersList extends Base {
64
65
66
	/** @var FederatedUserService */
67
	private $federatedUserService;
68
69
	/** @var RemoteService */
70
	private $remoteService;
71
72
	/** @var MemberService */
73
	private $memberService;
74
75
	/** @var ConfigService */
76
	private $configService;
77
78
79
	/**
80
	 * MembersList constructor.
81
	 *
82
	 * @param FederatedUserService $federatedUserService
83
	 * @param RemoteService $remoteService
84
	 * @param MemberService $memberService
85
	 * @param ConfigService $configService
86
	 */
87 View Code Duplication
	public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
88
		FederatedUserService $federatedUserService, RemoteService $remoteService,
89
		MemberService $memberService, ConfigService $configService
90
	) {
91
		parent::__construct();
92
		$this->federatedUserService = $federatedUserService;
93
		$this->remoteService = $remoteService;
94
		$this->memberService = $memberService;
95
		$this->configService = $configService;
96
	}
97
98
99
	protected function configure() {
100
		parent::configure();
101
		$this->setName('circles:members:list')
102
			 ->setDescription('listing Members from a Circle')
103
			 ->addArgument('circle_id', InputArgument::REQUIRED, 'ID of the circle')
104
			 ->addOption('instance', '', InputOption::VALUE_REQUIRED, 'Instance of the circle', '')
105
			 ->addOption('initiator', '', InputOption::VALUE_REQUIRED, 'set an initiator to the request', '')
106
			 ->addOption('json', '', InputOption::VALUE_NONE, 'returns result as JSON');
107
	}
108
109
110
	/**
111
	 * @param InputInterface $input
112
	 * @param OutputInterface $output
113
	 *
114
	 * @return int
115
	 * @throws CircleNotFoundException
116
	 * @throws InitiatorNotFoundException
117
	 * @throws NoUserException
118
	 * @throws OwnerNotFoundException
119
	 * @throws RemoteInstanceException
120
	 * @throws RemoteNotFoundException
121
	 * @throws RemoteResourceNotFoundException
122
	 * @throws UnknownRemoteException
123
	 * @throws RequestNetworkException
124
	 * @throws SignatoryException
125
	 */
126
	protected function execute(InputInterface $input, OutputInterface $output): int {
127
		$circleId = $input->getArgument('circle_id');
128
		$instance = $input->getOption('instance');
129
		$initiator = $input->getOption('initiator');
130
131
		if ($instance !== '' && !$this->configService->isLocalInstance($instance)) {
132
			$data = [];
133
			if ($initiator) {
134
				$data['initiator'] = $this->federatedUserService->getFederatedUser($initiator);
135
			}
136
137
			$members = $this->remoteService->getMembersFromInstance($circleId, $instance, $data);
138
		} else {
139
			$this->federatedUserService->commandLineInitiator($initiator, $circleId, true);
140
			$members = $this->memberService->getMembers($circleId);
141
		}
142
143
144
		if ($input->getOption('json')) {
145
			echo json_encode($members, JSON_PRETTY_PRINT) . "\n";
146
147
			return 0;
148
		}
149
150
		$output = new ConsoleOutput();
151
		$output = $output->section();
152
153
		$table = new Table($output);
154
		$table->setHeaders(['ID', 'Single ID', 'Username', 'Instance', 'Level']);
155
		$table->render();
156
157
		$local = $this->configService->getLocalInstance();
158
		foreach ($members as $member) {
159
			$table->appendRow(
160
				[
161
					$member->getId(),
162
					$member->getSingleId(),
163
					$member->getUserId(),
164
					($member->getInstance() === $local) ? '' : $member->getInstance(),
165
					Member::$DEF_LEVEL[$member->getLevel()]
166
				]
167
			);
168
		}
169
170
		return 0;
171
	}
172
173
}
174
175