| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Magestead\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Magestead\Exceptions\ExistingProjectException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Magestead\Helper\Options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Magestead\Installers\Project; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Magestead\Service\UsageApi; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Console\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\Console\Helper\ProgressBar; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\Console\Input\InputArgument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\Yaml\Dumper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\Yaml\Exception\ParseException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\Yaml\Parser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * Class NewCommand. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | class NewCommand extends Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     protected $_basePath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected $_projectPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     protected $_msConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $this->_basePath = dirname(__FILE__).'/../../../'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $this->_projectPath = getcwd(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->setName('new'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->setDescription('Initialise new Magestead project into current working directory'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->addArgument('project', InputArgument::REQUIRED, 'Name your project directory'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param InputInterface  $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @throws ExistingProjectException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @return \Magestead\Installers\Magento2Project|\Magestead\Installers\MagentoProject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $project = $this->setProject($input); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $helper = $this->getHelper('question'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $options = new Options($helper, $input, $output, $project); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $this->setupProject($output, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $output->writeln('<info>Spinning up your custom box</info>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         new ProcessCommand('vagrant up', $this->_projectPath, $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         return Project::create($options->getAllOptions(), $this->_msConfig, $this->_projectPath, $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @param $source | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @param $target | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * @param OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     protected function copyConfigFiles($source, $target, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             $progress = new ProgressBar($output, 3720); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $progress->start(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             foreach ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 $iterator = new \RecursiveIteratorIterator( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                     new \RecursiveDirectoryIterator($source, \RecursiveDirectoryIterator::SKIP_DOTS), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                     \RecursiveIteratorIterator::SELF_FIRST) as $item | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 if ($item->isDir()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                     mkdir($target.DIRECTORY_SEPARATOR.$iterator->getSubPathName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                     copy($item, $target.DIRECTORY_SEPARATOR.$iterator->getSubPathName()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 $progress->advance(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             $progress->finish(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             echo "\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $output->writeln('<error>There was an error while setting up the project structure</error>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @param array           $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * @param OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     protected function configureProject(array $options, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $msConfig = $this->getConfigFile($output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $app = ($options['app'] == 'magento2') ? 'magento2' : 'magento'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $hostname = 'magestead-'.$options['base_url']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $msConfig['vagrantfile']['vm']['box'] = $options['box']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $msConfig['vagrantfile']['vm']['box_url'] = $options['box']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $msConfig['vagrantfile']['vm']['hostname'] = $hostname; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $msConfig['vagrantfile']['vm']['memory'] = $options['memory_limit']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $msConfig['vagrantfile']['vm']['network']['private_network'] = $options['ip_address']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         $msConfig['magestead']['apps']['mba_12345']['type'] = $app; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $msConfig['magestead']['apps']['mba_12345']['locale'] = $options['locale']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $msConfig['magestead']['apps']['mba_12345']['default_currency'] = $options['default_currency']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $msConfig['magestead']['apps']['mba_12345']['base_url'] = $options['base_url']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $msConfig['magestead']['os'] = $options['os']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $msConfig['magestead']['server'] = $options['server']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $this->_msConfig = $msConfig; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $this->saveConfigFile($msConfig, $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @param OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     protected function getConfigFile(OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         $yaml = new Parser(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             return $yaml->parse(file_get_contents($this->_projectPath.'/magestead.yaml')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         } catch (ParseException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             $output->writeln('<error>Unable to parse the YAML string</error>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             printf('Unable to parse the YAML string: %s', $e->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * @param array           $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * @param OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 137 |  | View Code Duplication |     protected function saveConfigFile(array $config, OutputInterface $output) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $dumper = new Dumper(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         $yaml = $dumper->dump($config, 6); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             file_put_contents($this->_projectPath.'/magestead.yaml', $yaml); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |             $output->writeln('<error>Unable to write to the YAML file</error>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @param OutputInterface $output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * @param $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     protected function setupProject(OutputInterface $output, $options) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         $output->writeln('<info>Setting up project structure</info>'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         $provisionFolder = $this->_basePath.'provision'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         $this->copyConfigFiles($provisionFolder, $this->_projectPath, $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         $this->configureProject($options->getAllOptions(), $output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         (new UsageApi($options->getAllOptions()))->send(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |      * @param InputInterface $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * @throws ExistingProjectException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      * @return mixed | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 169 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 170 |  |  |     protected function setProject(InputInterface $input) | 
            
                                                        
            
                                    
            
            
                | 171 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 172 |  |  |         $project = $input->getArgument('project'); | 
            
                                                        
            
                                    
            
            
                | 173 |  |  |         $this->_projectPath = $this->_projectPath.'/'.$project; | 
            
                                                        
            
                                    
            
            
                | 174 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 175 |  |  |         if (is_dir($this->_projectPath)) { | 
            
                                                        
            
                                    
            
            
                | 176 |  |  |             throw new ExistingProjectException('Target project directory already exists'); | 
            
                                                        
            
                                    
            
            
                | 177 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 178 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 179 |  |  |         mkdir($this->_projectPath, 0777, true); | 
            
                                                        
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 181 |  |  |         return $project; | 
            
                                                        
            
                                    
            
            
                | 182 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 183 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 184 |  |  |  | 
            
                        
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.