Skip to content

Commit caacb69

Browse files
authored
feat: reduce cyklomatic complexity (#39)
1 parent 19bb148 commit caacb69

File tree

1 file changed

+105
-59
lines changed

1 file changed

+105
-59
lines changed

src/Console/Command/System/CheckCommand.php

Lines changed: 105 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -152,51 +152,71 @@ private function getLatestLtsNodeVersion(): string
152152
*/
153153
private function getShortMysqlVersion(): string
154154
{
155-
// Attempt 1: Check via PHP database connection
155+
// Try different methods to get MySQL version
156+
$version = $this->getMysqlVersionViaMagento();
157+
if (!empty($version)) {
158+
return $version;
159+
}
160+
161+
$version = $this->getMysqlVersionViaClient();
162+
if (!empty($version)) {
163+
return $version;
164+
}
165+
166+
$version = $this->getMysqlVersionViaPdo();
167+
if (!empty($version)) {
168+
return $version;
169+
}
170+
171+
return 'Unknown';
172+
}
173+
174+
/**
175+
* Get MySQL version via Magento connection
176+
*
177+
* @return string|null
178+
*/
179+
private function getMysqlVersionViaMagento(): ?string
180+
{
156181
try {
157-
// Try to use Magento connection
158182
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
159183
$resource = $objectManager->get(\Magento\Framework\App\ResourceConnection::class);
160184
$connection = $resource->getConnection();
161185
$version = $connection->fetchOne('SELECT VERSION()');
162-
if (!empty($version)) {
163-
return $version;
164-
}
186+
187+
return !empty($version) ? $version : null;
165188
} catch (\Exception $e) {
166-
// Fallback to direct PDO connection if Magento connection fails
189+
return null;
167190
}
191+
}
168192

169-
// Attempt 2: Try the MySQL client
193+
/**
194+
* Get MySQL version via command line client
195+
*
196+
* @return string|null
197+
*/
198+
private function getMysqlVersionViaClient(): ?string
199+
{
170200
exec('mysql --version 2>/dev/null', $output, $returnCode);
171201
if ($returnCode === 0 && !empty($output)) {
172202
$versionString = $output[0];
173203
preg_match('/Distrib ([0-9.]+)/', $versionString, $matches);
174-
if (isset($matches[1])) {
175-
return $matches[1];
176-
}
204+
205+
return isset($matches[1]) ? $matches[1] : null;
177206
}
178207

179-
// Attempt 3: Try generic database connection
180-
// Read ENV variables that might be present in different environments
208+
return null;
209+
}
210+
211+
/**
212+
* Get MySQL version via PDO connection
213+
*
214+
* @return string|null
215+
*/
216+
private function getMysqlVersionViaPdo(): ?string
217+
{
181218
try {
182-
$envMapping = [
183-
'host' => ['DB_HOST', 'MYSQL_HOST', 'MAGENTO_DB_HOST'],
184-
'port' => ['DB_PORT', 'MYSQL_PORT', 'MAGENTO_DB_PORT', '3306'],
185-
'user' => ['DB_USER', 'MYSQL_USER', 'MAGENTO_DB_USER'],
186-
'pass' => ['DB_PASSWORD', 'MYSQL_PASSWORD', 'MAGENTO_DB_PASSWORD'],
187-
'name' => ['DB_NAME', 'MYSQL_DATABASE', 'MAGENTO_DB_NAME'],
188-
];
189-
190-
$config = [];
191-
foreach ($envMapping as $key => $envVars) {
192-
foreach ($envVars as $env) {
193-
$value = $this->getEnvironmentVariable($env);
194-
if ($value !== null) {
195-
$config[$key] = $value;
196-
break;
197-
}
198-
}
199-
}
219+
$config = $this->getDatabaseConfig();
200220

201221
// Default values if nothing is found
202222
$host = $config['host'] ?? 'localhost';
@@ -207,14 +227,40 @@ private function getShortMysqlVersion(): string
207227
$dsn = "mysql:host=$host;port=$port";
208228
$pdo = new \PDO($dsn, $user, $pass, [\PDO::ATTR_TIMEOUT => 1]);
209229
$version = $pdo->query('SELECT VERSION()')->fetchColumn();
210-
if (!empty($version)) {
211-
return $version;
212-
}
230+
231+
return !empty($version) ? $version : null;
213232
} catch (\Exception $e) {
214-
// Ignore errors and return Unknown
233+
return null;
234+
}
235+
}
236+
237+
/**
238+
* Get database configuration from environment variables
239+
*
240+
* @return array
241+
*/
242+
private function getDatabaseConfig(): array
243+
{
244+
$envMapping = [
245+
'host' => ['DB_HOST', 'MYSQL_HOST', 'MAGENTO_DB_HOST'],
246+
'port' => ['DB_PORT', 'MYSQL_PORT', 'MAGENTO_DB_PORT', '3306'],
247+
'user' => ['DB_USER', 'MYSQL_USER', 'MAGENTO_DB_USER'],
248+
'pass' => ['DB_PASSWORD', 'MYSQL_PASSWORD', 'MAGENTO_DB_PASSWORD'],
249+
'name' => ['DB_NAME', 'MYSQL_DATABASE', 'MAGENTO_DB_NAME'],
250+
];
251+
252+
$config = [];
253+
foreach ($envMapping as $key => $envVars) {
254+
foreach ($envVars as $env) {
255+
$value = $this->getEnvironmentVariable($env);
256+
if ($value !== null) {
257+
$config[$key] = $value;
258+
break;
259+
}
260+
}
215261
}
216262

217-
return 'Unknown';
263+
return $config;
218264
}
219265

220266
/**
@@ -337,13 +383,13 @@ private function getSearchEngineFromMagentoConfig(): ?string
337383
{
338384
try {
339385
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
340-
386+
341387
// First try via deployment config
342388
$configResult = $this->checkSearchEngineViaDeploymentConfig($objectManager);
343389
if ($configResult !== null) {
344390
return $configResult;
345391
}
346-
392+
347393
// Then try via engine resolver
348394
$resolverResult = $this->checkSearchEngineViaEngineResolver($objectManager);
349395
if ($resolverResult !== null) {
@@ -355,7 +401,7 @@ private function getSearchEngineFromMagentoConfig(): ?string
355401

356402
return null;
357403
}
358-
404+
359405
/**
360406
* Check search engine via Magento deployment config
361407
*
@@ -382,10 +428,10 @@ private function checkSearchEngineViaDeploymentConfig($objectManager): ?string
382428
} catch (\Exception $e) {
383429
// Ignore specific exceptions
384430
}
385-
431+
386432
return null;
387433
}
388-
434+
389435
/**
390436
* Check search engine via Magento engine resolver
391437
*
@@ -405,7 +451,7 @@ private function checkSearchEngineViaEngineResolver($objectManager): ?string
405451
} catch (\Exception $e) {
406452
// Ignore specific exceptions
407453
}
408-
454+
409455
return null;
410456
}
411457

@@ -521,7 +567,7 @@ private function testElasticsearchConnection(string $url)
521567

522568
return false;
523569
}
524-
570+
525571
/**
526572
* Try to connect using Magento's HTTP client
527573
*
@@ -536,10 +582,10 @@ private function tryMagentoHttpClient(string $url): ?array
536582
$httpClient = $httpClientFactory->create();
537583
$httpClient->setTimeout(2);
538584
$httpClient->get($url);
539-
585+
540586
$status = $httpClient->getStatus();
541587
$response = $httpClient->getBody();
542-
588+
543589
if ($status === 200 && !empty($response)) {
544590
$data = json_decode($response, true);
545591
if (is_array($data)) {
@@ -549,7 +595,7 @@ private function tryMagentoHttpClient(string $url): ?array
549595
} catch (\Exception $e) {
550596
// Ignore exceptions
551597
}
552-
598+
553599
return null;
554600
}
555601

@@ -614,16 +660,16 @@ private function getEnvironmentVariable(string $name, ?string $default = null):
614660
if ($magentoValue !== null) {
615661
return $magentoValue;
616662
}
617-
663+
618664
// Try system environment variables
619665
$systemValue = $this->getSystemEnvironmentValue($name);
620666
if ($systemValue !== null) {
621667
return $systemValue;
622668
}
623-
669+
624670
return $default;
625671
}
626-
672+
627673
/**
628674
* Get environment variable from Magento
629675
*
@@ -634,13 +680,13 @@ private function getMagentoEnvironmentValue(string $name): ?string
634680
{
635681
try {
636682
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
637-
683+
638684
// Try via deployment config
639685
$deploymentValue = $this->getValueFromDeploymentConfig($objectManager, $name);
640686
if ($deploymentValue !== null) {
641687
return $deploymentValue;
642688
}
643-
689+
644690
// Try via environment service
645691
$serviceValue = $this->getValueFromEnvironmentService($objectManager, $name);
646692
if ($serviceValue !== null) {
@@ -649,10 +695,10 @@ private function getMagentoEnvironmentValue(string $name): ?string
649695
} catch (\Exception $e) {
650696
// Ignore exceptions
651697
}
652-
698+
653699
return null;
654700
}
655-
701+
656702
/**
657703
* Get value from Magento deployment config
658704
*
@@ -671,10 +717,10 @@ private function getValueFromDeploymentConfig($objectManager, string $name): ?st
671717
} catch (\Exception $e) {
672718
// Ignore exceptions
673719
}
674-
720+
675721
return null;
676722
}
677-
723+
678724
/**
679725
* Get value from Magento environment service
680726
*
@@ -696,13 +742,13 @@ private function getValueFromEnvironmentService($objectManager, string $name): ?
696742
} catch (\Exception $e) {
697743
// Ignore exceptions
698744
}
699-
745+
700746
return null;
701747
}
702-
748+
703749
/**
704750
* Get environment variable from the system
705-
*
751+
*
706752
* @param string $name Environment variable name
707753
* @return string|null
708754
*/
@@ -715,7 +761,7 @@ private function getSystemEnvironmentValue(string $name): ?string
715761
return $value;
716762
}
717763
}
718-
764+
719765
// Use Environment class if available (Magento 2.3+)
720766
try {
721767
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
@@ -729,7 +775,7 @@ private function getSystemEnvironmentValue(string $name): ?string
729775
} catch (\Exception $e) {
730776
// Continue with other methods
731777
}
732-
778+
733779
return null;
734780
}
735781
}

0 commit comments

Comments
 (0)