| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Leankoala\HealthFoundation\Check\Files\Content; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Leankoala\HealthFoundation\Check\Check; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Leankoala\HealthFoundation\Check\Result; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | class NumberOfLinesCheck implements Check | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     const IDENTIFIER = 'base:files:content:numberOfLines'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     private $file; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     private $relation; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     private $limit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     const RELATION_MAX = 'max'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     const RELATION_MIN = 'min'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     private $pattern; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     public function run() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         if ($this->pattern) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |             $grep = ' | grep ' . $this->pattern; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |             $grep = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $command = 'cat ' . $this->file . $grep . ' | wc -l'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         exec($command, $output, $return); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $numberLines = (int)$output[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         if ($this->relation === self::RELATION_MAX) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             if ($numberLines > $command) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |                 return new Result(Result::STATUS_FAIL, 'The document contains too many lines (' . $numberLines . '). Expected where ' . $this->limit . ' at the most.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 return new Result(Result::STATUS_PASS, 'The document contains ' . $numberLines . ' lines. Expected where ' . $this->limit . ' at the most.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             if ($numberLines < $command) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 return new Result(Result::STATUS_FAIL, 'The document contains too few lines (' . $numberLines . '). Expected where ' . $this->limit . ' at least.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |                 return new Result(Result::STATUS_PASS, 'The document contains ' . $numberLines . ' lines. Expected where ' . $this->limit . ' at least.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |     public function init($file, $limit, $relation = self::RELATION_MAX, $pattern = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         $this->file = $file; | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         $this->pattern = $pattern; | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         $this->relation = $relation; | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         $this->limit = $limit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public function getIdentifier() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         return self::IDENTIFIER; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 63 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 64 |  |  |  |