Completed
Push — master ( d1266d...63021f )
by Eugene
25s
created

ScriptHandler::validateOptions()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
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,
22
        self::OPTION_KEY_OUTPUT
23
    ];
24
25
    /**
26
     * @param Event $event
27
     *
28
     * @throws \InvalidArgumentException
29
     */
30
    public static function generateCSS(Event $event)
31
    {
32
        $extra = $event->getComposer()->getPackage()->getExtra();
33
        static::validateConfiguration($extra);
34
35
        $processor = new Processor($event->getIO());
36
        $currentDirectory = getcwd();
37
38
        foreach ($extra[static::CONFIG_MAIN_KEY] as $config) {
39
            foreach ($config[static::OPTION_KEY_INPUT] as $value) {
40
                $processor->attachFiles("{$currentDirectory}/{$value}", "{$currentDirectory}/{$config[static::OPTION_KEY_OUTPUT]}");
41
            }
42
43
            $formatter = isset($config[static::OPTION_KEY_FORMATTER]) ? $config[static::OPTION_KEY_FORMATTER] : static::DEFAULT_OPTION_FORMATTER;
44
45
            $processor->processFiles($formatter);
46
        }
47
        $processor->saveOutput();
48
    }
49
50
    /**
51
     * @param array $config
52
     *
53
     * @return bool
54
     * @throws \InvalidArgumentException
55
     */
56
    protected static function validateConfiguration(array $config)
57
    {
58
        if (empty($config[static::CONFIG_MAIN_KEY])) {
59
            throw new \InvalidArgumentException('compiler should needs to be configured through the extra.css-compiler setting');
60
        }
61
62
        if (!is_array($config[static::CONFIG_MAIN_KEY])) {
63
            throw new \InvalidArgumentException('the extra.css-compiler setting must be an array of objects');
64
        }
65
66
        return static::validateOptions($config[static::CONFIG_MAIN_KEY]);
67
    }
68
69
    /**
70
     * @param array $config
71
     *
72
     * @return bool
73
     * @throws \InvalidArgumentException
74
     */
75
    protected static function validateOptions(array $config)
76
    {
77
        foreach ($config as $option) {
78
            if (!is_array($option)) {
79
                throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[]." . static::OPTION_KEY_INPUT . ' array');
80
            }
81
82
            static::validateMandatoryOptions($option);
83
        }
84
85
        return true;
86
    }
87
88
    /**
89
     * @param array $config
90
     *
91
     * @return bool
92
     * @throws \InvalidArgumentException
93
     */
94
    protected static function validateMandatoryOptions(array $config)
95
    {
96
        foreach (static::$mandatoryOptions as $option) {
97
            if (empty($config[$option])) {
98
                throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . "[].{$option} is required!");
99
            }
100
101
            switch ($option) {
102
                case static::OPTION_KEY_INPUT:
103
                    static::validateIsArray($config[$option]);
104
                    break;
105
                case static::OPTION_KEY_OUTPUT:
106
                    static::validateIsString($config[$option]);
107
                    break;
108
            }
109
        }
110
111
        return true;
112
    }
113
114
    /**
115
     * @param array $option
116
     *
117
     * @return bool
118
     */
119 View Code Duplication
    protected static function validateIsArray($option)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
120
    {
121
        if (!is_array($option)) {
122
            throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . '[]' . static::OPTION_KEY_INPUT . ' should be array!');
123
        }
124
125
        return true;
126
    }
127
128
    /**
129
     * @param string $option
130
     *
131
     * @return bool
132
     */
133 View Code Duplication
    protected static function validateIsString($option)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
134
    {
135
        if (!is_string($option)) {
136
            throw new \InvalidArgumentException('extra.' . static::CONFIG_MAIN_KEY . '[]' . static::OPTION_KEY_OUTPUT . ' should string!');
137
        }
138
139
        return true;
140
    }
141
}
142