| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @author    Flipbox Factory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @copyright Copyright (c) 2017, Flipbox Digital | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @link      https://github.com/flipbox/queue/releases/latest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @license   https://github.com/flipbox/queue/blob/master/LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace flipbox\queue\strategies; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use flipbox\queue\jobs\JobInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use flipbox\queue\queues\MultipleQueueInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use yii\base\Object; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @author Flipbox Factory <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @since 1.0.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | abstract class AbstractStrategy extends Object implements StrategyInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var MultipleQueueInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     protected $queue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * Sets the queue. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param MultipleQueueInterface $queue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function setQueue(MultipleQueueInterface $queue) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->queue = $queue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * Implement this for the strategy of getting job from the queue. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @return JobInterface[]|false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     abstract protected function getJobFromQueues(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * Returns the job. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @return JobInterface|boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     public function fetch() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $return = $this->getJobFromQueues(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         if ($return === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         list($job, $index) = $return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $job->setHeader(self::HEADER_MULTIPLE_QUEUE_INDEX, $index); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         return $job; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * Delete the job from the queue. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @param JobInterface $job The job. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @return boolean whether the operation succeed. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |     public function delete(JobInterface $job) | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |         $index = $job->getHeader(self::HEADER_MULTIPLE_QUEUE_INDEX, null); | 
            
                                                        
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |         if (!isset($index)) { | 
            
                                                        
            
                                    
            
            
                | 76 |  |  |             return false; | 
            
                                                        
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |         $queue = $this->queue->getQueue($index); | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 81 |  |  |         if (!isset($index)) { | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |             return false; | 
            
                                                        
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 85 |  |  |         return $queue->delete($job); | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 87 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |  |