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

CirclesList::execute()   A

Complexity

Conditions 5
Paths 10

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

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