| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Softius\JenkinsJobMonitor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use GuzzleHttp\ClientInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\Console\Command\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\Console\Input\InputArgument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\Console\Input\InputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\Console\Output\OutputInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\Process\Process; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * Class MonitorCommand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @package Softius\JenkinsJobMonitor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class MonitorCommand extends Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @var GuzzleHttp\ClientInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * Configures the monitor command. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 3 |  |     protected function configure() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 3 |  |         $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 3 |  |             ->setName('monitor') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 3 |  |             ->setDescription('Monitors the non-interactive execution of processes') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 3 |  |             ->addArgument('url', InputArgument::REQUIRED, 'Jenkins home URL') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 3 |  |             ->addArgument('job', InputArgument::REQUIRED, 'Job name as configured in Jenkins') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 3 |  |             ->addArgument('monitor', InputArgument::OPTIONAL, 'Command to be monitored') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 3 |  |             ->addOption('display', null, InputOption::VALUE_OPTIONAL, '') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 3 |  |             ->addOption('description', null, InputOption::VALUE_OPTIONAL, ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * Executes the monitor command. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param InputInterface  $input  An InputInterface instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param OutputInterface $output An OutputInterface instance | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @return null|int null or 0 if everything went fine, or an error code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 3 |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 3 |  |             $command = $this->getMonitorArgument($input); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 3 |  |             $monitor = new JobMonitor( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 3 |  |                 $input->getArgument('job'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 3 |  |                 $input->getOption('display'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 3 |  |                 $input->getOption('description') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 3 |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 3 |  |             $monitor->start(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 3 |  |             $process = new Process($command); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 3 |  |             $process->run(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 3 |  |             $monitor->stop($process->getExitCode(), $process->getOutput()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 3 |  |             $this->getClient()->send($monitor->getRequest(), ['base_uri' => $input->getArgument('url')]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 3 |  |         } catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $output->writeln(sprintf('<error>%s</error>', $e->getMessage())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             return 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 3 |  |         return 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * Returns the command to monitor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * If monitor argument is not provided, STDIN is being used instead. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param InputInterface $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @throws \RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 81 | 3 | View Code Duplication |     private function getMonitorArgument(InputInterface $input) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 3 |  |         $monitor = $input->getArgument('monitor'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 3 |  |         if (!$monitor && 0 === ftell(STDIN)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             $monitor = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             while (!feof(STDIN)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 $monitor .= fread(STDIN, 1024); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 3 |  |         if (!$monitor) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             throw new \RuntimeException('Please provide a command or pipe content to STDIN.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 3 |  |         return $monitor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @param GuzzleHttp\ClientInterface $client | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 100 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 101 | 3 |  |     public function setClient(ClientInterface $client) | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 103 | 3 |  |         $this->client = $client; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 104 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @return Client | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 3 |  |     public function getClient() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 3 |  |         return $this->client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 113 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |  | 
            
                        
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.