Completed
Pull Request — master (#379)
by Maxence
01:44
created

CirclesList::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
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 OC\Core\Command\Base;
33
use OCA\Circles\Db\CirclesRequest;
34
use OCA\Circles\Exceptions\ConfigNoCircleAvailableException;
35
use OCA\Circles\Model\Circle;
36
use OCP\IL10N;
37
use Symfony\Component\Console\Helper\Table;
38
use Symfony\Component\Console\Input\InputArgument;
39
use Symfony\Component\Console\Input\InputInterface;
40
use Symfony\Component\Console\Input\InputOption;
41
use Symfony\Component\Console\Output\ConsoleOutput;
42
use Symfony\Component\Console\Output\OutputInterface;
43
44
45
/**
46
 * Class CirclesList
47
 *
48
 * @package OCA\Circles\Command
49
 */
50
class CirclesList extends Base {
51
52
53
	/** @var IL10N */
54
	private $l10n;
55
56
	/** @var CirclesRequest */
57
	private $circlesRequest;
58
59
60
	/**
61
	 * CirclesList constructor.
62
	 *
63
	 * @param IL10N $l10n
64
	 * @param CirclesRequest $circlesRequest
65
	 */
66
	public function __construct(IL10N $l10n, CirclesRequest $circlesRequest) {
67
		parent::__construct();
68
		$this->l10n = $l10n;
69
		$this->circlesRequest = $circlesRequest;
70
	}
71
72
73
	protected function configure() {
74
		parent::configure();
75
		$this->setName('circles:manage:list')
76
			 ->setDescription('listing current circles')
77
			 ->addArgument('owner', InputArgument::OPTIONAL, 'filter by owner', '')
78
			 ->addOption('viewer', '', InputOption::VALUE_REQUIRED, 'set viewer', '')
79
			 ->addOption('json', '', InputOption::VALUE_NONE, 'returns result as JSON');
80
	}
81
82
83
	/**
84
	 * @param InputInterface $input
85
	 * @param OutputInterface $output
86
	 *
87
	 * @return int
88
	 * @throws ConfigNoCircleAvailableException
89
	 */
90
	protected function execute(InputInterface $input, OutputInterface $output): int {
91
		$owner = $input->getArgument('owner');
92
		$viewer = $input->getOption('viewer');
93
		$json = $input->getOption('json');
94
95
		$output = new ConsoleOutput();
96
		$output = $output->section();
97
		$circles = $this->getCircles($owner, $viewer);
98
99
		if ($json) {
100
			echo json_encode($circles, JSON_PRETTY_PRINT) . "\n";
101
102
			return 0;
103
		}
104
105
		$table = new Table($output);
106
		$table->setHeaders(['ID', 'Name', 'Type', 'Owner']);
107
		$table->render();
108
		$output->writeln('');
109
110
		$c = 0;
0 ignored issues
show
Unused Code introduced by
$c 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...
111
		foreach ($circles as $circle) {
112
			$table->appendRow(
113
				[
114
					$circle->getUniqueId(),
115
					$circle->getName(),
116
					$circle->getTypeLongString(),
117
					$circle->getOwner()
118
						   ->getUserId()
119
				]
120
			);
121
		}
122
123
		return 0;
124
	}
125
126
127
	/**
128
	 * @param string $owner
129
	 * @param string $viewer
130
	 *
131
	 * @return Circle[]
132
	 * @throws ConfigNoCircleAvailableException
133
	 */
134
	private function getCircles(string $owner, string $viewer): array {
135
		if ($viewer === '') {
136
			$circles = $this->circlesRequest->forceGetCircles($owner);
137
		} else {
138
			$circles = $this->circlesRequest->getCircles($viewer, 0, '', 0, true, $owner);
139
		}
140
141
		return $circles;
142
	}
143
144
}
145
146