Completed
Push — master ( 3ddca8...414a73 )
by Maxence
02:57
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\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 View Code Duplication
	public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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('member', '', InputOption::VALUE_REQUIRED, 'search for member', '')
128
			 ->addOption('def', '', InputOption::VALUE_NONE, 'display complete circle configuration')
129
			 ->addOption('display-name', '', InputOption::VALUE_NONE, 'display the displayName')
130
			 ->addOption('all', '', InputOption::VALUE_NONE, 'display also hidden Circles');
131
	}
132
133
134
	/**
135
	 * @param InputInterface $input
136
	 * @param OutputInterface $output
137
	 *
138
	 * @return int
139
	 * @throws CircleNotFoundException
140
	 * @throws FederatedUserException
141
	 * @throws FederatedUserNotFoundException
142
	 * @throws InitiatorNotFoundException
143
	 * @throws InvalidIdException
144
	 * @throws OwnerNotFoundException
145
	 * @throws RemoteInstanceException
146
	 * @throws RemoteNotFoundException
147
	 * @throws RemoteResourceNotFoundException
148
	 * @throws RequestNetworkException
149
	 * @throws SignatoryException
150
	 * @throws UnknownRemoteException
151
	 * @throws UserTypeNotFoundException
152
	 * @throws FederatedItemException
153
	 * @throws MemberNotFoundException
154
	 * @throws SingleCircleNotFoundException
155
	 * @throws RequestBuilderException
156
	 */
157
	protected function execute(InputInterface $input, OutputInterface $output): int {
158
		$this->input = $input;
159
		$member = $input->getOption('member');
160
		$instance = $input->getOption('instance');
161
		$initiator = $input->getOption('initiator');
162
163
		$filterMember = null;
164
		if ($member !== '') {
165
			$filterMember = $this->federatedUserService->getFederatedMember($member);
166
		}
167
168
		if (!$this->configService->isLocalInstance($instance)) {
169
			$data = ['filterMember' => $filterMember];
170
			if ($initiator) {
171
				$data['initiator'] = $this->federatedUserService->getFederatedUser(
172
					$initiator,
173
					Member::TYPE_USER
174
				);
175
			}
176
177
			$circles = $this->remoteService->getCirclesFromInstance($instance, $data);
178
		} else {
179
			$this->federatedUserService->commandLineInitiator($initiator, '', true);
180
			$params = new SimpleDataStore(['includeSystemCircles' => $input->getOption('all')]);
181
			$circles = $this->circleService->getCircles(null, $filterMember, $params);
182
		}
183
184
		if (strtolower($input->getOption('output')) === 'json') {
185
			$output->writeln(json_encode($circles, JSON_PRETTY_PRINT));
186
187
			return 0;
188
		}
189
190
		$this->displayCircles($circles);
191
192
		return 0;
193
	}
194
195
196
	/**
197
	 * @param Circle[] $circles
198
	 */
199
	private function displayCircles(array $circles): void {
200
		$output = new ConsoleOutput();
201
		$output = $output->section();
202
		$table = new Table($output);
203
		$table->setHeaders(
204
			['Single Id', 'Name', 'Config', 'Source', 'Owner', 'Instance', 'Limit', 'Description']
205
		);
206
		$table->render();
207
208
		$display = ($this->input->getOption('def') ? Circle::FLAGS_LONG : Circle::FLAGS_SHORT);
209
		foreach ($circles as $circle) {
210
			$owner = $circle->getOwner();
211
			$table->appendRow(
212
				[
213
					$circle->getSingleId(),
214
					$this->input->getOption('display-name') ? $circle->getDisplayName() : $circle->getName(),
215
					json_encode(Circle::getCircleFlags($circle, $display)),
216
					Circle::$DEF_SOURCE[$circle->getSource()],
217
					$owner->getUserId(),
218
					$this->configService->displayInstance($owner->getInstance()),
219
					$this->getInt('members_limit', $circle->getSettings(), -1),
220
					substr(str_replace("\n", ' ', $circle->getDescription()), 0, 30)
221
				]
222
			);
223
		}
224
	}
225
226
}
227
228