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

RedisListDataManager::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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