| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the Blast Project package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Copyright (C) 2015-2017 Libre Informatique | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * This file is licenced under the GNU LGPL v3. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * For the full copyright and license information, please view the LICENSE.md | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | namespace Blast\CoreBundle\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Symfony\Component\Yaml\Yaml; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | class PatcherApplyCommand extends ContainerAwareCommand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     use PatcherConfig, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         PatcherLogger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private $dryRun = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * Command applies patch file [targetFilePath, patchPath]. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private $command = 'patch --no-backup-if-mismatch %3$s -f %1$s < %2$s > /dev/null'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             ->setName('librinfo:patchs:apply') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             ->setDescription('Apply Patches from Librinfo on misc vendors') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             ->addOption('force', 'f', InputOption::VALUE_NONE, 'Force patch apply'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->loadConfig(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         foreach ($this->config['patches'] as $patch) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             if ($patch['enabled'] === true && ($patch['patched'] == false || $input->getOption('force') === true)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 $this->applyPatch($patch['targetFile'], $patch['patchFile'], $patch['id']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $this->displayMessages($output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     private function applyPatch($targetFile, $patchFile, $patchId) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $targetFile = $this->config['paths']['rootDir'] . '/' . $targetFile; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 60 |  | View Code Duplication |         if (!file_exists($targetFile) || !file_exists($patchFile)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             $this->error('Missing patches :'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             if (!file_exists($targetFile)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |                 $this->comment(' - ' . $targetFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             if (!file_exists($patchFile)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 $this->comment(' - ' . $patchFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $command = $this->getCommand($targetFile, $patchFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         $out = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         system($command, $out); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         if ($out == 0) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $this->dryRun = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $command = $this->getCommand($targetFile, $patchFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             system($command, $out); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             $this->dryRun = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             if ($out != 0) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |                 $this->error('The patch ' . $patchFile . ' cannot be applyed on file ' . $targetFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |                 return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             foreach ($this->config['patches'] as $key => $patch) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 if ($patch['id'] == $patchId) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                     $this->config['patches'][$key]['patched'] = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             file_put_contents( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 $this->config['paths']['configFile'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 Yaml::dump( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                     ['patches' => $this->config['patches']] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |             $this->comment('The patch ' . $patchFile . ' cannot be applyed on file ' . $targetFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |     private function getCommand($targetFile, $patchFile) | 
            
                                                        
            
                                    
            
            
                | 106 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |         return sprintf( | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |             $this->command, | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |             $targetFile, | 
            
                                                        
            
                                    
            
            
                | 110 |  |  |             $patchFile, | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |             $this->dryRun ? '--dry-run' : '' | 
            
                                                        
            
                                    
            
            
                | 112 |  |  |         ); | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 114 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 115 |  |  |  | 
            
                        
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.