| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace Beanie; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Beanie\Command\CommandFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Beanie\Command\CommandInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Beanie\Command\Response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Beanie\Job\Job; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Beanie\Server\Pool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Beanie\Server\TubeAwareTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Beanie\Tube\TubeAwareInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Beanie\Tube\TubeStatus; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class Producer implements TubeAwareInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     use TubeAwareTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** @var Pool */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     protected $pool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     /** @var CommandFactory */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected $commandFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** @var array */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected static $jobStateMap = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         Response::RESPONSE_INSERTED => Job::STATE_RELEASED, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         Response::RESPONSE_BURIED => Job::STATE_BURIED | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param Pool $pool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 9 |  |     public function __construct(Pool $pool) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 9 |  |         $this->pool = $pool; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 9 |  |         $this->tubeStatus = new TubeStatus(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 9 |  |         $this->commandFactory = CommandFactory::instance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 9 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param string $jobData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param int $priority | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param int $delay | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param int $timeToRun | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @return Job | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @throws Exception\InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 5 |  |     public function put( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $jobData, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $priority = Beanie::DEFAULT_PRIORITY, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $delay = Beanie::DEFAULT_DELAY, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $timeToRun = Beanie::DEFAULT_TIME_TO_RUN | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 5 |  |         return $this->createJob( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 5 |  |             $jobData, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 5 |  |             $this->pool->transformTubeStatusTo($this->tubeStatus)->dispatchCommand( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 5 |  |                 $this->commandFactory->create(CommandInterface::COMMAND_PUT, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 5 |  |                     $jobData, $priority, $delay, $timeToRun | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 5 |  |                 ]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 5 |  |             )->invoke() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 5 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @param string $jobData | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @param Response $response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @return Job | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 5 |  |     protected function createJob($jobData, Response $response) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 5 |  |         return new Job( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 5 |  |             $response->getData(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 5 |  |             $jobData, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 5 |  |             $response->getServer(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 5 |  |             self::$jobStateMap[$response->getName()] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 5 |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @param string $tubeName | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 1 |  |     public function useTube($tubeName) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |         $this->tubeStatus->setCurrentTube($tubeName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @return Pool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 94 | 1 |  |     public function getPool() | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 96 | 1 |  |         return $this->pool; | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 98 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |  |