| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Victoire\Bundle\CoreBundle\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Doctrine\ORM\EntityManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Victoire\Bundle\CoreBundle\Entity\View; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Victoire\Bundle\TemplateBundle\Entity\Template; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class WidgetCssGenerateCommand extends ContainerAwareCommand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     public function configure() | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |         parent::configure(); | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |         $this | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |             ->setName('victoire:widget-css:generate') | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |             ->addOption('outofdate', null, InputOption::VALUE_NONE, 'Generate CSS only for View\'s CSS not up to date') | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |             ->addOption('limit', 'l', InputOption::VALUE_REQUIRED, 'Number of generated View\'s CSS') | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |             ->setDescription('Generate widgets CSS for each View'); | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * Generate a css file containing all css parameters for all widgets used in each view. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param InputInterface  $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         //Get options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $outOfDate = $input->getOption('outofdate'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $limit = $input->getOption('limit'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         //Get services | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         /** @var EntityManager $entityManager */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $entityManager = $this->getContainer()->get('doctrine.orm.entity_manager'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $viewCssBuilder = $this->getContainer()->get('victoire_core.view_css_builder'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $widgetMapBuilder = $this->getContainer()->get('victoire_widget_map.builder'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $widgetRepo = $entityManager->getRepository('Victoire\Bundle\WidgetBundle\Entity\Widget'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | //        //Get View's CSS to generate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $templateRepo = $entityManager->getRepository('VictoireTemplateBundle:Template'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $rootTemplates = $templateRepo->getInstance() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             ->where('template.template IS NULL') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             ->getQuery() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             ->getResult(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $templates = []; | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 61 |  | View Code Duplication |         $recursiveGetTemplates = function ($template) use (&$recursiveGetTemplates, &$templates) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             array_push($templates, $template); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             foreach ($template->getInheritors() as $template) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |                 if ($template instanceof Template) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                     $recursiveGetTemplates($template); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         }; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         foreach ($rootTemplates as $rootTemplate) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $recursiveGetTemplates($rootTemplate); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $pageRepo = $entityManager->getRepository('VictoirePageBundle:BasePage'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $pages = $pageRepo->findAll(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $errorRepo = $entityManager->getRepository('VictoireTwigBundle:ErrorPage'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $errorPages = $errorRepo->findAll(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         /* @var $views View[] */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $views = array_merge($templates, array_merge($pages, $errorPages)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         //Prepare limit | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $limit = ($limit && $limit < count($views)) ? $limit : count($views); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $count = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         //Set progress for output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $progress = $this->getHelper('progress'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $progress->start($output, $limit); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         foreach ($views as $view) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             if ($outOfDate && $view->isCssUpToDate()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 continue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             //Exit if limit reached | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             if ($count >= $limit) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             //If hash already exist, remove CSS file, else generate a hash | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             if ($viewHash = $view->getCssHash()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |                 $viewCssBuilder->removeCssFile($viewHash); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |                 $view->changeCssHash(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             //Generate CSS file with its widgets style | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             $widgetMapBuilder->build($view, $entityManager); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             $widgets = $widgetRepo->findAllWidgetsForView($view); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             $viewCssBuilder->generateViewCss($view, $widgets); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             //Set View's CSS as up to date | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             $view->setCssUpToDate(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             $entityManager->persist($view); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             $entityManager->flush($view); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             ++$count; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             $progress->advance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $progress->finish(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 125 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |  | 
            
                        
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.