| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace FOS\ElasticaBundle\Propel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use FOS\ElasticaBundle\Provider\AbstractProvider; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Propel provider. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @author William Durand <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class Provider extends AbstractProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     public function doPopulate($options, \Closure $loggerClosure = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         $queryClass = $this->objectClass.'Query'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         $nbObjects = $queryClass::create()->count(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $offset = $options['offset']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         for (; $offset < $nbObjects; $offset += $options['batch_size']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |             $objects = $queryClass::create() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |                 ->limit($options['batch_size']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |                 ->offset($offset) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |                 ->find() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |                 ->getArrayCopy(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |             $sliceSize = count($objects); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |             $objects = $this->filterObjects($options, $objects); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             if (!empty($objects)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                 $this->objectPersister->insertMany($objects); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             usleep($options['sleep']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             if ($loggerClosure) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |                 $loggerClosure($sliceSize, $nbObjects); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     protected function disableLogging() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     protected function enableLogging($logger) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * {@inheritDoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                        
            
                                                                    
                                                                                                        
            
            
                | 61 |  | View Code Duplication |     protected function configureOptions() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 63 |  |  |         parent::configureOptions(); | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 65 |  |  |         $this->resolver->setDefaults(array( | 
            
                                                        
            
                                    
            
            
                | 66 |  |  |             'clear_object_manager' => true, | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |             'debug_logging'        => false, | 
            
                                                        
            
                                    
            
            
                | 68 |  |  |             'ignore_errors'        => false, | 
            
                                                        
            
                                    
            
            
                | 69 |  |  |             'offset'               => 0, | 
            
                                                        
            
                                    
            
            
                | 70 |  |  |             'query_builder_method' => 'createQueryBuilder', | 
            
                                                        
            
                                    
            
            
                | 71 |  |  |             'sleep'                => 0 | 
            
                                                        
            
                                    
            
            
                | 72 |  |  |         )); | 
            
                                                        
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 74 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 75 |  |  |  | 
            
                        
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.