Completed
Push — master ( 402a90...134b49 )
by Henry
16:11
created

includes/Console/Command/Config.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript\Console\Command;
3
4
use Redaxscript\Console\Parser;
5
6
/**
7
 * children class to execute the config command
8
 *
9
 * @since 3.0.0
10
 *
11
 * @package Redaxscript
12
 * @category Console
13
 * @author Henry Ruhs
14
 */
15
16
class Config extends CommandAbstract
17
{
18
	/**
19
	 * array of the command
20
	 *
21
	 * @var array
22
	 */
23
24
	protected $_commandArray =
25
	[
26
		'config' =>
27
		[
28
			'description' => 'Config command',
29
			'argumentArray' =>
30
			[
31
				'list' =>
32
				[
33
					'description' => 'List the configuration'
34
				],
35
				'set' =>
36
				[
37
					'description' => 'Set the configuration',
38
					'optionArray' =>
39
					[
40
						'db-type' =>
41
						[
42
							'description' => 'Required database type'
43
						],
44
						'db-host' =>
45
						[
46
							'description' => 'Required database host or file'
47
						],
48
						'db-name' =>
49
						[
50
							'description' => 'Optional database name'
51
						],
52
						'db-user' =>
53
						[
54
							'description' => 'Optional database user'
55
						],
56
						'db-password' =>
57
						[
58
							'description' => 'Optional database password'
59
						],
60
						'db-prefix' =>
61
						[
62
							'description' => 'Optional database prefix'
63
						]
64
					]
65
				],
66
				'parse' =>
67
				[
68
					'description' => 'Parse the configuration',
69
					'optionArray' =>
70
					[
71
						'db-url' =>
72
						[
73
							'description' => 'Required database url'
74
						]
75
					]
76
				],
77
				'lock' =>
78
				[
79
					'description' => 'Lock the production environment'
80
				]
81
			]
82
		]
83
	];
84
85
	/**
86
	 * run the command
87
	 *
88
	 * @since 3.0.0
89
	 *
90
	 * @param string $mode name of the mode
91
	 *
92
	 * @return string|null
93
	 */
94
95
	public function run(string $mode = null) : ?string
96
	{
97
		$parser = new Parser($this->_request);
98
		$parser->init($mode);
99 8
100
		/* run command */
101 8
102 8
		$argumentKey = $parser->getArgument(1);
103
		if ($argumentKey === 'list')
104
		{
105
			return $this->_list();
106 8
		}
107 8
		if ($argumentKey === 'set')
108
		{
109 1
			return $this->_set($parser->getOption()) ? $this->success() : $this->error();
110
		}
111 7
		if ($argumentKey === 'parse')
112
		{
113 2
			return $this->_parse($parser->getOption()) ? $this->success() : $this->error();
114
		}
115 5
		if ($argumentKey === 'lock')
116
		{
117 3
			return $this->_lock() ? $this->success() : $this->error();
118
		}
119 2
		return $this->getHelp();
120
	}
121 1
122
	/**
123 1
	 * list the config
124
	 *
125
	 * @since 3.0.0
126
	 *
127
	 * @return string|null
128
	 */
129
130
	protected function _list() : ?string
131
	{
132
		$output = null;
133
		$configArray = $this->_config->get();
134 1
135
		/* process config */
136 1
137 1
		foreach ($configArray as $key => $value)
138
		{
139
			if ($key === 'dbPassword')
140
			{
141 1
				$value = str_repeat('*', strlen($value));
142
			}
143 1
			if ($value)
144
			{
145 1
				$output .= str_pad($key, 30) . $value . PHP_EOL;
146
			}
147 1
		}
148
		return $output;
149 1
	}
150
151
	/**
152 1
	 * set the config
153
	 *
154
	 * @since 3.0.0
155
	 *
156
	 * @param array $optionArray
157
	 *
158
	 * @return bool
159
	 */
160
161
	protected function _set(array $optionArray = []) : bool
162
	{
163
		$dbType = $this->prompt('db-type', $optionArray);
164
		$dbHost = $this->prompt('db-host', $optionArray);
165 2
		if ($dbType && $dbHost)
166
		{
167 2
			$this->_config->set('dbType', $dbType);
168 2
			$this->_config->set('dbHost', $dbHost);
169 2
			$this->_config->set('dbName', $optionArray['db-name']);
170
			$this->_config->set('dbUser', $optionArray['db-user']);
171 1
			$this->_config->set('dbPassword', $optionArray['db-password']);
172 1
			$this->_config->set('dbPrefix', $optionArray['db-prefix']);
173 1
			return $this->_config->write();
174 1
		}
175 1
		return false;
176 1
	}
177 1
178
	/**
179 1
	 * parse the config
180
	 *
181
	 * @since 3.0.0
182
	 *
183
	 * @param array $optionArray
184
	 *
185
	 * @return bool
186
	 */
187
188
	protected function _parse(array $optionArray = []) : bool
189
	{
190
		$dbUrl = $this->prompt('db-url', $optionArray);
191
		if ($dbUrl)
0 ignored issues
show
Bug Best Practice introduced by
The expression $dbUrl of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
192 3
		{
193
			$this->_config->parse($dbUrl);
194 3
			return $this->_config->write();
195 3
		}
196 3
		return false;
197
	}
198 2
199 2
	/**
200
	 * lock the config
201 1
	 *
202
	 * @since 3.0.0
203
	 *
204
	 * @return bool
205
	 */
206
207
	protected function _lock() : bool
208
	{
209
		$this->_config->set('env', 'production');
210
		return $this->_config->write();
211
	}
212
}
213