Completed
Pull Request — master (#546)
by Maxence
01:58
created

CirclesList::execute()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 50
rs 8.4686
c 0
b 0
f 0
cc 6
nc 10
nop 2
1
<?php declare(strict_types=1);
2
3
4
/**
5
 * Circles - Bring cloud-users closer together.
6
 *
7
 * This file is licensed under the Affero General Public License version 3 or
8
 * later. See the COPYING file.
9
 *
10
 * @author Maxence Lange <[email protected]>
11
 * @copyright 2017
12
 * @license GNU AGPL version 3 or any later version
13
 *
14
 * This program is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License
25
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
 *
27
 */
28
29
30
namespace OCA\Circles\Command;
31
32
use daita\MySmallPhpTools\Exceptions\InvalidItemException;
33
use daita\MySmallPhpTools\Exceptions\RequestNetworkException;
34
use daita\MySmallPhpTools\Exceptions\SignatoryException;
35
use daita\MySmallPhpTools\Exceptions\SignatureException;
36
use daita\MySmallPhpTools\Traits\TArrayTools;
37
use OC\Core\Command\Base;
38
use OCA\Circles\Exceptions\RemoteNotFoundException;
39
use OCA\Circles\Exceptions\RemoteResourceNotFoundException;
40
use OCA\Circles\Model\Circle;
41
use OCA\Circles\Model\Member;
42
use OCA\Circles\Model\ModelManager;
43
use OCA\Circles\Service\CircleService;
44
use OCA\Circles\Service\ConfigService;
45
use OCA\Circles\Service\RemoteService;
46
use Symfony\Component\Console\Helper\Table;
47
use Symfony\Component\Console\Input\InputArgument;
48
use Symfony\Component\Console\Input\InputInterface;
49
use Symfony\Component\Console\Input\InputOption;
50
use Symfony\Component\Console\Output\ConsoleOutput;
51
use Symfony\Component\Console\Output\OutputInterface;
52
53
54
/**
55
 * Class CirclesList
56
 *
57
 * @package OCA\Circles\Command
58
 */
59
class CirclesList extends Base {
60
61
62
	use TArrayTools;
63
64
65
	/** @var ModelManager */
66
	private $modelManager;
67
68
	/** @var CircleService */
69
	private $circleService;
70
71
	/** @var RemoteService */
72
	private $remoteService;
73
74
	/** @var ConfigService */
75
	private $configService;
76
77
78
	/**
79
	 * CirclesList constructor.
80
	 *
81
	 * @param ModelManager $modelManager
82
	 * @param CircleService $circleService
83
	 * @param RemoteService $remoteService
84
	 * @param ConfigService $configService
85
	 */
86
	public function __construct(
87
		ModelManager $modelManager, CircleService $circleService, RemoteService $remoteService,
88
		ConfigService $configService
89
	) {
90
		parent::__construct();
91
		$this->modelManager = $modelManager;
92
		$this->circleService = $circleService;
93
		$this->remoteService = $remoteService;
94
		$this->configService = $configService;
95
	}
96
97
98
	protected function configure() {
99
		parent::configure();
100
		$this->setName('circles:manage:list')
101
			 ->setDescription('listing current circles')
102
			 ->addArgument('owner', InputArgument::OPTIONAL, 'filter by owner', '')
103
			 ->addOption('level', '', InputOption::VALUE_REQUIRED, 'level of membership', Member::LEVEL_OWNER)
104
			 ->addOption('def', '', InputOption::VALUE_NONE, 'display complete circle configuration')
105
			 ->addOption('all', '', InputOption::VALUE_NONE, 'display also hidden Circles')
106
			 ->addOption('viewer', '', InputOption::VALUE_REQUIRED, 'set viewer', '')
107
			 ->addOption('json', '', InputOption::VALUE_NONE, 'returns result as JSON')
108
			 ->addOption('remote', '', InputOption::VALUE_REQUIRED, 'remote Nextcloud address', '');
109
	}
110
111
112
	/**
113
	 * @param InputInterface $input
114
	 * @param OutputInterface $output
115
	 *
116
	 * @return int
117
	 * @throws InvalidItemException
118
	 * @throws RemoteNotFoundException
119
	 * @throws RemoteResourceNotFoundException
120
	 * @throws RequestNetworkException
121
	 * @throws SignatoryException
122
	 * @throws SignatureException
123
	 */
124
	protected function execute(InputInterface $input, OutputInterface $output): int {
125
		$owner = $input->getArgument('owner');
126
		$level = $input->getOption('level');
127
		$viewer = $input->getOption('viewer');
128
		$json = $input->getOption('json');
129
		$remote = $input->getOption('remote');
130
131
		$output = new ConsoleOutput();
132
		$output = $output->section();
133
134
		$filter = null;
135
		if ($owner !== '') {
136
			$filter = new Member($owner, Member::TYPE_USER, '');
137
			$filter->setLevel((int)$level);
138
		}
139
		$circles = $this->getCircles($filter, $viewer, $remote);
140
141
		if ($json) {
142
			echo json_encode($circles, JSON_PRETTY_PRINT) . "\n";
143
144
			return 0;
145
		}
146
147
		$table = new Table($output);
148
		$table->setHeaders(['ID', 'Name', 'Type', 'Owner', 'Instance', 'Limit', 'Description']);
149
		$table->render();
150
151
		$local = $this->configService->getLocalInstance();
152
		$display = ($input->getOption('def') ? ModelManager::TYPES_LONG : ModelManager::TYPES_SHORT);
153
		foreach ($circles as $circle) {
154
//			if ($circle->isHidden() && !$input->getOption('all')) {
155
//				continue;
156
//			}
157
158
			$owner = $circle->getOwner();
159
			$table->appendRow(
160
				[
161
					$circle->getId(),
162
					$circle->getName(),
163
					json_encode($this->modelManager->getCircleTypes($circle, $display)),
164
					$owner->getUserId(),
165
					($owner->getInstance() === $local) ? '' : $owner->getInstance(),
166
					$this->getInt('members_limit', $circle->getSettings(), -1),
167
					substr($circle->getDescription(), 0, 30)
168
				]
169
			);
170
		}
171
172
		return 0;
173
	}
174
175
176
	/**
177
	 * @param Member|null $filter
178
	 * @param string $viewer
179
	 * @param string $remote
180
	 *
181
	 * @return Circle[]
182
	 * @throws InvalidItemException
183
	 * @throws RemoteNotFoundException
184
	 * @throws RemoteResourceNotFoundException
185
	 * @throws RequestNetworkException
186
	 * @throws SignatoryException
187
	 * @throws SignatureException
188
	 */
189
	private function getCircles(?Member $filter, string $viewer, string $remote): array {
190
		if ($viewer !== '') {
191
			$this->circleService->setLocalViewer($viewer);
192
		}
193
194
		if ($remote !== '') {
195
			return $this->remoteService->getCircles($remote);
196
		}
197
198
		return $this->circleService->getCircles($filter);
199
	}
200
201
}
202
203