Passed
Push — master ( 052ddc...675ec9 )
by Joas
13:56 queued 13s
created

Install::validateInput()   C

Complexity

Conditions 13
Paths 161

Size

Total Lines 74
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 13
eloc 51
c 1
b 0
f 0
nc 161
nop 3
dl 0
loc 74
rs 6.1083

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Bernhard Posselt <[email protected]>
6
 * @author Christian Kampka <[email protected]>
7
 * @author Christoph Wurst <[email protected]>
8
 * @author Daniel Hansson <[email protected]>
9
 * @author Daniel Kesselberg <[email protected]>
10
 * @author Joas Schilling <[email protected]>
11
 * @author Jörn Friedrich Dreyer <[email protected]>
12
 * @author Morris Jobke <[email protected]>
13
 * @author Roeland Jago Douma <[email protected]>
14
 * @author Thomas Müller <[email protected]>
15
 * @author Thomas Pulzer <[email protected]>
16
 *
17
 * @license AGPL-3.0
18
 *
19
 * This code is free software: you can redistribute it and/or modify
20
 * it under the terms of the GNU Affero General Public License, version 3,
21
 * as published by the Free Software Foundation.
22
 *
23
 * This program is distributed in the hope that it will be useful,
24
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26
 * GNU Affero General Public License for more details.
27
 *
28
 * You should have received a copy of the GNU Affero General Public License, version 3,
29
 * along with this program. If not, see <http://www.gnu.org/licenses/>
30
 *
31
 */
