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

CirclesList::getCircles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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