| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace Paysera\PhpStormHelper\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Paysera\PhpStormHelper\Entity\GlobalConfiguration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Paysera\PhpStormHelper\Entity\SuggestedGlobalConfiguration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Paysera\PhpStormHelper\Service\ExternalToolsConfigurationHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Paysera\PhpStormHelper\Service\PluginDownloader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\Console\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\Console\Input\InputArgument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class ConfigureInstallationCommand extends Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $externalToolsConfigurationHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     private $pluginDownloader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private $defaultGlobalConfiguration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 6 |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         ExternalToolsConfigurationHelper $externalToolsConfigurationHelper, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         PluginDownloader $pluginDownloader | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 6 |  |         parent::__construct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 6 |  |         $this->externalToolsConfigurationHelper = $externalToolsConfigurationHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 6 |  |         $this->pluginDownloader = $pluginDownloader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 6 |  |         $this->defaultGlobalConfiguration = new SuggestedGlobalConfiguration(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 6 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function setDefaultGlobalConfiguration(GlobalConfiguration $defaultGlobalConfiguration): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->defaultGlobalConfiguration = $defaultGlobalConfiguration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 6 |  |     protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 6 |  |             ->setName('configure-installation') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 6 |  |             ->addArgument( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 6 |  |                 'path-to-config-file', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 6 |  |                 InputArgument::OPTIONAL, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 6 |  |                 'Path to php configuration file, which returns GlobalConfiguration object' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         ; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 6 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         $globalConfiguration = $this->resolveGlobalConfiguration($input); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         $this->externalToolsConfigurationHelper->configureExternalTools( | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             $globalConfiguration->getExternalToolConfigurations() | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |         $this->pluginDownloader->downloadPlugins($globalConfiguration->getPlugins()); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |         $output->writeln('Restart all PhpStorm instances for changes to take effect'); | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     protected function resolveGlobalConfiguration(InputInterface $input): GlobalConfiguration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $configurationFilePath = $input->getArgument('path-to-config-file'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         if ($configurationFilePath === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             return $this->defaultGlobalConfiguration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $globalConfiguration = require $configurationFilePath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         if (!$globalConfiguration instanceof GlobalConfiguration) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             throw new RuntimeException('Expected configuration file to return GlobalConfiguration object'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         return $globalConfiguration; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 79 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 80 |  |  |  |