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

PredisRateLimiter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 71
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 71
loc 71
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A limit() 12 12 2
A limitSilently() 17 17 2
A key() 4 4 1
A getCurrent() 4 4 1
A updateCounter() 10 10 2
A ttl() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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