Completed
Push — master ( 974d4d...fa283c )
by Nikola
14s queued 12s
created

PredisRateLimiter::limit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
dl 12
loc 12
ccs 7
cts 7
cp 1
rs 9.8666
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RateLimit;
6
7
use Predis\ClientInterface;
8
use RateLimit\Exception\LimitExceeded;
9
10 View Code Duplication
final class PredisRateLimiter implements RateLimiter, SilentRateLimiter
0 ignored issues
show
Duplication introduced by
This class 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...
11
{
12
    /** @var ClientInterface */
13
    private $predis;
14
15
    /** @var string */
16
    private $keyPrefix;
17
18 5
    public function __construct(ClientInterface $predis, string $keyPrefix = '')
19
    {
20 5
        $this->predis = $predis;
21 5
        $this->keyPrefix = $keyPrefix;
22 5
    }
23
24 2
    public function limit(string $identifier, Rate $rate): void
25
    {
26 2
        $key = $this->key($identifier, $rate->getInterval());
27
28 2
        $current = $this->getCurrent($key);
29
30 2
        if ($current >= $rate->getOperations()) {
31 2
            throw LimitExceeded::for($identifier, $rate);
32
        }
33
34 2
        $this->updateCounter($key, $rate->getInterval());
35 2
    }
36
37 3
    public function limitSilently(string $identifier, Rate $rate): Status
38
    {
39 3
        $key = $this->key($identifier, $rate->getInterval());
40
41 3
        $current = $this->getCurrent($key);
42
43 3
        if ($current <= $rate->getOperations()) {
44 3
            $current = $this->updateCounter($key, $rate->getInterval());
45
        }
46
47 3
        return Status::from(
48 3
            $identifier,
49 3
            $current,
50 3
            $rate->getOperations(),
51 3
            time() + $this->ttl($key)
52
        );
53
    }
54
55 5
    private function key(string $identifier, int $interval): string
56
    {
57 5
        return "{$this->keyPrefix}{$identifier}:$interval";
58
    }
59
60 5
    private function getCurrent(string $key): int
61
    {
62 5
        return (int) $this->predis->get($key);
63
    }
64
65 5
    private function updateCounter(string $key, int $interval): int
66
    {
67 5
        $current = $this->predis->incr($key);
68
69 5
        if ($current === 1) {
70 5
            $this->predis->expire($key, $interval);
71
        }
72
73 5
        return $current;
74
    }
75
76 3
    private function ttl(string $key): int
77
    {
78 3
        return max((int) ceil($this->predis->pttl($key) / 1000), 0);
79
    }
80
}
81