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

RedisAdapter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 0
dl 0
loc 45
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0
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