| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Setono\SyliusSchedulerPlugin\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Setono\SyliusSchedulerPlugin\Doctrine\ORM\JobRepositoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Setono\SyliusSchedulerPlugin\Doctrine\ORM\ScheduleRepositoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Setono\SyliusSchedulerPlugin\Factory\JobFactoryInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Setono\SyliusSchedulerPlugin\Model\JobInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | class ScheduleJobCommand extends ContainerAwareCommand | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     protected static $defaultName = 'setono:scheduler:schedule'; | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      * @var JobFactoryInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     private $jobFactory; | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      * @var JobRepositoryInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     private $jobRepository; | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |      * @var ScheduleRepositoryInterface | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     private $scheduleRepository; | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      * @param JobFactoryInterface $jobFactory | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |      * @param JobRepositoryInterface $jobRepository | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |      * @param ScheduleRepositoryInterface $scheduleRepository | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         JobFactoryInterface $jobFactory, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         JobRepositoryInterface $jobRepository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         ScheduleRepositoryInterface $scheduleRepository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->jobFactory = $jobFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->jobRepository = $jobRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $this->scheduleRepository = $scheduleRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 49 |  |  |         parent::__construct(); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |      * @noinspection ReturnTypeCanBeDeclaredInspection | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     protected function configure() | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         $this | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             ->setDescription('Schedule jobs from the queue.') | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |             ->addOption('queue', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'Restrict to one or more queues.', []) | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         ; | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |      * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |      * @noinspection ReturnTypeCanBeDeclaredInspection | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $restrictedQueues = $input->getOption('queue'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $now = new \DateTime(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $schedules = $this->scheduleRepository->findByQueues($restrictedQueues); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         foreach ($schedules as $schedule) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             if (!$schedule->isNextJobShouldBeCreated($now)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             $job = $this->jobFactory->createFromSchedule($schedule); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             $job->setState(JobInterface::STATE_PENDING); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             $this->jobRepository->add($job); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             $output->writeln(sprintf( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |                 '%s scheduled to run at %s', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 (string) $job, | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 86 |  |  |                 null === $job->getExecuteAfter() ? '' : $job->getExecuteAfter()->format('Y-m-d H:i:s') | 
            
                                                                        
                                                                
            
                                    
            
            
                | 87 |  |  |             )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 90 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |  |