Completed
Push — master ( a52438...bf8826 )
by Henry
06:30
created

includes/Console/Command/Status.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\Auth;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Redaxscript\Console\Command\Auth.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
5
use Redaxscript\Console\Parser;
6
use Redaxscript\Db;
7
use function array_key_exists;
8
use function in_array;
9
use function is_array;
10
use function str_pad;
11
12
/**
13
 * children class to execute the status command
14
 *
15
 * @since 3.0.0
16
 *
17
 * @package Redaxscript
18
 * @category Console
19
 * @author Henry Ruhs
20
 */
21
22
class Status extends CommandAbstract
23
{
24
	/**
25
	 * array of the command
26
	 *
27
	 * @var array
28
	 */
29
30
	protected $_commandArray =
31
	[
32
		'status' =>
33
		[
34
			'description' => 'Status command',
35
			'argumentArray' =>
36
			[
37
				'auth' =>
38
				[
39
					'description' => 'Show auth status',
40
					'wordingArray' =>
41
					[
42
						'Not logged in',
43
						'Logged in'
44
					]
45
				],
46
				'database' =>
47
				[
48
					'description' => 'Show database status',
49
					'wordingArray' =>
50
					[
51
						'No connection found',
52
						'Database tables missing',
53
						'Connection established'
54
					]
55
				],
56
				'system' =>
57
				[
58
					'description' => 'Show system requirements',
59
					'wordingArray' =>
60
					[
61
						'Failed',
62
						'Passed'
63
					]
64
				]
65
			]
66
		]
67
	];
68
69
	/**
70
	 * run the command
71
	 *
72
	 * @since 3.0.0
73
	 *
74
	 * @param string $mode name of the mode
75
	 *
76
	 * @return string|null
77
	 */
78
79 3
	public function run(string $mode = null) : ?string
80
	{
81 3
		$parser = new Parser($this->_request);
82 3
		$parser->init($mode);
83
84
		/* run command */
85
86 3
		$argumentKey = $parser->getArgument(1);
87 3
		if ($argumentKey === 'auth')
88
		{
89
			return $this->_auth();
90
		}
91 3
		if ($argumentKey === 'database')
92
		{
93 1
			return $this->_database();
94
		}
95 2
		if ($argumentKey === 'system')
96
		{
97 1
			return $this->_system();
98
		}
99 1
		return $this->getHelp();
100
	}
101
102
	/**
103
	 * auth status
104
	 *
105
	 * @since 4.5.0
106
	 *
107
	 * @return string|null
108
	 */
109
110
	protected function _auth() : ?string
111
	{
112
		$output = null;
113
		$auth = new Auth($this->_request);
114
		$status = $auth->getStatus();
115
		$wordingArray = $this->_commandArray['status']['argumentArray']['auth']['wordingArray'];
116
		if (is_array($wordingArray) && array_key_exists($status, $wordingArray))
117
		{
118
			$output = $wordingArray[$status] . PHP_EOL;
119
		}
120
		return $output;
121
	}
122
123
	/**
124
	 * database status
125
	 *
126
	 * @since 3.0.0
127
	 *
128
	 * @return string|null
129
	 */
130
131 1
	protected function _database() : ?string
132
	{
133 1
		$output = null;
134 1
		$status = Db::getStatus();
135 1
		$wordingArray = $this->_commandArray['status']['argumentArray']['database']['wordingArray'];
136 1
		if (is_array($wordingArray) && array_key_exists($status, $wordingArray))
137
		{
138 1
			$output = $wordingArray[$status] . PHP_EOL;
139
		}
140 1
		return $output;
141
	}
142
143
	/**
144
	 * system status
145
	 *
146
	 * @since 3.0.0
147
	 *
148
	 * @return string|null
149
	 */
150
151 1
	protected function _system() : ?string
152
	{
153 1
		$output = null;
154 1
		$statusArray = $this->_getStatusArray();
155 1
		$wordingArray = $this->_commandArray['status']['argumentArray']['system']['wordingArray'];
156
157
		/* process status */
158
159 1
		foreach ($statusArray as $key => $valueArray)
160
		{
161 1
			if (is_array($valueArray) && array_key_exists('value', $valueArray) && array_key_exists('status', $valueArray))
162
			{
163 1
				$output .= str_pad($key, 30) . str_pad($valueArray['value'], 60) . $wordingArray[$valueArray['status']] . PHP_EOL;
164
			}
165
		}
166 1
		return $output;
167
	}
168
169
	/**
170
	 * get the status array
171
	 *
172
	 * @since 3.0.0
173
	 *
174
	 * @return array
175
	 */
176
177 1
	protected function _getStatusArray() : array
178
	{
179 1
		$phpOs = $this->_registry->get('phpOs');
180 1
		$driverArray = $this->_registry->get('driverArray');
181
		$testOsArray =
182
		[
183 1
			'linux',
184
			'windows'
185
		];
186
		$testDriverArray =
187
		[
188 1
			'mssql',
189
			'mysql',
190
			'pgsql',
191
			'sqlite'
192
		];
193
		$statusArray =
194
		[
195
			'OS' =>
196
			[
197 1
				'value' => $this->_registry->get('phpOs'),
198 1
				'status' => in_array($phpOs, $testOsArray) ? 1 : 0
199
			],
200
			'PHP' =>
201
			[
202 1
				'value' => $this->_registry->get('phpVersion'),
203 1
				'status' => $this->_registry->get('phpStatus') ? 1 : 0
204
			],
205
			'SESSION' =>
206
			[
207 1
				'value' => $this->_registry->get('sessionId'),
208 1
				'status' => $this->_registry->get('sessionStatus') ? 1 : 0
209
			]
210
		];
211
212
		/* process driver */
213
214 1
		if (is_array($driverArray))
215
		{
216 1
			foreach ($testDriverArray as $value)
217
			{
218 1
				$statusArray[$value] =
219
				[
220 1
					'status' => in_array($value, $driverArray) ? 1 : 0
221
				];
222
			}
223
		}
224 1
		return $statusArray;
225
	}
226
}
227