| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Dtc\QueueBundle\Redis; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Predis\Client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Predis\Response\Status; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class Predis implements RedisInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     protected $predis; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     protected $maxRetries; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 | 1 |  |     public function __construct(Client $predis, $maxRetries = 5) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 | 1 |  |         $this->predis = $predis; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 | 1 |  |         $this->maxRetries = $maxRetries; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 17 |  |     public function zAdd($zkey, $score, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 17 |  |         return $this->predis->zadd($zkey, [$value => $score]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 24 | 17 | View Code Duplication |     public function set($key, $value) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         /** @var Status $result */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 17 |  |         $result = $this->predis->set($key, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 17 |  |         if ('OK' == $result->getPayload()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 17 |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 11 |  |     public function get($key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 11 |  |         $this->predis->multi(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 11 |  |         $this->predis->exists($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 11 |  |         $this->predis->get($key); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 11 |  |         list($exists, $result) = $this->predis->exec(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 11 |  |         if (!$exists) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 1 |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 11 |  |         return $result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 48 | 1 | View Code Duplication |     public function setEx($key, $seconds, $value) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 1 |  |         $result = $this->predis->setex($key, $seconds, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |         if ('OK' == $result->getPayload()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 1 |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 13 |  |     public function lRem($lKey, $count, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 13 |  |         return $this->predis->lrem($lKey, $count, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 17 |  |     public function lPush($lKey, array $values) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 17 |  |         return $this->predis->lpush($lKey, $values); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 1 |  |     public function lRange($lKey, $start, $stop) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 1 |  |         return $this->predis->lrange($lKey, $start, $stop); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 13 |  |     public function del(array $keys) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 13 |  |         return $this->predis->del($keys); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 3 |  |     public function zRem($zkey, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 3 |  |         return $this->predis->zrem($zkey, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 13 |  |     public function zPop($key) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 13 |  |         $element = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $options = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 13 |  |             'cas' => true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 13 |  |             'watch' => $key, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 13 |  |             'retry' => $this->maxRetries, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 13 |  |             $this->predis->transaction($options, function ($tx) use ($key, &$element) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 13 |  |                 @list($element) = $tx->zrange($key, 0, 0); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 13 |  |                 if (isset($element)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 | 11 |  |                     $tx->multi(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 11 |  |                     $tx->zrem($key, $element); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 13 |  |             }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         } catch (\Exception $exception) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 | 13 |  |         return $element; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 108 | 13 |  |     public function zPopByMaxScore($key, $max) | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 110 | 13 |  |         $element = null; | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |         $options = [ | 
            
                                                        
            
                                    
            
            
                | 112 | 13 |  |             'cas' => true, | 
            
                                                        
            
                                    
            
            
                | 113 | 13 |  |             'watch' => $key, | 
            
                                                        
            
                                    
            
            
                | 114 | 13 |  |             'retry' => $this->maxRetries, | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |         ]; | 
            
                                                        
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 117 |  |  |         try { | 
            
                                                        
            
                                    
            
            
                | 118 | 13 |  |             $this->predis->transaction($options, function ($tx) use ($key, $max, &$element) { | 
            
                                                        
            
                                    
            
            
                | 119 | 13 |  |                 @list($element) = $tx->zrangebyscore($key, 0, $max, ['LIMIT' => [0, 1]]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 121 | 13 |  |                 if (isset($element)) { | 
            
                                                        
            
                                    
            
            
                | 122 | 11 |  |                     $tx->multi(); | 
            
                                                        
            
                                    
            
            
                | 123 | 11 |  |                     $tx->zrem($key, $element); | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |                 } | 
            
                                                        
            
                                    
            
            
                | 125 | 13 |  |             }); | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |         } catch (\Exception $exception) { | 
            
                                                        
            
                                    
            
            
                | 127 |  |  |             return null; | 
            
                                                        
            
                                    
            
            
                | 128 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 130 |  |  |         return $element; | 
            
                                                        
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 132 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 133 |  |  |  | 
            
                        
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.