Completed
Pull Request — master (#551)
by Maxence
03:07
created

CirclesList::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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