Completed
Push — master ( d5b935...21e4f3 )
by Michal
03:23
created

RedisDataManager::tables()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 45

Duplication

Lines 14
Ratio 31.11 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 14
loc 45
ccs 0
cts 28
cp 0
rs 8.5777
c 0
b 0
f 0
cc 6
nc 6
nop 1
crap 42
1
<?php
2
3
namespace UniMan\Drivers\Redis;
4
5
use RedisProxy\RedisProxy;
6
use UniMan\Core\DataManager\AbstractDataManager;
7
use UniMan\Core\Utils\Multisort;
8
use UniMan\Drivers\Redis\DataManager\RedisHashDataManager;
9
use UniMan\Drivers\Redis\DataManager\RedisKeyDataManager;
10
use UniMan\Drivers\Redis\DataManager\RedisListDataManager;
11
use UniMan\Drivers\Redis\DataManager\RedisSetDataManager;
12
use UniMan\Drivers\Redis\RedisDatabaseAliasStorage;
13
14
class RedisDataManager extends AbstractDataManager
15
{
16
    private $connection;
17
18
    private $databaseAliasStorage;
19
20
    private $itemsCountCache = false;
21
22 2
    public function __construct(RedisProxy $connection, RedisDatabaseAliasStorage $databaseAliasStorage)
23
    {
24 2
        $this->connection = $connection;
25 2
        $this->databaseAliasStorage = $databaseAliasStorage;
26 2
    }
27
28
    public function databases(array $sorting = [])
29
    {
30
        $keyspace = $this->connection->info('keyspace');
31
        $aliases = $this->databaseAliasStorage->loadAll();
32
        $databases = [];
33
        foreach ($keyspace as $db => $info) {
34
            $db = str_replace('db', '', $db);
35
            $alias = isset($aliases[$db]) ? ' (' . $aliases[$db] . ')' : '';
36
            $info['database'] = $db . $alias;
37
            $databases[$db] = $info;
38
        }
39
        return Multisort::sort($databases, $sorting);
40
    }
41
42
    protected function getDatabaseNameColumn()
43
    {
44
        return 'database';
45
    }
46
47
    public function tablesCount()
48
    {
49
        $tables = [
50
            RedisDriver::TYPE_KEY => 0,
51
            RedisDriver::TYPE_HASH => 0,
52
            RedisDriver::TYPE_SET => 0,
53
            RedisDriver::TYPE_LIST => 0,
54
        ];
55
        foreach ($this->connection->keys('*') as $key) {
56
            $type = $this->connection->type($key);
57
            switch ($type) {
58
                case RedisProxy::TYPE_STRING:
59
                    $tables[RedisDriver::TYPE_KEY]++;
60
                    break;
61
                case RedisProxy::TYPE_HASH:
62
                    $tables[RedisDriver::TYPE_HASH]++;
63
                    break;
64
                case RedisProxy::TYPE_SET:
65
                    $tables[RedisDriver::TYPE_SET]++;
66
                    break;
67
                case RedisProxy::TYPE_LIST:
68
                    $tables[RedisDriver::TYPE_LIST]++;
69
                    break;
70
                default:
71
                    break;
72
            }
73
        }
74
        return $tables;
75
    }
76
77
    public function tables(array $sorting = [])
78
    {
79
        $tables = [
80
            RedisDriver::TYPE_KEY => [
81
                'list_of_all_keys' => [
82
                    'key' => 'Show all keys',
83
                    'number_of_keys' => 0,
84
                ]
85
            ],
86
            RedisDriver::TYPE_HASH => [],
87
            RedisDriver::TYPE_SET => [],
88
            RedisDriver::TYPE_LIST => [],
89
        ];
90
        foreach ($this->connection->keys('*') as $key) {
91
            $type = $this->connection->type($key);
92
            if ($type === RedisProxy::TYPE_STRING) {
93
                $tables[RedisDriver::TYPE_KEY]['list_of_all_keys']['number_of_keys']++;
94 View Code Duplication
            } elseif ($type === RedisProxy::TYPE_HASH) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
                $result = $this->connection->hlen($key);
96
                $tables[RedisDriver::TYPE_HASH][$key] = [
97
                    'key' => $key,
98
                    'number_of_fields' => $result,
99
                ];
100
            } elseif ($type === RedisProxy::TYPE_SET) {
101
                $result = $this->connection->scard($key);
102
                $tables[RedisDriver::TYPE_SET][$key] = [
103
                    'key' => $key,
104
                    'number_of_members' => $result,
105
                ];
106 View Code Duplication
            } elseif ($type === RedisProxy::TYPE_LIST) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
107
                $result = $this->connection->llen($key);
108
                $tables[RedisDriver::TYPE_LIST][$key] = [
109
                    'key' => $key,
110
                    'number_of_elements' => $result,
111
                ];
112
            }
113
            // TODO sorted set
114
        }
115
        return [
116
            RedisDriver::TYPE_KEY => Multisort::sort($tables[RedisDriver::TYPE_KEY], $sorting),
117
            RedisDriver::TYPE_HASH => Multisort::sort($tables[RedisDriver::TYPE_HASH], $sorting),
118
            RedisDriver::TYPE_SET => Multisort::sort($tables[RedisDriver::TYPE_SET], $sorting),
119
            RedisDriver::TYPE_LIST => Multisort::sort($tables[RedisDriver::TYPE_LIST], $sorting),
120
        ];
