Completed
Push — master ( 0ea243...da58d4 )
by Henry
10:25 queued 33s
created

includes/Console/Command/Install.php (4 issues)

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
use Redaxscript\Db;
6
use Redaxscript\Installer;
7
use function method_exists;
8
9
/**
10
 * children class to execute the install command
11
 *
12
 * @since 3.0.0
13
 *
14
 * @package Redaxscript
15
 * @category Console
16
 * @author Henry Ruhs
17
 */
18
19
class Install extends CommandAbstract
20
{
21
	/**
22
	 * array of the command
23
	 *
24
	 * @var array
25
	 */
26
27
	protected $_commandArray =
28
	[
29
		'install' =>
30
		[
31
			'description' => 'Install command',
32
			'argumentArray' =>
33
			[
34
				'database' =>
35
				[
36
					'description' => 'Install the database',
37
					'optionArray' =>
38
					[
39
						'admin-name' =>
40
						[
41
							'description' => 'Required admin name'
42
						],
43
						'admin-user' =>
44
						[
45
							'description' => 'Required admin user'
46
						],
47
						'admin-password' =>
48
						[
49
							'description' => 'Required admin password'
50
						],
51
						'admin-email' =>
52
						[
53
							'description' => 'Required admin email'
54
						]
55
					]
56
				],
57
				'module' =>
58
				[
59
					'description' => 'Install the module',
60
					'optionArray' =>
61
					[
62
						'alias' =>
63
						[
64
							'description' => 'Required module alias'
65
						]
66
					]
67
				]
68
			]
69
		]
70
	];
71
72
	/**
73
	 * run the command
74
	 *
75
	 * @since 3.0.0
76
	 *
77
	 * @param string $mode name of the mode
78
	 *
79
	 * @return string|null
80
	 */
81
82 5
	public function run(string $mode = null) : ?string
83
	{
84 5
		$parser = new Parser($this->_request);
85 5
		$parser->init($mode);
86
87
		/* run command */
88
89 5
		$argumentKey = $parser->getArgument(1);
90 5
		$haltOnError = (bool)$parser->getOption('halt-on-error');
91 5
		if ($argumentKey === 'database')
92
		{
93 2
			return $this->_database($parser->getOption()) ? $this->success() : $this->error($haltOnError);
94
		}
95 3
		if ($argumentKey === 'module')
96
		{
97 2
			return $this->_module($parser->getOption()) ? $this->success() : $this->error($haltOnError);
98
		}
99 1
		return $this->getHelp();
100
	}
101
102
	/**
103
	 * install the database
104
	 *
105
	 * @since 3.0.0
106
	 *
107
	 * @param array $optionArray
108
	 *
109
	 * @return bool
110
	 */
111
112 2
	protected function _database(array $optionArray = []) : bool
113
	{
114 2
		$adminName = $this->prompt('admin-name', $optionArray);
115 2
		$adminUser = $this->prompt('admin-user', $optionArray);
116 2
		$adminPassword = $this->prompt('admin-password', $optionArray);
117 2
		$adminEmail = $this->prompt('admin-email', $optionArray);
118
119
		/* install */
120
121 2
		if ($adminName && $adminUser && $adminPassword && $adminEmail)
0 ignored issues
show
Bug Best Practice introduced by
The expression $adminName 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...
Bug Best Practice introduced by
The expression $adminUser 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...
Bug Best Practice introduced by
The expression $adminPassword 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...
Bug Best Practice introduced by
The expression $adminEmail 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...
122
		{
123 1
			$installer = new Installer($this->_registry, $this->_request, $this->_language, $this->_config);
124 1
			$installer->init();
125 1
			$installer->rawCreate();
126 1
			$installer->insertData(
127
			[
128 1
				'adminName' => $adminName,
129 1
				'adminUser' => $adminUser,
130 1
				'adminPassword' => $adminPassword,
131 1
				'adminEmail' => $adminEmail
132
			]);
133 1
			return Db::getStatus() === 2;
134
		}
135 1
		return false;
136
	}
137
138
	/**
139
	 * install the module
140
	 *
141
	 * @since 3.0.0
142
	 *
143
	 * @param array $optionArray
144
	 *
145
	 * @return bool
146
	 */
147
148 2
	protected function _module(array $optionArray = []) : bool
149
	{
150 2
		$alias = $this->prompt('alias', $optionArray);
151 2
		$moduleClass = 'Redaxscript\Modules\\' . $alias . '\\' . $alias;
152
153
		/* install */
154
155 2
		if (method_exists($moduleClass, 'install'))
156
		{
157 1
			$module = new $moduleClass($this->_registry, $this->_request, $this->_language, $this->_config);
158 1
			return $module->install();
159
		}
160 1
		return false;
161
	}
162
}
163