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

CirclesList   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 161
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 8
dl 0
loc 161
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A configure() 0 11 1
B execute() 0 46 6
B getCircles() 0 22 7
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\RequestNetworkException;
36
use daita\MySmallPhpTools\Exceptions\SignatoryException;
37
use daita\MySmallPhpTools\Exceptions\SignatureException;
38
use daita\MySmallPhpTools\Traits\TArrayTools;
39
use OC\Core\Command\Base;
40
use OC\User\NoUserException;
41
use OCA\Circles\Exceptions\CircleNotFoundException;
42
use OCA\Circles\Exceptions\InitiatorNotFoundException;
43
use OCA\Circles\Exceptions\OwnerNotFoundException;
44
use OCA\Circles\Exceptions\RemoteNotFoundException;
45
use OCA\Circles\Exceptions\RemoteResourceNotFoundException;
46
use OCA\Circles\Exceptions\UnknownRemoteException;
47
use OCA\Circles\Model\Circle;
48
use OCA\Circles\Model\Member;
49
use OCA\Circles\Model\ModelManager;
50
use OCA\Circles\Service\CircleService;
51
use OCA\Circles\Service\ConfigService;
52
use OCA\Circles\Service\FederatedUserService;
53
use OCA\Circles\Service\RemoteService;
54
use Symfony\Component\Console\Helper\Table;
55
use Symfony\Component\Console\Input\InputArgument;
56
use Symfony\Component\Console\Input\InputInterface;
57
use Symfony\Component\Console\Input\InputOption;
58
use Symfony\Component\Console\Output\ConsoleOutput;
59
use Symfony\Component\Console\Output\OutputInterface;
60
61
62
/**
63
 * Class CirclesList
64
 *
65
 * @package OCA\Circles\Command
66
 */
67
class CirclesList extends Base {
68
69
70
	use TArrayTools;
71
72
73
	/** @var ModelManager */
74
	private $modelManager;
75
76
	/** @var FederatedUserService */
77
	private $federatedUserService;
78
79
	/** @var CircleService */
80
	private $circleService;
81
82
	/** @var RemoteService */
83
	private $remoteService;
84
85
	/** @var ConfigService */
86
	private $configService;
87
88
89
	/**
90
	 * CirclesList constructor.
91
	 *
92
	 * @param ModelManager $modelManager
93
	 * @param FederatedUserService $federatedUserService
94
	 * @param CircleService $circleService
95
	 * @param RemoteService $remoteService
96
	 * @param ConfigService $configService
97
	 */
98
	public function __construct(
99
		ModelManager $modelManager, FederatedUserService $federatedUserService, CircleService $circleService,
100
		RemoteService $remoteService, ConfigService $configService
101
	) {
102
		parent::__construct();
103
		$this->modelManager = $modelManager;
104
		$this->federatedUserService = $federatedUserService;
105
		$this->circleService = $circleService;
106
		$this->remoteService = $remoteService;
107
		$this->configService = $configService;
108
	}
109
110
111
	protected function configure() {
112
		parent::configure();
113
		$this->setName('circles:manage:list')
114
			 ->setDescription('listing current circles')
115
			 ->addArgument('remote', InputArgument::OPTIONAL, 'remote Nextcloud address', '')
116
			 ->addOption('member', '', InputOption::VALUE_REQUIRED, 'search for member', '')
117
			 ->addOption('def', '', InputOption::VALUE_NONE, 'display complete circle configuration')
118
			 ->addOption('all', '', InputOption::VALUE_NONE, 'display also hidden Circles')
119
			 ->addOption('initiator', '', InputOption::VALUE_REQUIRED, 'set an initiator to the request', '')
120
			 ->addOption('json', '', InputOption::VALUE_NONE, 'returns result as JSON');
121
	}
122
123
124
	/**
125
	 * @param InputInterface $input
126
	 * @param OutputInterface $output
127
	 *
128
	 * @return int
129
	 * @throws CircleNotFoundException
130
	 * @throws InvalidItemException
131
	 * @throws NoUserException
132
	 * @throws RemoteNotFoundException
133
	 * @throws RemoteResourceNotFoundException
134
	 * @throws RequestNetworkException
135
	 * @throws SignatoryException
136
	 * @throws SignatureException
137
	 * @throws UnknownRemoteException
138
	 * @throws InitiatorNotFoundException
139
	 * @throws OwnerNotFoundException
140
	 */
141
	protected function execute(InputInterface $input, OutputInterface $output): int {
142
		$member = $input->getOption('member');
143
		$json = $input->getOption('json');
144
		$remote = $input->getArgument('remote');
145
146
		$output = new ConsoleOutput();
147
		$output = $output->section();
148
149
		$this->federatedUserService->commandLineInitiator($input->getOption('initiator'), '', true);
150
151
		$filter = null;
152
		if ($member !== '') {
153
			$filter = $this->federatedUserService->createFilterMember($member);
154
		}
155
156
		$circles = $this->getCircles($filter, $remote, $input->getOption('all'));
157
158
		if ($json) {
159
			echo json_encode($circles, JSON_PRETTY_PRINT) . "\n";
160
161
			return 0;
162
		}
163
164
		$table = new Table($output);
165
		$table->setHeaders(['ID', 'Name', 'Type', 'Owner', 'Instance', 'Limit', 'Description']);
166
		$table->render();
167
168
		$local = $this->configService->getLocalInstance();
169
		$display = ($input->getOption('def') ? ModelManager::TYPES_LONG : ModelManager::TYPES_SHORT);
170
		foreach ($circles as $circle) {
171
			$owner = $circle->getOwner();
172
			$table->appendRow(
173
				[
174
					$circle->getId(),
175
					$circle->getName(),
176
					json_encode($this->modelManager->getCircleTypes($circle, $display)),
177
					$owner->getUserId(),
178
					($owner->getInstance() === $local) ? '' : $owner->getInstance(),
179
					$this->getInt('members_limit', $circle->getSettings(), -1),
180
					substr($circle->getDescription(), 0, 30)
181
				]
182
			);
183
		}
184
185
		return 0;
186
	}
187
188
189
	/**
190
	 * @param Member|null $filter
191
	 * @param string $remote
192
	 * @param bool $all
193
	 *
194
	 * @return Circle[]
195
	 * @throws InitiatorNotFoundException
196
	 * @throws InvalidItemException
197
	 * @throws RemoteNotFoundException
198
	 * @throws RemoteResourceNotFoundException
199
	 * @throws RequestNetworkException
200
	 * @throws SignatoryException
201
	 * @throws SignatureException
202
	 * @throws UnknownRemoteException
203
	 */
204
	private function getCircles(?Member $filter, string $remote, bool $all = false): array {
205
		if ($remote !== '') {
206
			$circles = $this->remoteService->getCircles($remote);
207
		} else {
208
			$circles = $this->circleService->getCircles($filter);
209
		}
210
211
		if ($all) {
212
			return $circles;
213
		}
214
215
		$filtered = [];
216
		foreach ($circles as $circle) {
217
			if (!$circle->isConfig(Circle::CFG_SINGLE)
218
				&& !$circle->isConfig(Circle::CFG_HIDDEN)
219
				&& !$circle->isConfig(Circle::CFG_BACKEND)) {
220
				$filtered[] = $circle;
221
			}
222
		}
223
224
		return $filtered;
225
	}
226
227
}
228
229