Redis::loadLastFailureTime()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 3
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
namespace Ackintosh\Ganesha\Storage\Adapter;
4
5
use Ackintosh\Ganesha;
6
use Ackintosh\Ganesha\Configuration;
7
use Ackintosh\Ganesha\Exception\StorageException;
8
use Ackintosh\Ganesha\Storage\AdapterInterface;
9
10
class Redis implements AdapterInterface, SlidingTimeWindowInterface
11
{
12
    /**
13
     * @var \Ackintosh\Ganesha\Storage\Adapter\RedisStore
14
     */
15
    private $redis;
16
17
    /**
18
     * @var Configuration
19
     */
20
    private $configuration;
21
22
    /**
23
     * @param \Redis|\RedisArray|\RedisCluster|\Predis\Client|\Ackintosh\Ganesha\Storage\Adapter\RedisStore $redis
24
     */
25
    public function __construct($redis)
26
    {
27
        if (!($redis instanceof RedisStore)) {
28
            $redis = new RedisStore($redis);
29
        }
30
31
        $this->redis = $redis;
32
    }
33
34
    /**
35
     * @return bool
36
     */
37
    public function supportCountStrategy(): bool
38
    {
39
        return false;
40
    }
41
42
    /**
43
     * @return bool
44
     */
45
    public function supportRateStrategy(): bool
46
    {
47
        return true;
48
    }
49
50
    /**
51
     * @param Configuration $configuration
52
     *
53
     * @return void
54
     */
55
    public function setConfiguration(Configuration $configuration): void
56
    {
57
        $this->configuration = $configuration;
58
    }
59
60
    /**
61
     * @param string $service
62
     *
63
     * @return int
64
     * @throws StorageException
65
     */
66
    public function load(string $service): int
67
    {
68
        $expires = microtime(true) - $this->configuration['timeWindow'];
69
70
        if ($this->redis->zRemRangeByScore($service, '-inf', $expires) === false) {
71
            throw new StorageException('Failed to remove expired elements. service: ' . $service);
72
        }
73
74
        $r = $this->redis->zCard($service);
75
76
        if ($r === false) {
0 ignored issues
show
introduced by
The condition $r === false is always false.
Loading history...
77
            throw new StorageException('Failed to load cardinality. service: ' . $service);
78
        }
79
80
        return $r;
81
    }
82
83
    /**
84
     * @param  string $service
85
     * @param  int    $count
86
     * @return void
87
     */
88
    public function save(string $service, int $count): void
89
    {
90
        // Redis adapter does not support Count strategy
91
    }
92
93
    /**
94
     * @param string $service
95
     *
96
     * @throws StorageException
97
     */
98
    public function increment(string $service): void
99
    {
100
        $t = microtime(true);
101
        $this->redis->zAdd($service, $t, $t);
102
    }
103
104
    public function decrement(string $service): void
105
    {
106
        // Redis adapter does not support Count strategy
107
    }
108
109
    public function saveLastFailureTime(string $service, int $lastFailureTime): void
110
    {
111
        // nop
112
    }
113
114
    /**
115
     * @param $service
116
     *
117
     * @return int|null
118
     * @throws StorageException
119
     */
120
    public function loadLastFailureTime(string $service)
121
    {
122
        $lastFailure = $this->redis->zRange($service, -1, -1);
123
124
        if (!$lastFailure) {
125
            return null;
126
        }
127
128
        return (int)$lastFailure[0];
129
    }
130
131
    /**
132
     * @param string $service
133
     * @param int    $status
134
     *
135
     * @throws StorageException
136
     */
137
    public function saveStatus(string $service, int $status): void
138
    {
139
        $r = $this->redis->set($service, $status);
140
141
        if ($r === false) {
142
            throw new StorageException(sprintf(
143
                'Failed to save status. service: %s, status: %d',
144
                $service,
145
                $status
146
            ));
147
        }
148
    }
149
150
    /**
151
     * @param string $service
152
     *
153
     * @return int
154
     * @throws StorageException
155
     */
156
    public function loadStatus(string $service): int
157
    {
158
        $r = $this->redis->get($service);
159
160
        // \Redis::get() returns FALSE if key didn't exist.
161
        // @see https://github.com/phpredis/phpredis#get
162
        if ($r === false) {
163
            $this->saveStatus($service, Ganesha::STATUS_CALMED_DOWN);
164
            return Ganesha::STATUS_CALMED_DOWN;
165
        }
166
167
        return (int)$r;
168
    }
169
170
    public function reset(): void
171
    {
172
        // TODO: Implement reset() method.
173
    }
174
}
175