| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace EM\CssCompiler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Composer\Script\Event; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use EM\CssCompiler\Processor\Processor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @see   ScriptHandlerTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @since 0.1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class ScriptHandler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     const CONFIG_MAIN_KEY          = 'css-compiler'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     const OPTION_KEY_INPUT         = 'input'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     const OPTION_KEY_OUTPUT        = 'output'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     const OPTION_KEY_FORMATTER     = 'format'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     const DEFAULT_OPTION_FORMATTER = 'compact'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected static $mandatoryOptions = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         self::OPTION_KEY_INPUT  => 'array', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         self::OPTION_KEY_OUTPUT => 'string' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @api | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param Event $event | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @throws \InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 1 |  |     public static function generateCSS(Event $event) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 1 |  |         $extra = $event->getComposer()->getPackage()->getExtra(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 1 |  |         static::validateConfiguration($extra); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 1 |  |         $processor = new Processor($event->getIO()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 1 |  |         foreach ($extra[static::CONFIG_MAIN_KEY] as $options) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 1 |  |             foreach ($options[static::OPTION_KEY_INPUT] as $inputSource) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 1 |  |                 $processor->attachFiles( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 1 |  |                     static::resolvePath($inputSource, getcwd()), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 1 |  |                     static::resolvePath($options[static::OPTION_KEY_OUTPUT], getcwd()) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |                 ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  |             $formatter = array_key_exists(static::OPTION_KEY_FORMATTER, $options) ? $options[static::OPTION_KEY_FORMATTER] : static::DEFAULT_OPTION_FORMATTER; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 1 |  |             $processor->processFiles($formatter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 1 |  |         $processor->saveOutput(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @param string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param string $prefix | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 1 |  |     protected static function resolvePath($path, $prefix) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 1 |  |         return '/' === substr($path, 0, 1) ? $path : "{$prefix}/{$path}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @param array $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @throws \InvalidArgumentException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 6 |  |     protected static function validateConfiguration(array $config) | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 71 | 6 |  |         if (!array_key_exists(static::CONFIG_MAIN_KEY, $config)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 72 | 1 |  |             throw new \InvalidArgumentException('compiler should needs to be configured through the extra.css-compiler setting'); | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 75 | 5 |  |         if (!is_array($config[static::CONFIG_MAIN_KEY])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 76 | 2 |  |             throw new \InvalidArgumentException('the extra.' . static::CONFIG_MAIN_KEY . ' setting must be an array of objects'); | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 79 | 3 |  |         foreach ($config[static::CONFIG_MAIN_KEY] as $index => $options) { | 
            
                                                                        
                            
            
                                    
            
            
                | 80 | 3 |  |             if (!is_array($options)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 81 | 1 |  |                 throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[$index] should be an array"); | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 84 | 2 |  |             static::validateMandatoryOptions($options, $index); | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 86 | 2 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @param int   $index | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @throws \InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 7 |  |     protected static function validateMandatoryOptions(array $options, $index) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 7 |  |         foreach (static::$mandatoryOptions as $optionIndex => $type) { | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 97 | 7 | View Code Duplication |             if (!array_key_exists($optionIndex, $options)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 1 |  |                 throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[$index].{$optionIndex} is required!"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 6 |  |             $callable = "is_{$type}"; | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 102 | 6 | View Code Duplication |             if (!$callable($options[$optionIndex])) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 6 |  |                 throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[$index].{$optionIndex} should be {$type}!"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 3 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 107 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 108 |  |  |  | 
            
                        
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.