Completed
Branch master (f4cf4a)
by Michal
02:08
created

InfoHelper::createInfoForRedis()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 7

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 12
cts 12
cp 1
rs 8.2222
c 0
b 0
f 0
cc 7
eloc 12
nc 3
nop 2
crap 7
1
<?php
2
3
namespace RedisProxy;
4
5
use Predis\Client;
6
use Redis;
7
8
class InfoHelper
9
{
10
    private static $keyStartToSectionMap = [
11
        'redis_' => 'server',
12
        'uptime_' => 'server',
13
        'client_' => 'clients',
14
        'used_memory' => 'memory',
15
        'mem_' => 'memory',
16
        'rdb_' => 'persistence',
17
        'aof_' => 'persistence',
18
        'total_' => 'stats',
19
        'sync_' => 'stats',
20
        'keyspace_' => 'stats',
21
        'pubsub_' => 'stats',
22
        'repl_backlog_' => 'replication',
23
        'used_cpu_' => 'cpu',
24
        'db' => 'keyspace',
25
    ];
26
27
    private static $keyToSectionMap = [
28
        'os' => 'server',
29
        'arch_bits' => 'server',
30
        'multiplexing_api'  => 'server',
31
        'gcc_version' => 'server',
32
        'process_id' => 'server',
33
        'run_id' => 'server',
34
        'tcp_port' => 'server',
35
        'hz' => 'server',
36
        'lru_clock' => 'server',
37
        'config_file' => 'server',
38
        'connected_clients' => 'clients',
39
        'blocked_clients' => 'clients',
40
        'loading' => 'persistence',
41
        'instantaneous_ops_per_sec' => 'stats',
42
        'rejected_connections' => 'stats',
43
        'expired_keys' => 'stats',
44
        'evicted_keys' => 'stats',
45
        'latest_fork_usec' => 'stats',
46
        'role' => 'replication',
47
        'connected_slaves' => 'replication',
48
        'master_repl_offset' => 'replication',
49
    ];
50
51
    /**
52
     * @param Client|Redis $driver
53
     * @param array $result
54
     * @param integer|null $databases
55
     * @return array
56
     */
57 8
    public static function createInfoArray($driver, array $result, $databases = null)
58
    {
59 8
        $groupedResult = [];
60 8
        self::initializeKeyspace($groupedResult, $databases);
61
62 8
        if ($driver instanceof Client) {
63 4
            return self::createInfoForPredis($result, $groupedResult);
64
        }
65
66 4
        return self::createInfoForRedis($result, $groupedResult);
67
    }
68
69 8
    private static function initializeKeyspace(&$groupedResult, $databases = null)
70
    {
71 8
        if ($databases === null) {
72 8
            return;
73
        }
74 4
        $groupedResult['keyspace'] = [];
75 4
        for ($db = 0; $db < $databases; ++$db) {
76 4
            $groupedResult['keyspace']["db$db"] = [
77
                'keys' => 0,
78
                'expires' => null,
79
                'avg_ttl' => null,
80
            ];
81
        }
82 4
    }
83
84 4
    private static function createInfoForPredis(array $result, array $groupedResult)
85
    {
86 4
        $result = array_change_key_case($result, CASE_LOWER);
87 4
        if (isset($groupedResult['keyspace']) && isset($result['keyspace'])) {
88 2
            $groupedResult['keyspace'] = array_merge($groupedResult['keyspace'], $result['keyspace']);
89 2
            unset($result['keyspace']);
90
        }
91 4
        return array_merge($groupedResult, $result);
92
    }
93
94 4
    private static function createInfoForRedis(array $result, array $groupedResult)
95
    {
96 4
        foreach ($result as $key => $value) {
97 2
            if (isset(self::$keyToSectionMap[$key])) {
98 2
                $groupedResult[self::$keyToSectionMap[$key]][$key] = $value;
99 2
                continue;
100
            }
101
102 2
            foreach (self::$keyStartToSectionMap as $keyStart => $targetSection) {
103 2
                if (strpos($key, $keyStart) === 0 && $keyStart === 'db') {
104 2
                    $value = self::createKeyspaceInfo($value);
105
                }
106 2
                if (strpos($key, $keyStart) === 0) {
107 2
                    $groupedResult[$targetSection][$key] = $value;
108 2
                    continue;
109
                }
110
            }
111
        }
112 4
        return $groupedResult;
113
    }
114
115 2
    private static function createKeyspaceInfo($keyspaceInfo)
116
    {
117 2
        list($keys, $expires, $avgTtl) = explode(',', $keyspaceInfo);
118
        return [
119 2
            'keys' => strpos($keys, '=') !== false ? explode('=', $keys)[1] : 0,
120 2
            'expires' => strpos($expires, '=') !== false ? explode('=', $expires)[1] : null,
121 2
            'avg_ttl' => strpos($avgTtl, '=') !== false ? explode('=', $avgTtl)[1] : null,
122
        ];
123
    }
124
}
125