Completed
Push — master ( 921d57...18f6a2 )
by Michal
02:03
created

RedisSortedSetDataManager::__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 RedisSortedSetDataManager
9
{
10
    private $connection;
11
12
    public function __construct(RedisProxy $connection)
13
    {
14
        $this->connection = $connection;
15
    }
16
17 View Code Duplication
    public function itemsCount(string $table, array $filter): int
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
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->zcard($table);
21
        }
22
        $iterator = '';
23
        $totalItems = 0;
24
        do {
25
            $res = $this->connection->zscan($table, $iterator, null, 1000);
26
            if (!$res) {
27
                return $totalItems;
28
            }
29
            foreach ($res as $member => $score) {
30
                $item = [
31
                    'member' => $member,
32
                    'length' => strlen($member),
33
                    'score' => $score,
34
                ];
35
                if (Filter::apply($item, $filter)) {
36
                    $totalItems++;
37
                }
38
            }
39
        } while ($iterator !== 0);
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison !== seems to always evaluate to true as the types of $iterator (string) and 0 (integer) can never be identical. Maybe you want to use a loose comparison != instead?
Loading history...
40
        return $totalItems;
41
    }
42
43 View Code Duplication
    public function items(string $table, int $page, int $onPage, array $filter = []): array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
44
    {
45
        $skipped = 0;
46
        $offset = ($page - 1) * $onPage;
47
        $items = [];
48
        $iterator = '';
49
        do {
50
            $pattern = null;
51
            $res = $this->connection->zscan($table, $iterator, $pattern, $onPage * 10);
52
            if (!$res) {
53
                return $items;
54
            }
55
            foreach ($res as $member => $score) {
56
                $item = [
57
                    'member' => $member,
58
                    'length' => strlen($member),
59
                    'score' => $score,
60
                ];
61
                if (!Filter::apply($item, $filter)) {
62
                    continue;
63
                }
64
65
                if ($skipped < $offset) {
66
                    $skipped++;
67
                    continue;
68
                }
69
70
                $items[$member] = $item;
71
                if (count($items) === $onPage) {
72
                    break;
73
                }
74
            }
75
        } while ($iterator !== 0 && count($items) < $onPage);
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison !== seems to always evaluate to true as the types of $iterator (string) and 0 (integer) can never be identical. Maybe you want to use a loose comparison != instead?
Loading history...
76
        return $items;
77
    }
78
}
79