Passed
Push — master ( 91ae7f...ecf287 )
by Blizzz
15:15 queued 11s
created

ShowConfig::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 18
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 21
rs 9.6666
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Christoph Wurst <[email protected]>
7
 * @author Daniel Kesselberg <[email protected]>
8
 * @author Joas Schilling <[email protected]>
9
 * @author Laurens Post <[email protected]>
10
 * @author Morris Jobke <[email protected]>
11
 * @author Roeland Jago Douma <[email protected]>
12
 *
13
 * @license AGPL-3.0
14
 *
15
 * This code is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License, version 3,
17
 * as published by the Free Software Foundation.
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, version 3,
25
 * along with this program. If not, see <http://www.gnu.org/licenses/>
26
 *
27
 */
28
29
namespace OCA\User_LDAP\Command;
30
31
use OC\Core\Command\Base;
32
use OCA\User_LDAP\Configuration;
33
use OCA\User_LDAP\Helper;
34
use Symfony\Component\Console\Helper\Table;
35
use Symfony\Component\Console\Input\InputArgument;
36
use Symfony\Component\Console\Input\InputInterface;
37
use Symfony\Component\Console\Input\InputOption;
38
use Symfony\Component\Console\Output\OutputInterface;
39
40
class ShowConfig extends Base {
41
	/** @var \OCA\User_LDAP\Helper */
42
	protected $helper;
43
44
	/**
45
	 * @param Helper $helper
46
	 */
47
	public function __construct(Helper $helper) {
48
		$this->helper = $helper;
49
		parent::__construct();
50
	}
51
52
	protected function configure() {
53
		$this
54
			->setName('ldap:show-config')
55
			->setDescription('shows the LDAP configuration')
56
			->addArgument(
57
					'configID',
58
					InputArgument::OPTIONAL,
59
					'will show the configuration of the specified id'
60
					 )
61
			->addOption(
62
					'show-password',
63
					null,
64
					InputOption::VALUE_NONE,
65
					'show ldap bind password'
66
					 )
67
			->addOption(
68
					'output',
69
					null,
70
					InputOption::VALUE_OPTIONAL,
71
					'Output format (table, plain, json or json_pretty, default is table)',
72
					'table'
73
					 )
74
		;
75
	}
76
77
	protected function execute(InputInterface $input, OutputInterface $output): int {
78
		$availableConfigs = $this->helper->getServerConfigurationPrefixes();
79
		$configID = $input->getArgument('configID');
80
		if (!is_null($configID)) {
81
			$configIDs[] = $configID;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$configIDs was never initialized. Although not strictly required by PHP, it is generally a good practice to add $configIDs = array(); before regardless.
Loading history...
82
			if (!in_array($configIDs[0], $availableConfigs)) {
83
				$output->writeln("Invalid configID");
84
				return 1;
85
			}
86
		} else {
87
			$configIDs = $availableConfigs;
88
		}
89
90
		$this->renderConfigs($configIDs, $input, $output);
91
		return 0;
92
	}
93
94
	/**
95
	 * prints the LDAP configuration(s)
96
	 * @param string[] configID(s)
0 ignored issues
show
Bug introduced by
The type OCA\User_LDAP\Command\configID was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
97
	 * @param InputInterface $input
98
	 * @param OutputInterface $output
99
	 */
100
	protected function renderConfigs($configIDs, $input, $output) {
101
		$renderTable = $input->getOption('output') === 'table' or $input->getOption('output') === null;
102
		$showPassword = $input->getOption('show-password');
103
104
		$configs = [];
105
		foreach ($configIDs as $id) {
106
			$configHolder = new Configuration($id);
107
			$configuration = $configHolder->getConfiguration();
108
			ksort($configuration);
109
110
			$rows = [];
111
			if ($renderTable) {
112
				foreach ($configuration as $key => $value) {
113
					if (is_array($value)) {
114
						$value = implode(';', $value);
115
					}
116
					if ($key === 'ldapAgentPassword' && !$showPassword) {
117
						$rows[] = [$key, '***'];
118
					} else {
119
						$rows[] = [$key, $value];
120
					}
121
				}
122
				$table = new Table($output);
123
				$table->setHeaders(['Configuration', $id]);
124
				$table->setRows($rows);
125
				$table->render();
126
			} else {
127
				foreach ($configuration as $key => $value) {
128
					if ($key === 'ldapAgentPassword' && !$showPassword) {
129
						$rows[$key] = '***';
130
					} else {
131
						$rows[$key] = $value;
132
					}
133
				}
134
				$configs[$id] = $rows;
135
			}
136
		}
137
		if (!$renderTable) {
138
			$this->writeArrayInOutputFormat($input, $output, $configs);
139
		}
140
	}
141
}
142