Completed
Push — master ( 6a3d7a...383329 )
by Maxence
03:05 queued 10s
created

CirclesList::displayCircles()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.504
c 0
b 0
f 0
cc 4
nc 4
nop 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\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\RequestBuilderException;
51
use OCA\Circles\Exceptions\SingleCircleNotFoundException;
52
use OCA\Circles\Exceptions\UnknownRemoteException;
53
use OCA\Circles\Exceptions\UserTypeNotFoundException;
54
use OCA\Circles\Model\Circle;
55
use OCA\Circles\Model\Member;
56
use OCA\Circles\Model\ModelManager;
57
use OCA\Circles\Service\CircleService;
58
use OCA\Circles\Service\ConfigService;
59
use OCA\Circles\Service\FederatedUserService;
60
use OCA\Circles\Service\RemoteService;
61
use Symfony\Component\Console\Helper\Table;
62
use Symfony\Component\Console\Input\InputInterface;
63
use Symfony\Component\Console\Input\InputOption;
64
use Symfony\Component\Console\Output\ConsoleOutput;
65
use Symfony\Component\Console\Output\OutputInterface;
66
67
68
/**
69
 * Class CirclesList
70
 *
71
 * @package OCA\Circles\Command
72
 */
73
class CirclesList extends Base {
74
75
76
	use TArrayTools;
77
78
79
	/** @var ModelManager */
80
	private $modelManager;
81
82
	/** @var FederatedUserService */
83
	private $federatedUserService;
84
85
	/** @var RemoteService */
86
	private $remoteService;
87
88
	/** @var CircleService */
89
	private $circleService;
90
91
	/** @var ConfigService */
92
	private $configService;
93
94
95
	/** @var InputInterface */
96
	private $input;
97
98
99
	/**
100
	 * CirclesList constructor.
101
	 *
102
	 * @param ModelManager $modelManager
103
	 * @param FederatedUserService $federatedUserService
104
	 * @param RemoteService $remoteService
105
	 * @param CircleService $circleService
106
	 * @param ConfigService $configService
107
	 */
108
	public function __construct(
109
		ModelManager $modelManager, FederatedUserService $federatedUserService, RemoteService $remoteService,
110
		CircleService $circleService, ConfigService $configService
111
	) {
112
		parent::__construct();
113
		$this->modelManager = $modelManager;
114
		$this->federatedUserService = $federatedUserService;
115
		$this->remoteService = $remoteService;
116
		$this->circleService = $circleService;
117
		$this->configService = $configService;
118
	}
119
120
121
	protected function configure() {
122
		parent::configure();
123
		$this->setName('circles:manage:list')
124
			 ->setDescription('listing current circles')
125
			 ->addOption('instance', '', InputOption::VALUE_REQUIRED, 'Instance of the circle', '')
126
			 ->addOption('initiator', '', InputOption::VALUE_REQUIRED, 'set an initiator to the request', '')
127
			 ->addOption('initiator-type', '', InputOption::VALUE_REQUIRED, 'set initiator type', '0')
128
			 ->addOption('member', '', InputOption::VALUE_REQUIRED, 'search for member', '')
129
			 ->addOption('def', '', InputOption::VALUE_NONE, 'display complete circle configuration')
130
			 ->addOption('display-name', '', InputOption::VALUE_NONE, 'display the displayName')
131
			 ->addOption('all', '', InputOption::VALUE_NONE, 'display also hidden Circles');
132
	}
133
134
135
	/**
136
	 * @param InputInterface $input
137
	 * @param OutputInterface $output
138
	 *
139
	 * @return int
140
	 * @throws CircleNotFoundException
141
	 * @throws FederatedUserException
142
	 * @throws FederatedUserNotFoundException
143
	 * @throws InitiatorNotFoundException
144
	 * @throws InvalidIdException
145
	 * @throws OwnerNotFoundException
146
	 * @throws RemoteInstanceException
147
	 * @throws RemoteNotFoundException
148
	 * @throws RemoteResourceNotFoundException
149
	 * @throws RequestNetworkException
150
	 * @throws SignatoryException
151
	 * @throws UnknownRemoteException
152
	 * @throws UserTypeNotFoundException
153
	 * @throws FederatedItemException
154
	 * @throws MemberNotFoundException
155
	 * @throws SingleCircleNotFoundException
156
	 * @throws RequestBuilderException
157
	 */
158
	protected function execute(InputInterface $input, OutputInterface $output): int {
159
		$this->input = $input;
160
		$member = $input->getOption('member');
161
		$instance = $input->getOption('instance');
162
		$initiator = $input->getOption('initiator');
163
164
		$filterMember = null;
165
		if ($member !== '') {
166
			$filterMember = $this->federatedUserService->getFederatedMember($member);
167
		}
168
169
		if (!$this->configService->isLocalInstance($instance)) {
170
			$data = ['filterMember' => $filterMember];
171
			if ($initiator) {
172
				$data['initiator'] = $this->federatedUserService->getFederatedUser(
173
					$initiator,
174
					Member::parseTypeString($input->getOption('initiator-type')),
175
				);
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected ')'
Loading history...
176
			}
177
178
			$circles = $this->remoteService->getCirclesFromInstance($instance, $data);
179
		} else {
180
			$this->federatedUserService->commandLineInitiator(
181
				$initiator,
182
				Member::parseTypeString($input->getOption('initiator-type')),
183
				'',
184
				true
185
			);
186
187
			$params = new SimpleDataStore(['includeSystemCircles' => $input->getOption('all')]);
188
			$circles = $this->circleService->getCircles(null, $filterMember, $params);
189
		}
190
191
		if (strtolower($input->getOption('output')) === 'json') {
192
			$output->writeln(json_encode($circles, JSON_PRETTY_PRINT));
193
194
			return 0;
195
		}
196
197
		$this->displayCircles($circles);
198
199
		return 0;
200
	}
201
202
203
	/**
204
	 * @param Circle[] $circles
205
	 */
206
	private function displayCircles(array $circles): void {
207
		$output = new ConsoleOutput();
208
		$output = $output->section();
209
		$table = new Table($output);
210
		$table->setHeaders(
211
			['Single Id', 'Name', 'Config', 'Source', 'Owner', 'Instance', 'Limit', 'Description']
212
		);
213
		$table->render();
214
215
		$display = ($this->input->getOption('def') ? Circle::FLAGS_LONG : Circle::FLAGS_SHORT);
216
		foreach ($circles as $circle) {
217
			$owner = $circle->getOwner();
218
			$table->appendRow(
219
				[
220
					$circle->getSingleId(),
221
					$this->input->getOption('display-name') ? $circle->getDisplayName() : $circle->getName(),
222
					json_encode(Circle::getCircleFlags($circle, $display)),
223
					Circle::$DEF_SOURCE[$circle->getSource()],
224
					$owner->getUserId(),
225
					$this->configService->displayInstance($owner->getInstance()),
226
					$this->getInt('members_limit', $circle->getSettings(), -1),
227
					substr(str_replace("\n", ' ', $circle->getDescription()), 0, 30)
228
				]
229
			);
230
		}
231
	}
232
233
}
234
235