Completed
Pull Request — master (#551)
by Maxence
01:54
created

CirclesList::execute()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 8.5341
c 0
b 0
f 0
cc 6
nc 10
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\Exceptions\SignatureException;
38
use daita\MySmallPhpTools\Traits\TArrayTools;
39
use OC\Core\Command\Base;
40
use OC\User\NoUserException;
41
use OCA\Circles\Exceptions\CircleNotFoundException;
42
use OCA\Circles\Exceptions\RemoteNotFoundException;
43
use OCA\Circles\Exceptions\RemoteResourceNotFoundException;
44
use OCA\Circles\Exceptions\UnknownRemoteException;
45
use OCA\Circles\Exceptions\ViewerNotFoundException;
46
use OCA\Circles\Model\Circle;
47
use OCA\Circles\Model\Member;
48
use OCA\Circles\Model\ModelManager;
49
use OCA\Circles\Service\CircleService;
50
use OCA\Circles\Service\ConfigService;
51
use OCA\Circles\Service\CurrentUserService;
52
use OCA\Circles\Service\RemoteService;
53
use Symfony\Component\Console\Helper\Table;
54
use Symfony\Component\Console\Input\InputArgument;
55
use Symfony\Component\Console\Input\InputInterface;
56
use Symfony\Component\Console\Input\InputOption;
57
use Symfony\Component\Console\Output\ConsoleOutput;
58
use Symfony\Component\Console\Output\OutputInterface;
59
60
61
/**
62
 * Class CirclesList
63
 *
64
 * @package OCA\Circles\Command
65
 */
66
class CirclesList extends Base {
67
68
69
	use TArrayTools;
70
71
72
	/** @var ModelManager */
73
	private $modelManager;
74
75
	/** @var CurrentUserService */
76
	private $currentUserService;
77
78
	/** @var CircleService */
79
	private $circleService;
80
81
	/** @var RemoteService */
82
	private $remoteService;
83
84
	/** @var ConfigService */
85
	private $configService;
86
87
88
	/**
89
	 * CirclesList constructor.
90
	 *
91
	 * @param ModelManager $modelManager
92
	 * @param CurrentUserService $currentUserService
93
	 * @param CircleService $circleService
94
	 * @param RemoteService $remoteService
95
	 * @param ConfigService $configService
96
	 */
97
	public function __construct(
98
		ModelManager $modelManager, CurrentUserService $currentUserService, CircleService $circleService,
99
		RemoteService $remoteService, ConfigService $configService
100
	) {
101
		parent::__construct();
102
		$this->modelManager = $modelManager;
103
		$this->currentUserService = $currentUserService;
104
		$this->circleService = $circleService;
105
		$this->remoteService = $remoteService;
106
		$this->configService = $configService;
107
	}
108
109
110
	protected function configure() {
111
		parent::configure();
112
		$this->setName('circles:manage:list')
113
			 ->setDescription('listing current circles')
114
			 ->addArgument('remote', InputArgument::OPTIONAL, 'remote Nextcloud address', '')
115
			 ->addOption('member', '', InputOption::VALUE_REQUIRED, 'search for member', '')
116
			 ->addOption('def', '', InputOption::VALUE_NONE, 'display complete circle configuration')
117
			 ->addOption('all', '', InputOption::VALUE_NONE, 'display also hidden Circles')
118
			 ->addOption('viewer', '', InputOption::VALUE_REQUIRED, 'set viewer', '')
119
			 ->addOption('json', '', InputOption::VALUE_NONE, 'returns result as JSON');
120
	}
121
122
123
	/**
124
	 * @param InputInterface $input
125
	 * @param OutputInterface $output
126
	 *
127
	 * @return int
128
	 * @throws CircleNotFoundException
129
	 * @throws InvalidItemException
130
	 * @throws NoUserException
131
	 * @throws RemoteNotFoundException
132
	 * @throws RemoteResourceNotFoundException
133
	 * @throws RequestNetworkException
134
	 * @throws SignatoryException
135
	 * @throws SignatureException
136
	 * @throws UnknownRemoteException
137
	 * @throws ViewerNotFoundException
138
	 */
139
	protected function execute(InputInterface $input, OutputInterface $output): int {
140
		$member = $input->getOption('member');
141
		$viewer = $input->getOption('viewer');
0 ignored issues
show
Unused Code introduced by
$viewer is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
142
		$json = $input->getOption('json');
143
		$remote = $input->getArgument('remote');
144
145
		$output = new ConsoleOutput();
146
		$output = $output->section();
147
148
		$this->currentUserService->commandLineViewer($input->getOption('viewer'), true);
149
150
		$filter = null;
151
		if ($member !== '') {
152
			$filter = $this->currentUserService->createFilterMember($member);
153
		}
154
155
		$circles = $this->getCircles($filter, $remote, $input->getOption('all'));
156
157
		if ($json) {
158
			echo json_encode($circles, JSON_PRETTY_PRINT) . "\n";
159
160
			return 0;
161
		}
162
163
		$table = new Table($output);
164
		$table->setHeaders(['ID', 'Name', 'Type', 'Owner', 'Instance', 'Limit', 'Description']);
165
		$table->render();
166
167
		$local = $this->configService->getLocalInstance();
168
		$display = ($input->getOption('def') ? ModelManager::TYPES_LONG : ModelManager::TYPES_SHORT);
169
		foreach ($circles as $circle) {
170
			$owner = $circle->getOwner();
171
			$table->appendRow(
172
				[
173
					$circle->getId(),
174
					$circle->getName(),
175
					json_encode($this->modelManager->getCircleTypes($circle, $display)),
176
					$owner->getUserId(),
177
					($owner->getInstance() === $local) ? '' : $owner->getInstance(),
178
					$this->getInt('members_limit', $circle->getSettings(), -1),
179
					substr($circle->getDescription(), 0, 30)
180
				]
181
			);
182
		}
183
184
		return 0;
185
	}
186
187
188
	/**
189
	 * @param Member|null $filter
190
	 * @param string $viewer
0 ignored issues
show
Bug introduced by
There is no parameter named $viewer. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
191
	 * @param string $remote
192
	 * @param bool $all
193
	 *
194
	 * @return Circle[]
195
	 * @throws InvalidItemException
196
	 * @throws RemoteNotFoundException
197
	 * @throws RemoteResourceNotFoundException
198
	 * @throws RequestNetworkException
199
	 * @throws SignatoryException
200
	 * @throws SignatureException
201
	 * @throws ViewerNotFoundException
202
	 * @throws CircleNotFoundException
203
	 * @throws UnknownRemoteException
204
	 * @throws NoUserException
205
	 */
206
	private function getCircles(?Member $filter, string $remote, bool $all = false): array {
207
		if ($remote !== '') {
208
			$circles = $this->remoteService->getCircles($remote);
209
		} else {
210
			$circles = $this->circleService->getCircles($filter);
211
		}
212
213
		if ($all) {
214
			return $circles;
215
		}
216
217
		$filtered = [];
218
		foreach ($circles as $circle) {
219
			if (!$circle->isConfig(Circle::CFG_SINGLE)
220
				&& !$circle->isConfig(Circle::CFG_HIDDEN)) {
221
				$filtered[] = $circle;
222
			}
223
		}
224
225
		return $filtered;
226
	}
227
228
}
229
230