32
33
namespace OC\Core\Command\Maintenance;
34
35
use InvalidArgumentException;
36
use OC\Installer;
37
use OC\Setup;
38
use OC\SystemConfig;
39
use OCP\Defaults;
40
use Symfony\Component\Console\Command\Command;
41
use Symfony\Component\Console\Helper\QuestionHelper;
42
use Symfony\Component\Console\Input\InputInterface;
43
use Symfony\Component\Console\Input\InputOption;
44
use Symfony\Component\Console\Output\OutputInterface;
45
use Symfony\Component\Console\Question\Question;
46
47
class Install extends Command {
48
49
	/**
50
	 * @var SystemConfig
51
	 */
52
	private $config;
53
54
	public function __construct(SystemConfig $config) {
55
		parent::__construct();
56
		$this->config = $config;
57
	}
58
59
	protected function configure() {
60
		$this
61
			->setName('maintenance:install')
62
			->setDescription('install Nextcloud')
63
			->addOption('database', null, InputOption::VALUE_REQUIRED, 'Supported database type', 'sqlite')
64
			->addOption('database-name', null, InputOption::VALUE_REQUIRED, 'Name of the database')
65
			->addOption('database-host', null, InputOption::VALUE_REQUIRED, 'Hostname of the database', 'localhost')
66
			->addOption('database-port', null, InputOption::VALUE_REQUIRED, 'Port the database is listening on')
67
			->addOption('database-user', null, InputOption::VALUE_REQUIRED, 'User name to connect to the database')
68
			->addOption('database-pass', null, InputOption::VALUE_OPTIONAL, 'Password of the database user', null)
69
			->addOption('database-table-space', null, InputOption::VALUE_OPTIONAL, 'Table space of the database (oci only)', null)
70
			->addOption('admin-user', null, InputOption::VALUE_REQUIRED, 'User name of the admin account', 'admin')
71
			->addOption('admin-pass', null, InputOption::VALUE_REQUIRED, 'Password of the admin account')
72
			->addOption('admin-email', null, InputOption::VALUE_OPTIONAL, 'E-Mail of the admin account')
73
			->addOption('data-dir', null, InputOption::VALUE_REQUIRED, 'Path to data directory', \OC::$SERVERROOT."/data");
74
	}
75
76
	protected function execute(InputInterface $input, OutputInterface $output) {
77
78
		// validate the environment
79
		$server = \OC::$server;
80
		$setupHelper = new Setup(
81
			$this->config,
82
			$server->getIniWrapper(),
83
			$server->getL10N('lib'),
84
			$server->query(Defaults::class),
85
			$server->getLogger(),
86
			$server->getSecureRandom(),
87
			\OC::$server->query(Installer::class)
88
		);
89
		$sysInfo = $setupHelper->getSystemInfo(true);
90
		$errors = $sysInfo['errors'];
91
		if (count($errors) > 0) {
92
			$this->printErrors($output, $errors);
93
94
			// ignore the OS X setup warning
95
			if (count($errors) !== 1 ||
96
				(string)$errors[0]['error'] !== 'Mac OS X is not supported and Nextcloud will not work properly on this platform. Use it at your own risk! ') {
97
				return 1;
98
			}
99
		}
100
101
		// validate user input
102
		$options = $this->validateInput($input, $output, array_keys($sysInfo['databases']));
103
104
		// perform installation
105
		$errors = $setupHelper->install($options);
106
		if (count($errors) > 0) {
107
			$this->printErrors($output, $errors);
108
			return 1;
109
		}
110
		$output->writeln("Nextcloud was successfully installed");
111
		return 0;
112
	}
113
114
	/**
115
	 * @param InputInterface $input
116
	 * @param OutputInterface $output
117
	 * @param string[] $supportedDatabases
118
	 * @return array
119
	 */
120
	protected function validateInput(InputInterface $input, OutputInterface $output, $supportedDatabases) {
121
		$db = strtolower($input->getOption('database'));
122
123
		if (!in_array($db, $supportedDatabases)) {
124
			throw new InvalidArgumentException("Database <$db> is not supported.");
125
		}
126
127
		$dbUser = $input->getOption('database-user');
128
		$dbPass = $input->getOption('database-pass');
129
		$dbName = $input->getOption('database-name');
130
		$dbPort = $input->getOption('database-port');
131
		if ($db === 'oci') {
132
			// an empty hostname needs to be read from the raw parameters
133
			$dbHost = $input->getParameterOption('--database-host', '');
134
		} else {
135
			$dbHost = $input->getOption('database-host');
136
		}
137
		if ($dbPort) {
138
			// Append the port to the host so it is the same as in the config (there is no dbport config)
139
			$dbHost .= ':' . $dbPort;
0 ignored issues
show
Bug introduced by
Are you sure $dbPort of type string|string[]|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

139
			$dbHost .= ':' . /** @scrutinizer ignore-type */ $dbPort;
Loading history...
140
		}
141
		if ($input->hasParameterOption('--database-pass')) {
142
			$dbPass = (string) $input->getOption('database-pass');
143
		}
144
		$adminLogin = $input->getOption('admin-user');
145
		$adminPassword = $input->getOption('admin-pass');
146
		$adminEmail = $input->getOption('admin-email');
147
		$dataDir = $input->getOption('data-dir');
148
149
		if ($db !== 'sqlite') {
150
			if (is_null($dbUser)) {
151
				throw new InvalidArgumentException("Database user not provided.");
152
			}
153
			if (is_null($dbName)) {
154
				throw new InvalidArgumentException("Database name not provided.");
155
			}
156
			if (is_null($dbPass)) {
157
				/** @var QuestionHelper $helper */
158
				$helper = $this->getHelper('question');
159
				$question = new Question('What is the password to access the database with user <'.$dbUser.'>?');
0 ignored issues
show
Bug introduced by
Are you sure $dbUser of type boolean|string|string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

159
				$question = new Question('What is the password to access the database with user <'./** @scrutinizer ignore-type */ $dbUser.'>?');
Loading history...
160
				$question->setHidden(true);
161
				$question->setHiddenFallback(false);
162
				$dbPass = $helper->ask($input, $output, $question);
163
			}
164
		}
165
166
		if (is_null($adminPassword)) {
167
			/** @var QuestionHelper $helper */
168
			$helper = $this->getHelper('question');
169
			$question = new Question('What is the password you like to use for the admin account <'.$adminLogin.'>?');
170
			$question->setHidden(true);
171
			$question->setHiddenFallback(false);
172
			$adminPassword = $helper->ask($input, $output, $question);
173
		}
174
175
		if ($adminEmail !== null && !filter_var($adminEmail, FILTER_VALIDATE_EMAIL)) {
176
			throw new InvalidArgumentException('Invalid e-mail-address <' . $adminEmail . '> for <' . $adminLogin . '>.');
0 ignored issues
show
Bug introduced by
Are you sure $adminEmail of type boolean|string|string[] can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

176
			throw new InvalidArgumentException('Invalid e-mail-address <' . /** @scrutinizer ignore-type */ $adminEmail . '> for <' . $adminLogin . '>.');
Loading history...
177
		}
178
179
		$options = [
180
			'dbtype' => $db,
181
			'dbuser' => $dbUser,
182
			'dbpass' => $dbPass,
183
			'dbname' => $dbName,
184
			'dbhost' => $dbHost,
185
			'adminlogin' => $adminLogin,
186
			'adminpass' => $adminPassword,
187
			'adminemail' => $adminEmail,
188
			'directory' => $dataDir
189
		];
190
		if ($db === 'oci') {
191
			$options['dbtablespace'] = $input->getParameterOption('--database-table-space', '');
192
		}
193
		return $options;
194
	}
195
196
	/**
197
	 * @param OutputInterface $output
198
	 * @param $errors
199
	 */
200
	protected function printErrors(OutputInterface $output, $errors) {
201
		foreach ($errors as $error) {
202
			if (is_array($error)) {
203
				$output->writeln('<error>' . (string)$error['error'] . '</error>');
204
				$output->writeln('<info> -> ' . (string)$error['hint'] . '</info>');
205
			} else {
206
				$output->writeln('<error>' . (string)$error . '</error>');
207
			}
208
		}
209
	}
210
}
211