From 0d3dae1b4a9222fe54a54fb0b04ec199fd4e9bee Mon Sep 17 00:00:00 2001 From: Tim Visser Date: Tue, 15 Nov 2022 12:33:14 +0100 Subject: [PATCH 1/6] Allow for Redis info arrays which use Clients and Server keys Signed-off-by: Tim Visser --- src/Check/Redis.php | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/src/Check/Redis.php b/src/Check/Redis.php index 00a9839..c14894a 100644 --- a/src/Check/Redis.php +++ b/src/Check/Redis.php @@ -53,13 +53,25 @@ public function check() $stats = $client->info(); $responseTime = microtime(true) - $startTime; + $successInformation = [ + "responseTime" => $responseTime, + ]; + + if (array_key_exists('connected_clients', $stats)) { + $successInformation['connections'] = (int) $stats['connected_clients']; + } elseif (array_key_exists('Clients', $stats) && array_key_exists('connected_clients', $stats['Clients'])) { + $successInformation['connections'] = (int) $stats['Clients']['connected_clients']; + } + + if (array_key_exists('uptime_in_seconds', $stats)) { + $successInformation['uptime'] = (int) $stats['uptime_in_seconds']; + } elseif (array_key_exists('Server', $stats) && array_key_exists('uptime_in_seconds', $stats['Server'])) { + $successInformation['uptime'] = (int) $stats['Server']['uptime_in_seconds']; + } + return new Success( '', - [ - "responseTime" => $responseTime, - "connections" => (int) $stats["connected_clients"], - "uptime" => (int) $stats["uptime_in_seconds"], - ] + $successInformation ); } From 2d51d4c7902092873bb13456121c2701673db3f9 Mon Sep 17 00:00:00 2001 From: Tim Visser Date: Tue, 15 Nov 2022 13:49:24 +0100 Subject: [PATCH 2/6] Import array_key_exists function Signed-off-by: Tim Visser --- src/Check/Redis.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Check/Redis.php b/src/Check/Redis.php index c14894a..766d323 100644 --- a/src/Check/Redis.php +++ b/src/Check/Redis.php @@ -8,6 +8,7 @@ use RedisException; use RuntimeException; +use function array_key_exists; use function class_exists; use function microtime; From e66b6b1f4af652f3dc9b90d6f4899276a853d358 Mon Sep 17 00:00:00 2001 From: Tim Visser Date: Tue, 17 Jan 2023 15:08:41 +0100 Subject: [PATCH 3/6] Check whether returned Redis info is an array Signed-off-by: Tim Visser --- src/Check/Redis.php | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/Check/Redis.php b/src/Check/Redis.php index 766d323..04c6b14 100644 --- a/src/Check/Redis.php +++ b/src/Check/Redis.php @@ -58,16 +58,18 @@ public function check() "responseTime" => $responseTime, ]; - if (array_key_exists('connected_clients', $stats)) { - $successInformation['connections'] = (int) $stats['connected_clients']; - } elseif (array_key_exists('Clients', $stats) && array_key_exists('connected_clients', $stats['Clients'])) { - $successInformation['connections'] = (int) $stats['Clients']['connected_clients']; - } + if (is_array($stats)) { + if (array_key_exists('connected_clients', $stats)) { + $successInformation['connections'] = (int)$stats['connected_clients']; + } elseif (array_key_exists('Clients', $stats) && array_key_exists('connected_clients', $stats['Clients'])) { + $successInformation['connections'] = (int)$stats['Clients']['connected_clients']; + } - if (array_key_exists('uptime_in_seconds', $stats)) { - $successInformation['uptime'] = (int) $stats['uptime_in_seconds']; - } elseif (array_key_exists('Server', $stats) && array_key_exists('uptime_in_seconds', $stats['Server'])) { - $successInformation['uptime'] = (int) $stats['Server']['uptime_in_seconds']; + if (array_key_exists('uptime_in_seconds', $stats)) { + $successInformation['uptime'] = (int)$stats['uptime_in_seconds']; + } elseif (array_key_exists('Server', $stats) && array_key_exists('uptime_in_seconds', $stats['Server'])) { + $successInformation['uptime'] = (int)$stats['Server']['uptime_in_seconds']; + } } return new Success( From ca849a72570c698887c2da59cd4ba4148e4b9329 Mon Sep 17 00:00:00 2001 From: Tim Visser Date: Tue, 17 Jan 2023 15:14:46 +0100 Subject: [PATCH 4/6] Follow code style in changes to Redis Check Signed-off-by: Tim Visser --- src/Check/Redis.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Check/Redis.php b/src/Check/Redis.php index 04c6b14..3204e25 100644 --- a/src/Check/Redis.php +++ b/src/Check/Redis.php @@ -10,6 +10,7 @@ use function array_key_exists; use function class_exists; +use function is_array; use function microtime; /** @@ -60,15 +61,15 @@ public function check() if (is_array($stats)) { if (array_key_exists('connected_clients', $stats)) { - $successInformation['connections'] = (int)$stats['connected_clients']; + $successInformation['connections'] = (int) $stats['connected_clients']; } elseif (array_key_exists('Clients', $stats) && array_key_exists('connected_clients', $stats['Clients'])) { - $successInformation['connections'] = (int)$stats['Clients']['connected_clients']; + $successInformation['connections'] = (int) $stats['Clients']['connected_clients']; } if (array_key_exists('uptime_in_seconds', $stats)) { - $successInformation['uptime'] = (int)$stats['uptime_in_seconds']; + $successInformation['uptime'] = (int) $stats['uptime_in_seconds']; } elseif (array_key_exists('Server', $stats) && array_key_exists('uptime_in_seconds', $stats['Server'])) { - $successInformation['uptime'] = (int)$stats['Server']['uptime_in_seconds']; + $successInformation['uptime'] = (int) $stats['Server']['uptime_in_seconds']; } } From 3a61c5a7c15ec2574b1465ee28fac6d7bfd77410 Mon Sep 17 00:00:00 2001 From: Tim Visser Date: Tue, 17 Jan 2023 15:57:47 +0100 Subject: [PATCH 5/6] Check if Client/Server info is an array when extracting Redis success information Signed-off-by: Tim Visser --- src/Check/Redis.php | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/Check/Redis.php b/src/Check/Redis.php index 3204e25..d61da3d 100644 --- a/src/Check/Redis.php +++ b/src/Check/Redis.php @@ -59,18 +59,16 @@ public function check() "responseTime" => $responseTime, ]; - if (is_array($stats)) { - if (array_key_exists('connected_clients', $stats)) { - $successInformation['connections'] = (int) $stats['connected_clients']; - } elseif (array_key_exists('Clients', $stats) && array_key_exists('connected_clients', $stats['Clients'])) { - $successInformation['connections'] = (int) $stats['Clients']['connected_clients']; - } + if (array_key_exists('connected_clients', $stats)) { + $successInformation['connections'] = (int) $stats['connected_clients']; + } elseif (array_key_exists('Clients', $stats) && is_array($stats['Clients']) && array_key_exists('connected_clients', $stats['Clients'])) { + $successInformation['connections'] = (int) $stats['Clients']['connected_clients']; + } - if (array_key_exists('uptime_in_seconds', $stats)) { - $successInformation['uptime'] = (int) $stats['uptime_in_seconds']; - } elseif (array_key_exists('Server', $stats) && array_key_exists('uptime_in_seconds', $stats['Server'])) { - $successInformation['uptime'] = (int) $stats['Server']['uptime_in_seconds']; - } + if (array_key_exists('uptime_in_seconds', $stats)) { + $successInformation['uptime'] = (int) $stats['uptime_in_seconds']; + } elseif (array_key_exists('Server', $stats) && is_array($stats['Server']) && array_key_exists('uptime_in_seconds', $stats['Server'])) { + $successInformation['uptime'] = (int) $stats['Server']['uptime_in_seconds']; } return new Success( From a327af93d3c4b79875e22e93b9d442885e773b13 Mon Sep 17 00:00:00 2001 From: Tim Visser Date: Tue, 17 Jan 2023 16:21:46 +0100 Subject: [PATCH 6/6] Spread if check over multiple lines to adhere to code style Signed-off-by: Tim Visser --- src/Check/Redis.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Check/Redis.php b/src/Check/Redis.php index d61da3d..61a7656 100644 --- a/src/Check/Redis.php +++ b/src/Check/Redis.php @@ -61,13 +61,20 @@ public function check() if (array_key_exists('connected_clients', $stats)) { $successInformation['connections'] = (int) $stats['connected_clients']; - } elseif (array_key_exists('Clients', $stats) && is_array($stats['Clients']) && array_key_exists('connected_clients', $stats['Clients'])) { + } elseif ( + array_key_exists('Clients', $stats) && + is_array($stats['Clients']) && + array_key_exists('connected_clients', $stats['Clients']) + ) { $successInformation['connections'] = (int) $stats['Clients']['connected_clients']; } if (array_key_exists('uptime_in_seconds', $stats)) { $successInformation['uptime'] = (int) $stats['uptime_in_seconds']; - } elseif (array_key_exists('Server', $stats) && is_array($stats['Server']) && array_key_exists('uptime_in_seconds', $stats['Server'])) { + } elseif ( + array_key_exists('Server', $stats) && + is_array($stats['Server']) && array_key_exists('uptime_in_seconds', $stats['Server']) + ) { $successInformation['uptime'] = (int) $stats['Server']['uptime_in_seconds']; }