Completed
Push — psr-cache ( 16d82e )
by
unknown
08:57 queued 14s
created

DoctrineCache::getRateInfo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 10
ccs 7
cts 7
cp 1
rs 9.9332
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Noxlogic\RateLimitBundle\Service\Storage;
4
5
use Doctrine\Common\Cache\Cache;
6
use Noxlogic\RateLimitBundle\Service\RateLimitInfo;
7
8
class DoctrineCache implements StorageInterface {
9
    /** @var \Doctrine\Common\Cache\Cache */
10
    protected $client;
11
12 5
    public function __construct(Cache $client) {
13 5
        $this->client = $client;
14 5
    }
15
16 3 View Code Duplication
    public function getRateInfo($key) {
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...
17 3
        $info = $this->client->fetch($key);
18
19 3
        $rateLimitInfo = new RateLimitInfo();
20 3
        $rateLimitInfo->setLimit($info['limit']);
21 3
        $rateLimitInfo->setCalls($info['calls']);
22 3
        $rateLimitInfo->setResetTimestamp($info['reset']);
23
24 3
        return $rateLimitInfo;
25
    }
26
27 2 View Code Duplication
    public function limitRate($key) {
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...
28 2
        $info = $this->client->fetch($key);
29 2
        if ($info === false || !array_key_exists('limit', $info)) {
30 1
            return false;
31
        }
32
33 1
        $info['calls']++;
34
35 1
        $expire = $info['reset'] - time();
36
37 1
        $this->client->save($key, $info, $expire);
38
39 1
        return $this->getRateInfo($key);
40
    }
41
42 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...
43 1
        $info          = array();
44 1
        $info['limit'] = $limit;
45 1
        $info['calls'] = 1;
46 1
        $info['reset'] = time() + $period;
47
48 1
        $this->client->save($key, $info, $period);
49
50 1
        return $this->getRateInfo($key);
51
    }
52
53 1
    public function resetRate($key) {
54 1
        $this->client->delete($key);
55 1
        return true;
56
    }
57
}
58