| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Dtc\QueueBundle\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Dtc\QueueBundle\Util\Util; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | abstract class MessageableJob extends RetryableJob | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 | 36 |  |     public function __construct(Worker $worker = null, $batch = false, $priority = 10, \DateTime $whenAt = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 | 36 |  |         parent::__construct($worker, $batch, $priority, $whenAt); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 | 36 |  |         if (!$this->getWhenAt()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 | 36 |  |             $this->setWhenAt(Util::getMicrotimeDateTime()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 | 36 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 | 33 |  |     protected function toMessageArray() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         return array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 | 33 |  |             'worker' => $this->getWorkerName(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 33 |  |             'args' => $this->getArgs(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 33 |  |             'method' => $this->getMethod(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 33 |  |             'priority' => $this->getPriority(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 33 |  |             'whenAt' => $this->getWhenAt()->format('U.u'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 33 |  |             'createdAt' => $this->getCreatedAt()->format('U.u'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 33 |  |             'updatedAt' => $this->getUpdatedAt()->format('U.u'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 33 |  |             'expiresAt' => ($expiresAt = $this->getExpiresAt()) ? $expiresAt->format('U.u') : null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 33 |  |             'retries' => $this->getRetries(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 33 |  |             'maxRetries' => $this->getMaxRetries(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 33 |  |             'failures' => $this->getFailures(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 33 |  |             'maxFailures' => $this->getMaxFailures(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 33 |  |             'exceptions' => $this->getExceptions(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 33 |  |             'maxExceptions' => $this->getMaxExceptions(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @return string A json_encoded version of a queueable version of the object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 8 |  |     public function toMessage() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 8 |  |         return json_encode($this->toMessageArray()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param string $message a json_encoded version of the object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 5 |  |     public function fromMessage($message) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 5 |  |         $arr = json_decode($message, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 5 |  |         if (is_array($arr)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 5 |  |             $this->fromMessageArray($arr); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 5 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 22 |  |     protected function fromMessageArray(array $arr) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 22 |  |         if (isset($arr['worker'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 22 |  |             $this->setWorkerName($arr['worker']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 22 |  |         if (isset($arr['args'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 22 |  |             $this->setArgs($arr['args']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 22 |  |         if (isset($arr['method'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 22 |  |             $this->setMethod($arr['method']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 22 |  |         if (isset($arr['priority'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 19 |  |             $this->setPriority($arr['priority']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 22 |  |         $this->fromMessageArrayRetries($arr); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 22 |  |         foreach (['expiresAt', 'createdAt', 'updatedAt', 'whenAt'] as $dateField) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 22 |  |             if (isset($arr[$dateField])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 22 |  |                 $timeStr = $arr[$dateField]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 22 |  |                 if ($timeStr) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 22 |  |                     $dateTime = \DateTime::createFromFormat('U.u', $timeStr); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 22 |  |                     if ($dateTime) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 22 |  |                         $method = 'set'.ucfirst($dateField); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 22 |  |                         $this->$method($dateTime); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 22 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 86 | 22 |  |     protected function fromMessagearrayRetries(array $arr) | 
            
                                                        
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 88 | 22 |  |         if (isset($arr['retries'])) { | 
            
                                                        
            
                                    
            
            
                | 89 | 22 |  |             $this->setRetries($arr['retries']); | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 91 | 22 |  |         if (isset($arr['maxRetries'])) { | 
            
                                                        
            
                                    
            
            
                | 92 | 22 |  |             $this->setMaxRetries($arr['maxRetries']); | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 94 | 22 |  |         if (isset($arr['failures'])) { | 
            
                                                        
            
                                    
            
            
                | 95 | 22 |  |             $this->setFailures($arr['failures']); | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 97 | 22 |  |         if (isset($arr['maxFailures'])) { | 
            
                                                        
            
                                    
            
            
                | 98 | 22 |  |             $this->setMaxFailures($arr['maxFailures']); | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 100 | 22 |  |         if (isset($arr['exceptions'])) { | 
            
                                                        
            
                                    
            
            
                | 101 | 22 |  |             $this->setExceptions($arr['exceptions']); | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 103 | 22 |  |         if (isset($arr['maxExceptions'])) { | 
            
                                                        
            
                                    
            
            
                | 104 | 22 |  |             $this->setMaxExceptions($arr['maxExceptions']); | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 106 | 22 |  |     } | 
            
                                                        
            
                                    
            
            
                | 107 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |  |