Completed
Pull Request — master (#18)
by
unknown
04:28
created

MemcachedRateLimiter   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 74
Duplicated Lines 39.19 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 29
loc 74
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A limit() 12 12 2
A limitSilently() 17 17 2
A key() 0 4 1
A getCurrent() 0 4 1
A updateCounter() 0 11 2
A ttl() 0 6 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 RateLimit\Exception\LimitExceeded;
8
use Memcached;
9
10
final class MemcachedRateLimiter implements RateLimiter, SilentRateLimiter
11
{
12
    /** @var Memcached */
13
    private $memcached;
14
15
    /** @var string */
16
    private $keyPrefix;
17
18 5
    public function __construct(Memcached $memcached, string $keyPrefix = '')
19
    {
20 5
        $this->memcached = $memcached;
21 5
        $this->keyPrefix = $keyPrefix;
22 5
    }
23
24 2 View Code Duplication
    public function limit(string $identifier, Rate $rate): void
0 ignored issues
show
Duplication introduced by
This method 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...
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 View Code Duplication
    public function limitSilently(string $identifier, Rate $rate): Status
0 ignored issues
show
Duplication introduced by
This method 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...
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->memcached->get($key);
63
    }
64
65 5
    private function updateCounter(string $key, int $interval): int
66
    {
67 5
        $this->memcached->increment($key, 1, 1);
68 5
        $current = $this->getCurrent($key);
69
70 5
        if ($current === 1) {
71 5
            $this->memcached->touch($key, $interval);
72
        }
73
74 5
        return $current;
75
    }
76
77 3
    private function ttl(string $key): int
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
78
    {
79
        // memcached doesn't support retrieving TTL by key
80
        // of course there are crutches, but we are normal developers
81 3
        return 0;
82
    }
83
}
84