Completed
Pull Request — master (#116)
by Joshua
37:01 queued 35:54
created

Memcache   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 64
Duplicated Lines 31.25 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 20
loc 64
ccs 32
cts 32
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getRateInfo() 0 6 1
A limitRate() 0 16 4
A createRate() 11 11 1
A resetRate() 0 5 1
A createRateInfo() 9 9 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
namespace Noxlogic\RateLimitBundle\Service\Storage;
4
5
use Noxlogic\RateLimitBundle\Service\RateLimitInfo;
6
7
class Memcache implements StorageInterface
8
{
9
    /**
10
     * @var \Memcached
11
     */
12
    protected $client;
13
14 5
    public function __construct(\Memcached $client)
15
    {
16 5
        $this->client = $client;
17 5
    }
18
19 1
    public function getRateInfo($key)
20
    {
21 1
        $info = $this->client->get($key);
22
23 1
        return $this->createRateInfo($info);
24
    }
25
26 2
    public function limitRate($key)
27
    {
28 2
        $cas = null;
29 2
        $i = 0;
30
        do {
31 2
            $info = $this->client->get($key, null, $cas);
32 2
            if (!$info) {
33 1
                return false;
34
            }
35
36 1
            $info['calls']++;
37 1
            $this->client->cas($cas, $key, $info);
38 1
        } while ($this->client->getResultCode() == \Memcached::RES_DATA_EXISTS && $i++ < 5);
39
40 1
        return $this->createRateInfo($info);
41
    }
42
43 1 View Code Duplication
    public function createRate($key, $limit, $period)
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...
44
    {
45 1
        $info = array();
46 1
        $info['limit'] = $limit;
47 1
        $info['calls'] = 1;
48 1
        $info['reset'] = time() + $period;
49
50 1
        $this->client->set($key, $info, $period);
51
52 1
        return $this->createRateInfo($info);
53
    }
54
55 1
    public function resetRate($key)
56
    {
57 1
        $this->client->delete($key);
58 1
        return true;
59
    }
60
61 3 View Code Duplication
    private function createRateInfo(array $info)
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...
62
    {
63 3
        $rateLimitInfo = new RateLimitInfo();
64 3
        $rateLimitInfo->setLimit($info['limit']);
65 3
        $rateLimitInfo->setCalls($info['calls']);
66 3
        $rateLimitInfo->setResetTimestamp($info['reset']);
67
68 3
        return $rateLimitInfo;
69
    }
70
}
71