| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Dtc\QueueBundle\ODM; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Doctrine\ODM\MongoDB\DocumentManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Dtc\GridBundle\Grid\Column\GridColumn; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Dtc\GridBundle\Grid\Source\DocumentGridSource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Dtc\QueueBundle\Model\BaseJob; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class LiveJobsGridSource extends DocumentGridSource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     protected $jobManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     protected $running = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     public function getId() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         return 'dtc_queue.grid_source.jobs_'.($this->isRunning() ? 'running' : 'waiting').'.odm'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public function setRunning($flag) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $this->running = $flag; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     public function isRunning() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         return $this->running; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     protected function getRunningQueryBuilder() | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |         /** @var DocumentManager $documentManager */ | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |         $documentManager = $this->jobManager->getObjectManager(); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         $builder = $documentManager->createQueryBuilder($this->jobManager->getJobClass()); | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |         $builder->field('status')->equals(BaseJob::STATUS_RUNNING); | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         $builder->sort('startedAt', 'desc'); | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |         $builder->limit($this->limit); | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         $builder->skip($this->offset); | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |         return $builder; | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public function __construct(JobManager $jobManager) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->jobManager = $jobManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         /** @var DocumentManager $documentManager */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $documentManager = $jobManager->getObjectManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         parent::__construct($documentManager, $jobManager->getJobClass()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     public function getColumns() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         if ($columns = parent::getColumns()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             return $columns; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $this->autoDiscoverColumns(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         return parent::getColumns(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 62 |  | View Code Duplication |     public function autoDiscoverColumns() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         parent::autoDiscoverColumns(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         if ($this->columns) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             foreach ($this->columns as $column) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |                 if ($column instanceof GridColumn) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |                     $column->setOption('sortable', false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     public function getDefaultSort() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 79 |  | View Code Duplication |     protected function getQueryBuilder() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         if ($this->isRunning()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             return $this->getRunningQueryBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $builder = $this->jobManager->getJobQueryBuilder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $builder->limit($this->limit); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $builder->skip($this->offset); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         return $builder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 90 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |  | 
            
                        
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.