| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Health\Commands; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Illuminate\Console\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Contracts\Config\Repository as Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Symfony\Component\Console\Helper\TableSeparator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Health\Services\HealthService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Health\Health; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Health\HealthCheck; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Health Command | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 14 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  | class HealthCommand extends Command | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |      * The console command name. | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |      * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |     protected $name = 'health'; | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |      * The console command description. | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |      * @var string | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |     protected $description = 'Health Check.'; | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |      * Configuration repository. | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |      * @var Config | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |     protected $config; | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |      * @var HealthService | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     private $healthService; | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |      * Create a new command instance. | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |      * @param \Illuminate\Contracts\Config\Repository $config | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     public function __construct(Config $config, HealthService $healthService) | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         $this->config = $config; | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         $this->healthService = $healthService; | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         parent::__construct(); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |      * Execute the console command. | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |      * @return mixed | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     public function handle() | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         $health = $this->healthService->getHealth($this->config->get('health.checks')); | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |         $this->output($health->getState(), 'Health State'); | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |         $this->line(''); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |         $statusCode = $health->isOk(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |         foreach ($health->getChecks() as $check) { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |             $this->output($check->getState(), $check->getName(), $check->getData()); | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |             // $this->output($check->getState(), json_encode($check->getData())); | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |      * Write a string as standard output. | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |      * @param string $string | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |      * @param string $style | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |      * @param int|string|null $verbosity | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |      * @return void | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |     public function line($string, $style = null, $verbosity = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         $styled = $style ? "<$style>$string</$style>" : $string; | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         $this->output->writeln($styled, $this->parseVerbosity($verbosity)); | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |      * Output based on pipline/job status | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |      * @param string $status | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |      * @param string $name | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |      * @param mixed $data | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     private function output(string $status, string $name, $data = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 103 |  |  |         $this->line($this->formattedOutput($status, $name, $data)); | 
            
                                                                        
                                                                
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                        
                                                                
            
                                    
            
            
                | 106 |  |  |     /** | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |      * Output based on pipline/job status | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |      * | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |      * @param string $status | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |      * @param string $name | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |      * @param mixed $data | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     private function formattedOutput(string $status, string $name, $data = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $data = $data ? json_encode($data) : ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         if ($status == HealthCheck::STATE_UP) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             $result = '<info>✔ ' . $name . '</info> ' . $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |             $result = '<error>✖ ' . $name . '</error> ' . $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 123 |  |  |         return $result; | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 125 |  |  | } |