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

CirclesList::execute()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 8.4468
c 0
b 0
f 0
cc 6
nc 10
nop 2

How to fix   Long Method   

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 declare(strict_types=1);
2
3
4
/**
5
 * Circles - Bring cloud-users closer together.
6
 *
7
 * This file is licensed under the Affero General Public License version 3 or
8
 * later. See the COPYING file.
9
 *
10
 * @author Maxence Lange <[email protected]>
11
 * @copyright 2017
12
 * @license GNU AGPL version 3 or any later version
13
 *
14
 * This program is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License
25
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
 *
27
 */
28
29
30
namespace OCA\Circles\Command;
31
32
use daita\MySmallPhpTools\Exceptions\InvalidItemException;
33
use daita\MySmallPhpTools\Exceptions\RequestNetworkException;
34
use daita\MySmallPhpTools\Exceptions\SignatoryException;
35
use daita\MySmallPhpTools\Exceptions\SignatureException;
36
use daita\MySmallPhpTools\Traits\TArrayTools;
37
use OC\Core\Command\Base;
38
use OCA\Circles\Exceptions\RemoteNotFoundException;
39
use OCA\Circles\Exceptions\RemoteResourceNotFoundException;
40
use OCA\Circles\Exceptions\ViewerNotFoundException;
41
use OCA\Circles\Model\Circle;
42
use OCA\Circles\Model\Member;
43
use OCA\Circles\Model\ModelManager;
44
use OCA\Circles\Service\CircleService;
45
use OCA\Circles\Service\ConfigService;
46
use OCA\Circles\Service\CurrentUserService;
47
use OCA\Circles\Service\RemoteService;
48
use Symfony\Component\Console\Helper\Table;
49
use Symfony\Component\Console\Input\InputArgument;
50
use Symfony\Component\Console\Input\InputInterface;
51
use Symfony\Component\Console\Input\InputOption;
52
use Symfony\Component\Console\Output\ConsoleOutput;
53
use Symfony\Component\Console\Output\OutputInterface;
54
55
56
/**
57
 * Class CirclesList
58
 *
59
 * @package OCA\Circles\Command
60
 */
61
class CirclesList extends Base {
62
63
64
	use TArrayTools;
65
66
67
	/** @var ModelManager */
68
	private $modelManager;
69
70
	/** @var CurrentUserService */
71
	private $currentUserService;
72
73
	/** @var CircleService */
74
	private $circleService;
75
76
	/** @var RemoteService */
77
	private $remoteService;
78
79
	/** @var ConfigService */
80
	private $configService;
81
82
83
	/**
84
	 * CirclesList constructor.
85
	 *
86
	 * @param ModelManager $modelManager
87
	 * @param CurrentUserService $currentUserService
88
	 * @param CircleService $circleService
89
	 * @param RemoteService $remoteService
90
	 * @param ConfigService $configService
91
	 */
92
	public function __construct(
93
		ModelManager $modelManager, CurrentUserService $currentUserService, CircleService $circleService,
94
		RemoteService $remoteService, ConfigService $configService
95
	) {
96
		parent::__construct();
97
		$this->modelManager = $modelManager;
98
		$this->currentUserService = $currentUserService;
99
		$this->circleService = $circleService;
100
		$this->remoteService = $remoteService;
101
		$this->configService = $configService;
102
103
		$this->currentUserService->bypassCurrentUserCondition(true);
104
	}
105
106
107
	protected function configure() {
108
		parent::configure();
109
		$this->setName('circles:manage:list')
110
			 ->setDescription('listing current circles')
111
			 ->addArgument('owner', InputArgument::OPTIONAL, 'filter by owner', '')
112
			 ->addOption('level', '', InputOption::VALUE_REQUIRED, 'level of membership', Member::LEVEL_OWNER)
113
			 ->addOption('def', '', InputOption::VALUE_NONE, 'display complete circle configuration')
114
			 ->addOption('all', '', InputOption::VALUE_NONE, 'display also hidden Circles')
115
			 ->addOption('viewer', '', InputOption::VALUE_REQUIRED, 'set viewer', '')
116
			 ->addOption('json', '', InputOption::VALUE_NONE, 'returns result as JSON')
117
			 ->addOption('remote', '', InputOption::VALUE_REQUIRED, 'remote Nextcloud address', '');
118
	}
119
120
121
	/**
122
	 * @param InputInterface $input
123
	 * @param OutputInterface $output
124
	 *
125
	 * @return int
126
	 * @throws InvalidItemException
127
	 * @throws RemoteNotFoundException
128
	 * @throws RemoteResourceNotFoundException
129
	 * @throws RequestNetworkException
130
	 * @throws SignatoryException
131
	 * @throws SignatureException
132
	 * @throws ViewerNotFoundException
133
	 */
134
	protected function execute(InputInterface $input, OutputInterface $output): int {
135
		$owner = $input->getArgument('owner');
136
		$level = $input->getOption('level');
137
		$viewer = $input->getOption('viewer');
138
		$json = $input->getOption('json');
139
		$remote = $input->getOption('remote');
140
141
		$output = new ConsoleOutput();
142
		$output = $output->section();
143
144
		$filter = null;
145
		if ($owner !== '') {
146
			$filter = new Member($owner, Member::TYPE_USER, '');
147
			$filter->setLevel((int)$level);
148
		}
149
150
		$circles = $this->getCircles($filter, $viewer, $remote);
151
152
		if ($json) {
153
			echo json_encode($circles, JSON_PRETTY_PRINT) . "\n";
154
155
			return 0;
156
		}
157
158
		$table = new Table($output);
159
		$table->setHeaders(['ID', 'Name', 'Type', 'Owner', 'Instance', 'Limit', 'Description']);
160
		$table->render();
161
162
		$local = $this->configService->getLocalInstance();
163
		$display = ($input->getOption('def') ? ModelManager::TYPES_LONG : ModelManager::TYPES_SHORT);
164
		foreach ($circles as $circle) {
165
//			if ($circle->isHidden() && !$input->getOption('all')) {
166
//				continue;
167
//			}
168
169
			$owner = $circle->getOwner();
170
			$table->appendRow(
171
				[
172
					$circle->getId(),
173
					$circle->getName(),
174
					json_encode($this->modelManager->getCircleTypes($circle, $display)),
175
					$owner->getUserId(),
176
					($owner->getInstance() === $local) ? '' : $owner->getInstance(),
177
					$this->getInt('members_limit', $circle->getSettings(), -1),
178
					substr($circle->getDescription(), 0, 30)
179
				]
180
			);
181
		}
182
183
		return 0;
184
	}
185
186
187
	/**
188
	 * @param Member|null $filter
189
	 * @param string $viewer
190
	 * @param string $remote
191
	 *
192
	 * @return Circle[]
193
	 * @throws InvalidItemException
194
	 * @throws RemoteNotFoundException
195
	 * @throws RemoteResourceNotFoundException
196
	 * @throws RequestNetworkException
197
	 * @throws SignatoryException
198
	 * @throws SignatureException
199
	 * @throws ViewerNotFoundException
200
	 */
201
	private function getCircles(?Member $filter, string $viewer, string $remote): array {
202
		if ($viewer !== '') {
203
			$this->currentUserService->setLocalViewer($viewer);
204
		}
205
		if ($remote !== '') {
206
			return $this->remoteService->getCircles($remote);
207
		}
208
209
		return $this->circleService->getCircles($filter);
210
	}
211
212
}
213
214