Test Setup Failed
Push — master ( 8c5f4b...dcf915 )
by Alexpts
03:11
created

RedisAdapter::inc()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace PTS\RateLimiter\Adapter;
5
6
use PTS\RateLimiter\StoreInterface;
7
use Redis;
8
9
class RedisAdapter implements StoreInterface
10
{
11
    protected Redis $redis;
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING, expecting T_FUNCTION or T_CONST
Loading history...
12
13
14
    public function __construct(Redis $redis)
15
    {
16
        $this->redis = $redis;
17 6
    }
18
19 6
    public function get(string $key): int
20 6
    {
21
        return (int)$this->redis->get($key);
22 4
    }
23
24 4
    public function inc(string $key, int $ttl = 60): int
25
    {
26
        $value = $this->redis->incr($key);
27 5
        if ($value === 1) {
28
            $this->redis->expire($key, $ttl);
29 5
        }
30 5
31 5
        return $value;
32
    }
33
34 5
    public function reset(string $key): bool
35
    {
36
        return (bool)$this->redis->del($key);
37 6
    }
38
39 6
    public function isExceeded(string $key, int $max): bool
40
    {
41
        $value = $this->get($key);
42 1
        return $value >= $max;
43
    }
44 1
45 1
    public function ttl(string $key): ?int
46
    {
47
        $ttl = $this->redis->ttl($key);
48 1
        return $ttl > 0 ? $ttl : null;
49
    }
50
}
51