| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Shippinno\Job\Infrastructure\Ui\Console\Laravel\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Doctrine\Common\Persistence\ManagerRegistry; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Console\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use LogicException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Psr\Log\LoggerAwareTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Psr\Log\LoggerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Psr\Log\NullLogger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Shippinno\Job\Application\Messaging\ConsumeStoredJobService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Shippinno\Job\Infrastructure\Persistence\Doctrine\ManagerRegistryAwareTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Throwable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | class JobConsume extends Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     use ManagerRegistryAwareTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     use LoggerAwareTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     protected $signature = 'job:consume'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @var ConsumeStoredJobService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     private $consumeStoredJobService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private $terminating = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param ConsumeStoredJobService $consumeStoredJobService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param ManagerRegistry|null $managerRegistry | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param LoggerInterface|null $logger | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 40 | 2 | View Code Duplication |     public function __construct( | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         ConsumeStoredJobService $consumeStoredJobService, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         ManagerRegistry $managerRegistry = null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         LoggerInterface $logger = null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 2 |  |         parent::__construct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 2 |  |         $this->consumeStoredJobService = $consumeStoredJobService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 2 |  |         $this->setManagerRegistry($managerRegistry); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 2 |  |         $this->setLogger(null !== $logger ? $logger : new NullLogger); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 2 |  |     public function handle() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 53 | 2 | View Code Duplication |         if (extension_loaded('pcntl')) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 2 |  |             pcntl_async_signals(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 2 |  |             pcntl_signal(SIGTERM, [$this, 'terminate']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 2 |  |             pcntl_signal(SIGINT, [$this, 'terminate']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 2 |  |         $queueName = $this->queueName(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |         $forever = !env('JOB_TESTING', false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         do { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |             $this->clear(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |             $this->consumeStoredJobService->execute( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |                 $queueName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 function () { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                     try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                         $this->flush(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                     } catch (Throwable $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                     return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 1 |  |                 }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 function () { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                     $this->clear(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |         } while ($forever && !$this->terminating); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 2 |  |     private function queueName(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 2 |  |         $queueName = env('JOB_CONSUME_QUEUE'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 2 |  |         if (!$queueName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 1 |  |             throw new LogicException('The env JOB_CONSUME_QUEUE is not defined'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 1 |  |         return $queueName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |     public function terminate() | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |         $this->logger->info('Terminating job:consume.'); | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |         $this->terminating = true; | 
            
                                                        
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 99 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 100 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.