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

CirclesList::execute()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 50

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 50
rs 8.7797
c 0
b 0
f 0
cc 5
nc 12
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\RemoteService;
45
use Symfony\Component\Console\Helper\Table;
46
use Symfony\Component\Console\Input\InputArgument;
47
use Symfony\Component\Console\Input\InputInterface;
48
use Symfony\Component\Console\Input\InputOption;
49
use Symfony\Component\Console\Output\ConsoleOutput;
50
use Symfony\Component\Console\Output\OutputInterface;
51
52
53
/**
54
 * Class CirclesList
55
 *
56
 * @package OCA\Circles\Command
57
 */
58
class CirclesList extends Base {
59
60
61
	use TArrayTools;
62
63
64
	/** @var CircleService */
65
	private $circleService;
66
67
	/** @var RemoteService */
68
	private $remoteService;
69
70
	/** @var ModelManager */
71
	private $modelManager;
72
73
74
	/**
75
	 * CirclesList constructor.
76
	 *
77
	 * @param CircleService $circleService
78
	 * @param RemoteService $remoteService
79
	 * @param ModelManager $modelManager
80
	 */
81
	public function __construct(
82
		CircleService $circleService, RemoteService $remoteService, ModelManager $modelManager
83
	) {
84
		parent::__construct();
85
		$this->circleService = $circleService;
86
		$this->remoteService = $remoteService;
87
		$this->modelManager = $modelManager;
88
	}
89
90
91
	protected function configure() {
92
		parent::configure();
93
		$this->setName('circles:manage:list')
94
			 ->setDescription('listing current circles')
95
			 ->addArgument('owner', InputArgument::OPTIONAL, 'filter by owner', '')
96
			 ->addOption('level', '', InputOption::VALUE_REQUIRED, 'level of membership', Member::LEVEL_OWNER)
97
			 ->addOption('def', '', InputOption::VALUE_NONE, 'display complete circle configuration')
98
			 ->addOption('all', '', InputOption::VALUE_NONE, 'display also hidden Circles')
99
			 ->addOption('viewer', '', InputOption::VALUE_REQUIRED, 'set viewer', '')
100
			 ->addOption('json', '', InputOption::VALUE_NONE, 'returns result as JSON')
101
			 ->addOption('remote', '', InputOption::VALUE_REQUIRED, 'remote Nextcloud address', '');
102
	}
103
104
105
	/**
106
	 * @param InputInterface $input
107
	 * @param OutputInterface $output
108
	 *
109
	 * @return int
110
	 * @throws InvalidItemException
111
	 * @throws RemoteNotFoundException
112
	 * @throws RemoteResourceNotFoundException
113
	 * @throws RequestNetworkException
114
	 * @throws SignatoryException
115
	 * @throws SignatureException
116
	 */
117
	protected function execute(InputInterface $input, OutputInterface $output): int {
118
		$owner = $input->getArgument('owner');
119
		$level = $input->getOption('level');
120
		$viewer = $input->getOption('viewer');
121
		$json = $input->getOption('json');
122
		$remote = $input->getOption('remote');
123
		$display = ($input->getOption('def') ? ModelManager::TYPES_LONG : ModelManager::TYPES_SHORT);
124
125
		$output = new ConsoleOutput();
126
		$output = $output->section();
127
128
		$filter = null;
129
		if ($owner !== '') {
130
			$filter = new Member($owner, Member::TYPE_USER, '');
131
			$filter->setLevel((int)$level);
132
		}
133
		$circles = $this->getCircles($filter, $viewer, $remote);
134
135
		if ($json) {
136
			echo json_encode($circles, JSON_PRETTY_PRINT) . "\n";
137
138
			return 0;
139
		}
140
141
		$table = new Table($output);
142
		$table->setHeaders(['ID', 'Name', 'Type', 'Owner', 'Instance', 'Limit', 'Description']);
143
		$table->render();
144
145
		foreach ($circles as $circle) {
146
//			if ($circle->isHidden() && !$input->getOption('all')) {
147
//				continue;
148
//			}
149
150
			$owner = $circle->getOwner();
151
			$settings = $circle->getSettings();
152
			$table->appendRow(
153
				[
154
					$circle->getId(),
155
					$circle->getName(),
156
					json_encode($this->modelManager->getCircleTypes($circle, $display)),
157
					$owner->getUserId(),
158
					$owner->getInstance(),
159
					$this->getInt('members_limit', $settings, -1),
160
					substr($circle->getDescription(), 0, 30)
161
				]
162
			);
163
		}
164
165
		return 0;
166
	}
167
168
169
	/**
170
	 * @param Member|null $filter
171
	 * @param string $viewer
172
	 * @param string $remote
173
	 *
174
	 * @return Circle[]
175
	 * @throws InvalidItemException
176
	 * @throws RemoteNotFoundException
177
	 * @throws RemoteResourceNotFoundException
178
	 * @throws RequestNetworkException
179
	 * @throws SignatoryException
180
	 * @throws SignatureException
181
	 */
182
	private function getCircles(?Member $filter, string $viewer, string $remote): array {
183
		if ($viewer !== '') {
184
			$this->circleService->setLocalViewer($viewer);
185
		}
186
187
		if ($remote !== '') {
188
			return $this->remoteService->getCircles($remote);
189
		}
190
191
		return $this->circleService->getCircles($filter);
192
	}
193
194
}
195
196