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

CirclesList::execute()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 42
rs 8.9368
c 0
b 0
f 0
cc 5
nc 4
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\Db\CircleRequest;
39
use OCA\Circles\Exceptions\RemoteNotFoundException;
40
use OCA\Circles\Exceptions\RemoteResourceNotFoundException;
41
use OCA\Circles\Model\Circle;
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 CircleRequest $circleRequest
78
	 * @param RemoteService $remoteService
79
	 * @param ModelManager $modelManager
80
	 */
81
	public function __construct(
82
		CircleRequest $circleRequest, CircleService $circleService, RemoteService $remoteService,
83
		ModelManager $modelManager
84
	) {
85
		parent::__construct();
86
		$this->circleService = $circleService;
87
		$this->remoteService = $remoteService;
88
		$this->modelManager = $modelManager;
89
	}
90
91
92
	protected function configure() {
93
		parent::configure();
94
		$this->setName('circles:manage:list')
95
			 ->setDescription('listing current circles')
96
			 ->addArgument('owner', InputArgument::OPTIONAL, 'filter by owner', '')
97
			 ->addOption('all', '', InputOption::VALUE_NONE, 'display also hidden Circles')
98
			 ->addOption('viewer', '', InputOption::VALUE_REQUIRED, 'set viewer', '')
99
			 ->addOption('json', '', InputOption::VALUE_NONE, 'returns result as JSON')
100
			 ->addOption('remote', '', InputOption::VALUE_REQUIRED, 'remote Nextcloud address', '');
101
	}
102
103
104
	/**
105
	 * @param InputInterface $input
106
	 * @param OutputInterface $output
107
	 *
108
	 * @return int
109
	 * @throws RemoteNotFoundException
110
	 * @throws RemoteResourceNotFoundException
111
	 * @throws RequestNetworkException
112
	 * @throws SignatoryException
113
	 * @throws SignatureException
114
	 */
115
	protected function execute(InputInterface $input, OutputInterface $output): int {
116
		$owner = $input->getArgument('owner');
117
		$viewer = $input->getOption('viewer');
118
		$json = $input->getOption('json');
119
		$remote = $input->getOption('remote');
120
121
		$output = new ConsoleOutput();
122
		$output = $output->section();
123
		$circles = $this->getCircles($owner, $viewer, $remote);
124
125
		if ($json) {
126
			echo json_encode($circles, JSON_PRETTY_PRINT) . "\n";
127
128
			return 0;
129
		}
130
131
		$table = new Table($output);
132
		$table->setHeaders(['ID', 'Name', 'Type', 'Owner', 'Instance', 'Limit', 'Description']);
133
		$table->render();
134
135
		foreach ($circles as $circle) {
136
			if ($circle->isHidden() && !$input->getOption('all')) {
137
				continue;
138
			}
139
140
			$owner = $circle->getOwner();
141
			$settings = $circle->getSettings();
142
			$table->appendRow(
143
				[
144
					$circle->getId(),
145
					$circle->getName(),
146
					json_encode($this->modelManager->getCircleTypes($circle, ModelManager::TYPES_SHORT)),
147
					$owner->getUserId(),
148
					$owner->getInstance(),
149
					$this->getInt('members_limit', $settings, -1),
150
					substr($circle->getDescription(), 0, 30)
151
				]
152
			);
153
		}
154
155
		return 0;
156
	}
157
158
159
	/**
160
	 * @param string $owner
161
	 * @param string $viewer
162
	 * @param string $remote
163
	 *
164
	 * @return Circle[]
165
	 * @throws RemoteNotFoundException
166
	 * @throws RemoteResourceNotFoundException
167
	 * @throws RequestNetworkException
168
	 * @throws SignatoryException
169
	 * @throws SignatureException
170
	 * @throws InvalidItemException
171
	 */
172
	private function getCircles(string $owner, string $viewer, string $remote): array {
173
		if ($viewer !== '') {
174
			$this->circleService->setLocalViewer($viewer);
175
		}
176
177
		if ($remote !== '') {
178
			return $this->remoteService->getCircles($remote);
179
		}
180
181
		return $this->circleService->getCircles($owner);
182
	}
183
184
}
185
186