Completed
Push — master ( 29fb7d...cbd3fd )
by Eugene
27s
created

ScriptHandler.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace EM\CssCompiler\Handler;
4
5
use Composer\Script\Event;
6
use EM\CssCompiler\Processor\Processor;
7
8
class ScriptHandler
9
{
10
    const CONFIG_MAIN_KEY          = 'css-compiler';
11
    const CONFIG_INPUT_KEY         = 'input';
12
    const CONFIG_OUTPUT_KEY        = 'output';
13
    const CONFIG_FORMATTER_KEY     = 'format';
14
    const CONFIG_DEFAULT_FORMATTER = 'compact';
15
16
    public static function compileCSS(Event $event)
17
    {
18
        $extra = $event->getComposer()->getPackage()->getExtra();
19
20
        static::validateConfiguration($extra);
21
22
        $processor = new Processor($event->getIO());
23
        $currentDirectory = getcwd();
24
        foreach ($extra[static::CONFIG_MAIN_KEY] as $config) {
25
            foreach ($config[static::CONFIG_INPUT_KEY] as $item => $value) {
26
                $processor->attachFiles("{$currentDirectory}/{$value}", "{$currentDirectory}/{$config[static::CONFIG_OUTPUT_KEY]}");
27
            }
28
29
            $formatter = isset($config[static::CONFIG_FORMATTER_KEY])
30
                ? $config[static::CONFIG_FORMATTER_KEY]
31
                : static::CONFIG_DEFAULT_FORMATTER;
32
33
            $processor->processFiles($formatter);
34
        }
35
36
        $processor->saveOutput();
37
    }
38
39
    protected static function validateConfiguration(array $config)
40
    {
41
        if (!isset($config[static::CONFIG_MAIN_KEY])) {
42
            throw new \InvalidArgumentException('the parameter handler needs to be configured through the extra.css-compiler setting.');
43
        }
44
45
        if (!is_array($config[static::CONFIG_MAIN_KEY])) {
46
            throw new \InvalidArgumentException('the extra.css-compiler setting must be an array of a configuration objects.');
47
        }
48
49
        foreach ($config[static::CONFIG_MAIN_KEY] as $el) {
50
            if (!is_array($el)) {
51
                throw new \InvalidArgumentException('The extra.css-compiler should contain only configuration objects.');
52
            }
53
54 View Code Duplication
            if (!isset($el[static::CONFIG_INPUT_KEY])) {
0 ignored issues
show
This code seems to be duplicated across your project.

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.

Loading history...
55
                throw new \InvalidArgumentException('The extra.css-compiler[].' . static::CONFIG_INPUT_KEY . ' is required!');
56
            }
57 View Code Duplication
            if (!isset($el[static::CONFIG_OUTPUT_KEY])) {
0 ignored issues
show
This code seems to be duplicated across your project.

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.

Loading history...
58
                throw new \InvalidArgumentException('The extra.css-compiler[].' . static::CONFIG_OUTPUT_KEY . ' is required!');
59
            }
60
        }
61
    }
62
}
63