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

Memcache::resetRate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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