Completed
Pull Request — master (#551)
by Maxence
84:52
created

CirclesList   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

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

5 Methods

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