| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Parking\Console\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | class Import implements Command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | { | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 7 |  |  |     public function execute($input, $output) | 
            
                                                                        
                            
            
                                    
            
            
                | 8 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  |         $this->validate($input); | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |         $commands = $this->parse($input); | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |         if (!$commands) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |             throw new \Exception("Commands not found"); | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |         $path = getcwd() . "/tmp/data"; | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |         $repository = new \Parking\Infrastructure\Repository\ParkingRepository($path); | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |         foreach ($commands as $command) { | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |             $argv = $this->generateArgv($command); | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 20 |  |  |             $handler = new \Parking\Console\Handler($argv); | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |             $handler->addCommand(new \Parking\Console\Command\CreateParkingLot($repository)); | 
            
                                                                        
                            
            
                                    
            
            
                | 22 |  |  |             $handler->addCommand(new \Parking\Console\Command\Leave($repository)); | 
            
                                                                        
                            
            
                                    
            
            
                | 23 |  |  |             $handler->addCommand(new \Parking\Console\Command\Park($repository)); | 
            
                                                                        
                            
            
                                    
            
            
                | 24 |  |  |             $handler->addCommand(new \Parking\Console\Command\RegistrationNumbersForCarsWithColour($repository)); | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  |             $handler->addCommand(new \Parking\Console\Command\SlotNumbersForCarsWithColour($repository)); | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  |             $handler->addCommand(new \Parking\Console\Command\SlotNumberForRegistrationNumber($repository)); | 
            
                                                                        
                            
            
                                    
            
            
                | 27 |  |  |             $handler->addCommand(new \Parking\Console\Command\Status($repository)); | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |             $handler->dispatch(); | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     public function generateArgv($command) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         return array_merge(['parking'], explode(' ', $command)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     protected function parse($input) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $fp = fopen($input[0], 'r'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         while ($command = fgets($fp, 4096)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             $commands[] = trim($command); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         fclose($fp); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         return $commands; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param $input | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @throws \Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     protected function validate($input) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         if (!isset ($input[0])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             throw new \Exception('Param required'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         if (!is_file ($input[0])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             throw new \Exception('File invalid'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 66 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 67 |  |  | } | 
            
                        
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.