| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Scheduler\Action; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Scheduler\Exception\SchedulerException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Scheduler\Job\JobInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use \DateTimeInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use SuperClosure\Serializer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * Class CallableTask | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @package Task | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @author Aleh Hutnikau, <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class CallableAction implements ActionInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     /** @var JobInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $job; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** @var DateTimeInterface */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $time; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** @var integer */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $state; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** @var mixed */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     private $report; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @return mixed|void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 5 |  |     public function __invoke() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 5 |  |         $this->state = self::STATE_IN_PROGRESS; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 5 |  |         $this->report = call_user_func($this->getJob()->getCallable(), $this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 5 |  |         $this->state = self::STATE_FINISHED; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 5 |  |         return $this->report; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * AbstractTask constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param JobInterface $job - job related to action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param DateTimeInterface $time - time of occurrence (note: this it not time of execution) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 20 |  |     public function __construct(JobInterface $job, DateTimeInterface $time) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 20 |  |         $this->state = self::STATE_INITIAL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 20 |  |         $this->job = $job; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 20 |  |         $this->time = $time; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 20 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 8 |  |     public function getTime() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 8 |  |         return $this->time; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 7 |  |     public function getJob() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 7 |  |         return $this->job; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @return mixed|void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 2 |  |     public function getId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 2 |  |         return $this->getTime()->getTimestamp().md5(implode('_', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 2 |  |             $this->getTime()->getTimestamp(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 2 |  |             $this->getJob()->getRRule()->getStartDate()->getTimestamp(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 2 |  |             $this->getJob()->getRRule()->getRrule(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 2 |  |             $this->hashCallable($this->getJob()->getCallable()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         ])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @inheritdoc | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 84 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 85 | 2 |  |     public function getState() | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 87 | 2 |  |         return $this->state; | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @inheritdoc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 2 |  |     public function getReport() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 2 |  |         if ($this->state !== self::STATE_FINISHED) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 1 |  |             throw new SchedulerException('Attempt to get report of not finished action'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 1 |  |         return $this->report; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * Get unique hash from callable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @param $callable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 2 |  |     private function hashCallable($callable) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 2 |  |         $result = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 2 |  |         if (is_string($callable)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 1 |  |             $result = $callable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 2 |  |         } else if (is_array($callable)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 2 |  |             if (is_object($callable[0])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 2 |  |                 $result = get_class(array_shift($callable)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 1 |  |                 $result = serialize(array_shift($callable)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 2 |  |             $result .= serialize($callable); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 | 1 |  |         } else if (is_object($callable) && $callable instanceof \Closure) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 | 1 |  |             $serializer = new Serializer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 1 |  |             return $serializer->serialize($callable); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 1 |  |         } else if (is_object($callable)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 | 1 |  |             $result = serialize($callable); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 2 |  |         return md5($result); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 125 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 126 |  |  | } |