| 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 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 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 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 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 |  |  |  | 
            
                        
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.