121
    }
122
123
    public function itemsCount($type, $table, array $filter = [])
124
    {
125
        if ($this->itemsCountCache !== false) {
126
            return $this->itemsCountCache;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->itemsCountCache; (boolean) is incompatible with the return type declared by the interface UniMan\Core\DataManager\...erInterface::itemsCount of type integer.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
127
        }
128
        $itemsCount = 0;
129
        if ($type === RedisDriver::TYPE_KEY) {
130
            $manager = new RedisKeyDataManager($this->connection);
131
            $itemsCount = $manager->itemsCount($filter);
132 View Code Duplication
        } elseif ($type === RedisDriver::TYPE_HASH) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
            $manager = new RedisHashDataManager($this->connection);
134
            $itemsCount = $manager->itemsCount($table, $filter);
135
        } elseif ($type === RedisDriver::TYPE_SET) {
136
            $manager = new RedisSetDataManager($this->connection);
137
            $itemsCount = $manager->itemsCount($table, $filter);
138 View Code Duplication
        } elseif ($type === RedisDriver::TYPE_LIST) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
139
            $manager = new RedisListDataManager($this->connection);
140
            $itemsCount = $manager->itemsCount($table, $filter);
141
        }
142
        $this->itemsCountCache = $itemsCount;
0 ignored issues
show
Documentation Bug introduced by
The property $itemsCountCache was declared of type boolean, but $itemsCount is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
143
        return $itemsCount;
144
    }
145
146
    public function items($type, $table, $page, $onPage, array $filter = [], array $sorting = [])
147
    {
148
        $items = [];
149
        if ($type === RedisDriver::TYPE_KEY) {
150
            $manager = new RedisKeyDataManager($this->connection);
151
            $items = $manager->items($page, $onPage, $filter);
152 View Code Duplication
        } elseif ($type === RedisDriver::TYPE_HASH) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
153
            $manager = new RedisHashDataManager($this->connection);
154
            $items = $manager->items($table, $page, $onPage, $filter);
155
        } elseif ($type === RedisDriver::TYPE_SET) {
156
            $manager = new RedisSetDataManager($this->connection);
157
            $items = $manager->items($table, $page, $onPage, $filter);
158 View Code Duplication
        } elseif ($type === RedisDriver::TYPE_LIST) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
159
            $manager = new RedisListDataManager($this->connection);
160
            $items = $manager->items($table, $page, $onPage, $filter);
161
        }
162
163
        if ($this->itemsCount($type, $table, $filter) <= $onPage) {
164
            $items = Multisort::sort($items, $sorting);
165
        } elseif ($sorting) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $sorting of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
166
            $this->addMessage('Sorting has not been applied because the number of items is greater then the limit. Increase the limit or modify the filter.');
167
        }
168
169
        return $items;
170
    }
171
172
    public function deleteItem($type, $table, $item)
173
    {
174
        if ($type === RedisDriver::TYPE_HASH) {
175
            return $this->connection->hdel($table, $item);
176
        }
177
        if ($type === RedisDriver::TYPE_KEY) {
178
            return $this->connection->del($item);
179
        }
180
        if ($type === RedisDriver::TYPE_SET) {
181
            return $this->connection->srem($table, $item);
182
        }
183
        if ($type === RedisDriver::TYPE_LIST) {
184
            $value = md5(uniqid());
185
            $this->connection->lset($table, $item, $value);
186
            return $this->connection->lrem($table, $value);
187
        }
188
        return parent::deleteItem($type, $table, $item);
189
    }
190
191
    public function deleteTable($type, $table)
192
    {
193
        return $this->connection->del($table);
194
    }
195
196
    public function selectDatabase($database)
197
    {
198
        $this->connection->select($database);
199
    }
200
201
    public function execute($commands)
202
    {
203
        $listOfCommands = array_filter(array_map('trim', explode("\n", $commands)), function ($command) {
204
            return $command;
205
        });
206
207
        $results = [];
208
        foreach ($listOfCommands as $command) {
209
            $commandParts = explode(' ', $command);
210
            $function = array_shift($commandParts);
211
            $function = strtolower($function);
212
            $results[$command]['headers'] = $this->headers($function);
213
            $rows = call_user_func_array([$this->connection, $function], $commandParts);
214
            $items = $this->getItems($function, $rows);
215
            $results[$command]['items'] = $items;
216
            $results[$command]['count'] = count($items);
217
        }
218
        return $results;
219
    }
220
221
    private function headers($function)
222
    {
223
        if ($function === 'get' || $function === 'hget') {
224
            return ['value'];
225
        }
226
        if ($function === 'keys') {
227
            return ['key'];
228
        }
229
        if ($function === 'hgetall') {
230
            return ['key', 'value'];
231
        }
232
        if ($function === 'hlen') {
233
            return ['items_count'];
234
        }
235
        return [];
236
    }
237
238
    private function getItems($function, $rows)
239
    {
240
        $items = [];
241
        if ($function === 'keys') {
242
            foreach ($rows as $key) {
243
                $items[] = [$key];
244
            }
245
        } elseif ($function === 'hgetall') {
246
            foreach ($rows as $key => $value) {
247
                $items[] = [$key, $value];
248
            }
249
        } else {
250
            return [[$rows]];
251
        }
252
        return $items;
253
    }
254
}
255