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

CirclesList   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 8
dl 0
loc 151
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 34 6
A displayCircles() 0 27 5
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 daita\MySmallPhpTools\Model\SimpleDataStore;
37
use daita\MySmallPhpTools\Traits\TArrayTools;
38
use OC\Core\Command\Base;
39
use OCA\Circles\Exceptions\CircleNotFoundException;
40
use OCA\Circles\Exceptions\FederatedItemException;
41
use OCA\Circles\Exceptions\FederatedUserException;
42
use OCA\Circles\Exceptions\FederatedUserNotFoundException;
43
use OCA\Circles\Exceptions\InitiatorNotFoundException;
44
use OCA\Circles\Exceptions\InvalidIdException;
45
use OCA\Circles\Exceptions\MemberNotFoundException;
46
use OCA\Circles\Exceptions\OwnerNotFoundException;
47
use OCA\Circles\Exceptions\RemoteInstanceException;
48
use OCA\Circles\Exceptions\RemoteNotFoundException;
49
use OCA\Circles\Exceptions\RemoteResourceNotFoundException;
50
use OCA\Circles\Exceptions\SingleCircleNotFoundException;
51
use OCA\Circles\Exceptions\UnknownRemoteException;
52
use OCA\Circles\Exceptions\UserTypeNotFoundException;
53
use OCA\Circles\Model\Circle;
54
use OCA\Circles\Model\ModelManager;
55
use OCA\Circles\Service\CircleService;
56
use OCA\Circles\Service\ConfigService;
57
use OCA\Circles\Service\FederatedUserService;
58
use OCA\Circles\Service\RemoteService;
59
use Symfony\Component\Console\Helper\Table;
60
use Symfony\Component\Console\Input\InputInterface;
61
use Symfony\Component\Console\Input\InputOption;
62
use Symfony\Component\Console\Output\ConsoleOutput;
63
use Symfony\Component\Console\Output\OutputInterface;
64
65
66
/**
67
 * Class CirclesList
68
 *
69
 * @package OCA\Circles\Command
70
 */
71
class CirclesList extends Base {
72
73
74
	use TArrayTools;
75
76
77
	/** @var ModelManager */
78
	private $modelManager;
79
80
	/** @var FederatedUserService */
81
	private $federatedUserService;
82
83
	/** @var RemoteService */
84
	private $remoteService;
85
86
	/** @var CircleService */
87
	private $circleService;
88
89
	/** @var ConfigService */
90
	private $configService;
91
92
93
	/** @var InputInterface */
94
	private $input;
95
96
97
	/**
98
	 * CirclesList constructor.
99
	 *
100
	 * @param ModelManager $modelManager
101
	 * @param FederatedUserService $federatedUserService
102
	 * @param RemoteService $remoteService
103
	 * @param CircleService $circleService
104
	 * @param ConfigService $configService
105
	 */
106
	public function __construct(
107
		ModelManager $modelManager, FederatedUserService $federatedUserService, RemoteService $remoteService,
108
		CircleService $circleService, ConfigService $configService
109
	) {
110
		parent::__construct();
111
		$this->modelManager = $modelManager;
112
		$this->federatedUserService = $federatedUserService;
113
		$this->remoteService = $remoteService;
114
		$this->circleService = $circleService;
115
		$this->configService = $configService;
116
	}
117
118
119
	protected function configure() {
120
		parent::configure();
121
		$this->setName('circles:manage:list')
122
			 ->setDescription('listing current circles')
123
			 ->addOption('instance', '', InputOption::VALUE_REQUIRED, 'Instance of the circle', '')
124
			 ->addOption('initiator', '', InputOption::VALUE_REQUIRED, 'set an initiator to the request', '')
125
			 ->addOption('member', '', InputOption::VALUE_REQUIRED, 'search for member', '')
126
			 ->addOption('def', '', InputOption::VALUE_NONE, 'display complete circle configuration')
127
			 ->addOption('display-name', '', InputOption::VALUE_NONE, 'display the displayName')
128
			 ->addOption('all', '', InputOption::VALUE_NONE, 'display also hidden Circles');
129
	}
130
131
132
	/**
133
	 * @param InputInterface $input
134
	 * @param OutputInterface $output
135
	 *
136
	 * @return int
137
	 * @throws CircleNotFoundException
138
	 * @throws FederatedUserException
139
	 * @throws FederatedUserNotFoundException
140
	 * @throws InitiatorNotFoundException
141
	 * @throws InvalidIdException
142
	 * @throws OwnerNotFoundException
143
	 * @throws RemoteInstanceException
144
	 * @throws RemoteNotFoundException
145
	 * @throws RemoteResourceNotFoundException
146
	 * @throws RequestNetworkException
147
	 * @throws SignatoryException
148
	 * @throws UnknownRemoteException
149
	 * @throws UserTypeNotFoundException
150
	 * @throws FederatedItemException
151
		 * @throws MemberNotFoundException
152
	 * @throws SingleCircleNotFoundException
153
	 */
154
	protected function execute(InputInterface $input, OutputInterface $output): int {
155
		$this->input = $input;
156
		$member = $input->getOption('member');
157
		$instance = $input->getOption('instance');
158
		$initiator = $input->getOption('initiator');
159
160
		$filterMember = null;
161
		if ($member !== '') {
162
			$filterMember = $this->federatedUserService->getFederatedMember($member);
163
		}
164
165
		if ($instance !== '' && !$this->configService->isLocalInstance($instance)) {
166
			$data = ['filterMember' => $filterMember];
167
			if ($initiator) {
168
				$data['initiator'] = $this->federatedUserService->getFederatedUser($initiator);
169
			}
170
171
			$circles = $this->remoteService->getCirclesFromInstance($instance, $data);
172
		} else {
173
			$this->federatedUserService->commandLineInitiator($initiator, '', true);
174
			$params = new SimpleDataStore(['includeSystemCircles' => $input->getOption('all')]);
175
			$circles = $this->circleService->getCircles(null, $filterMember, $params);
176
		}
177
178
		if (strtolower($input->getOption('output')) === 'json') {
179
			$output->writeln(json_encode($circles, JSON_PRETTY_PRINT));
180
181
			return 0;
182
		}
183
184
		$this->displayCircles($circles);
185
186
		return 0;
187
	}
188
189
190
	/**
191
	 * @param Circle[] $circles
192
	 */
193
	private function displayCircles(array $circles): void {
194
		$output = new ConsoleOutput();
195
		$output = $output->section();
196
		$table = new Table($output);
197
		$table->setHeaders(
198
			['Single Id', 'Name', 'Config', 'Source', 'Owner', 'Instance', 'Limit', 'Description']
199
		);
200
		$table->render();
201
202
		$local = $this->configService->getFrontalInstance();
203
		$display = ($this->input->getOption('def') ? Circle::FLAGS_LONG : Circle::FLAGS_SHORT);
204
		foreach ($circles as $circle) {
205
			$owner = $circle->getOwner();
206
			$table->appendRow(
207
				[
208
					$circle->getSingleId(),
209
					$this->input->getOption('display-name') ? $circle->getDisplayName() : $circle->getName(),
210
					json_encode(Circle::getCircleFlags($circle, $display)),
211
					Circle::$DEF_SOURCE[$circle->getSource()],
212
					$owner->getUserId(),
213
					($owner->getInstance() === $local) ? '' : $owner->getInstance(),
214
					$this->getInt('members_limit', $circle->getSettings(), -1),
215
					substr($circle->getDescription(), 0, 30)
216
				]
217
			);
218
		}
219
	}
220
221
}
222
223