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
						'db-env' =>
76
						[
77
							'description' => 'Get the variable from ENV'
78
						]
79
					]
80
				],
81
				'lock' =>
82
				[
83
					'description' => 'Lock the production environment'
84
				]
85
			]
86
		]
87
	];
88
89
	/**
90
	 * run the command
91
	 *
92
	 * @since 3.0.0
93
	 *
94
	 * @param string $mode name of the mode
95
	 *
96
	 * @return string|null
97
	 */
98
99 8
	public function run(string $mode = null) : ?string
100
	{
101 8
		$parser = new Parser($this->_request);
102 8
		$parser->init($mode);
103
104
		/* run command */
105
106 8
		$argumentKey = $parser->getArgument(1);
107 8
		if ($argumentKey === 'list')
108
		{
109 1
			return $this->_list();
110
		}
111 7
		if ($argumentKey === 'set')
112
		{
113 2
			return $this->_set($parser->getOption()) ? $this->success() : $this->error();
114
		}
115 5
		if ($argumentKey === 'parse')
116
		{
117 3
			return $this->_parse($parser->getOption()) ? $this->success() : $this->error();
118
		}
119 2
		if ($argumentKey === 'lock')
120
		{
121 1
			return $this->_lock() ? $this->success() : $this->error();
122
		}
123 1
		return $this->getHelp();
124
	}
125
126
	/**
127
	 * list the config
128
	 *
129
	 * @since 3.0.0
130
	 *
131
	 * @return string|null
132
	 */
133
134 1
	protected function _list() : ?string
135
	{
136 1
		$output = null;
137 1
		$configArray = $this->_config->get();
138
139
		/* process config */
140
141 1
		foreach ($configArray as $key => $value)
142
		{
143 1
			if ($key === 'dbPassword')
144
			{
145 1
				$value = str_repeat('*', strlen($value));
146
			}
147 1
			if ($value)
148
			{
149 1
				$output .= str_pad($key, 30) . $value . PHP_EOL;
150
			}
151
		}
152 1
		return $output;
153
	}
154
155
	/**
156
	 * set the config
157
	 *
158
	 * @since 3.0.0
159
	 *
160
	 * @param array $optionArray
161
	 *
162
	 * @return bool
163
	 */
164
165 2
	protected function _set(array $optionArray = []) : bool
166
	{
167 2
		$dbType = $this->prompt('db-type', $optionArray);
168 2
		$dbHost = $this->prompt('db-host', $optionArray);
169 2
		if ($dbType && $dbHost)
170
		{
171 1
			$this->_config->set('dbType', $dbType);
172 1
			$this->_config->set('dbHost', $dbHost);
173 1
			$this->_config->set('dbName', $optionArray['db-name']);
174 1
			$this->_config->set('dbUser', $optionArray['db-user']);
175 1
			$this->_config->set('dbPassword', $optionArray['db-password']);
176 1
			$this->_config->set('dbPrefix', $optionArray['db-prefix']);
177 1
			return $this->_config->write();
178
		}
179 1
		return false;
180
	}
181
182
	/**
183
	 * parse the config
184
	 *
185
	 * @since 3.0.0
186
	 *
187
	 * @param array $optionArray
188
	 *
189
	 * @return bool
190
	 */
191
192 3
	protected function _parse(array $optionArray = []) : bool
193
	{
194 3
		$dbUrl = $this->prompt('db-url', $optionArray);
195 3
		$dbUrl = $optionArray['db-env'] ? getenv($dbUrl) : $dbUrl;
196 3
		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...
197
		{
198 2
			$this->_config->parse($dbUrl);
199 2
			return $this->_config->write();
200
		}
201 1
		return false;
202
	}
203
204
	/**
205
	 * lock the config
206
	 *
207
	 * @since 3.0.0
208
	 *
209
	 * @return bool
210
	 */
211
212 1
	protected function _lock() : bool
213
	{
214 1
		$this->_config->set('env', 'production');
215 1
		return $this->_config->write();
216
	}
217
}
218