Passed
Push — master ( 65709d...74d483 )
by bader
04:24 queued 10s
created

RedisEngine::setExpiration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 1
b 0
f 1
1
<?php
2
namespace Awssat\Visits\DataEngines;
3
4
5
use Illuminate\Contracts\Redis\Factory;
6
7
class RedisEngine implements DataEngine
8
{
9
    private $redis = null;
10
    private $connection = null;
11
    private $prefix = null;
12
13
    public function __construct(Factory $redis)
14
    {
15
        $this->redis = $redis;
16
    }
17
18
    public function connect(string $connection): DataEngine
19
    {
20
        $this->connection = $this->redis->connection($connection);
21
        return $this;
22
    }
23
24
    public function setPrefix(string $prefix): DataEngine
25
    {
26
        $this->prefix = $prefix . ':';
27
        return $this;
28
    }
29
30
    public function increment(string $key, int $value, ?string $member = null): bool
31
    {
32
        if (! empty($member) || is_numeric($member)) {
33
            $this->connection->zincrby($this->prefix.$key, $value, $member);
34
        } else {
35
            $this->connection->incrby($this->prefix.$key, $value);
36
        }
37
        
38
        // both methods returns integer and raise an excpetion in case of an error.
39
        return true;
40
    }
41
42
    public function decrement(string $key, int $value, ?string $member = null): bool
43
    {
44
        return $this->increment($key, -$value, $member);
45
    }
46
47
    public function delete($key, ?string $member = null): bool
48
    {
49
        if(is_array($key)) {
50
            array_walk($key, function($item) {
51
                $this->delete($item);
52
            });
53
            return true;
54
        }
55
56
        if(! empty($member) || is_numeric($member)) {
57
            return $this->connection->zrem($this->prefix.$key, $member) > 0;
58
        } else {
59
            return $this->connection->del($this->prefix.$key) > 0;
60
        }
61
    }
62
63
    public function get(string $key, ?string $member = null)
64
    {
65
        if(! empty($member) || is_numeric($member)) {
66
            return $this->connection->zscore($this->prefix.$key, $member);
67
        } else {
68
            return $this->connection->get($this->prefix.$key);
69
        }
70
    }
71
72
    public function set(string $key, $value, ?string $member = null): bool
73
    {
74
        if(! empty($member) || is_numeric($member)) {
75
            return $this->connection->zAdd($this->prefix.$key, $value, $member) > 0;
76
        } else {
77
            return (bool) $this->connection->set($this->prefix.$key, $value);
78
        }
79
    }
80
81
    public function search(string $word, bool $noPrefix = true): array
82
    {
83
        return array_map(
84
            function($item) use($noPrefix) {
85
                if ($noPrefix && substr($item, 0, strlen($this->prefix)) == $this->prefix) {
86
                    return substr($item, strlen($this->prefix));
87
                } 
88
89
                return $item;
90
            }, 
91
            $this->connection->keys($this->prefix.$word) ?? []
92
        );
93
    }
94
95
    public function flatList(string $key, int $limit = -1): array
96
    {
97
        return $this->connection->lrange($this->prefix.$key, 0, $limit);
98
    }
99
100
    public function addToFlatList(string $key, $value): bool
101
    {
102
        return $this->connection->rpush($this->prefix.$key, $value) !== false;
103
    }
104
105
    public function valueList(string $key, int $limit = -1, bool $orderByAsc = false, bool $withValues = false): array
106
    {
107
        $range = $orderByAsc ? 'zrange' : 'zrevrange';
108
109
        return $this->connection->$range($this->prefix.$key, 0, $limit,  ['withscores' => $withValues]);
110
    }
111
112
    public function exists(string $key): bool
113
    {
114
        return (bool) $this->connection->exists($this->prefix.$key);
115
    }
116
117
    public function timeLeft(string $key): int
118
    {
119
        return $this->connection->ttl($this->prefix.$key);
120
    }
121
122
    public function setExpiration(string $key, int $time): bool
123
    {
124
        return $this->connection->expire($this->prefix.$key, $time);
125
    }
126
}