Completed
Push — master ( 85ddb2...d5b935 )
by Michal
11:09
created

RedisKeyDataManager::items()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 33
ccs 0
cts 20
cp 0
rs 8.7697
c 0
b 0
f 0
cc 6
nc 6
nop 3
crap 42
1
<?php
2
3
namespace UniMan\Drivers\Redis\DataManager;
4
5
use RedisProxy\RedisProxy;
6
use UniMan\Core\Utils\Filter;
7
8
class RedisKeyDataManager
9
{
10
    private $connection;
11
12
    public function __construct(RedisProxy $connection)
13
    {
14
        $this->connection = $connection;
15
    }
16
17
    public function itemsCount(array $filter): int
18
    {
19
        $totalItems = 0;
20
        foreach ($this->connection->keys('*') as $key) {
21
            if ($this->connection->type($key) !== RedisProxy::TYPE_STRING) {
22
                continue;
23
            }
24
            $result = $this->connection->get($key);
25
            $item = [
26
                'key' => $key,
27
                'value' => $result,
28
                'length' => strlen($result),
29
            ];
30
31
            if (Filter::apply($item, $filter)) {
32
                $totalItems++;
33
            }
34
        }
35
        return $totalItems;
36
    }
37
38
    public function items(int $page, int $onPage, array $filter = []): array
39
    {
40
        $skipped = 0;
41
        $offset = ($page - 1) * $onPage;
42
        $items = [];
43
        foreach ($this->connection->keys('*') as $key) {
44
            if ($this->connection->type($key) !== RedisProxy::TYPE_STRING) {
45
                continue;
46
            }
47
            $result = $this->connection->get($key);
48
49
            $item = [
50
                'key' => $key,
51
                'value' => $result,
52
                'length' => strlen($result),
53
            ];
54
55
            if (!Filter::apply($item, $filter)) {
56
                continue;
57
            }
58
59
            if ($skipped < $offset) {
60
                $skipped++;
61
                continue;
62
            }
63
            
64
            $items[$key] = $item;
65
            if (count($items) === $onPage) {
66
                break;
67
            }
68
        }
69
        return $items;
70
    }
71
}
72