jaytaph /
RateLimitBundle
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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) { |
| 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
|
|||
| 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) { |
| 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 |
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.