Passed
Push — master ( aa493d...38332a )
by Bjørn
02:32
created

OptionsTrait   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Test Coverage

Coverage 64.58%

Importance

Changes 0
Metric Value
eloc 50
dl 0
loc 123
ccs 31
cts 48
cp 0.6458
rs 10
c 0
b 0
f 0
wmc 25

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultOptions() 0 10 3
A getAllOptionDefinitions() 0 3 1
A setProvidedOptions() 0 18 5
C checkOptions() 0 55 16
1
<?php
2
3
namespace WebPConvert\Convert\BaseConverters\BaseTraits;
4
5
use WebPConvert\Convert\Exceptions\ConversionFailed\ConversionSkippedException;
6
use WebPConvert\Convert\Exceptions\ConversionFailed\InvalidInput\InvalidOptionTypeException;
7
8
trait OptionsTrait
9
{
10
11
    /** @var array  Provided conversion options */
12
    public $providedOptions;
13
14
    /** @var array  Calculated conversion options (merge of default options and provided options)*/
15
    public $options;
16
17
    // The concrete converters must supply this method...
18
    abstract protected function getOptionDefinitionsExtra();
19
20
    abstract protected function getMimeTypeOfSource();
21
22
    public static $optionDefinitionsBasic = [
23
        ['quality', 'number|string', 'auto'],
24
        ['max-quality', 'number', 85],
25
        ['default-quality', 'number', 75],
26
        ['metadata', 'string', 'none'],
27
        ['lossless', 'boolean|string', false],
28
        ['skip', 'boolean', false],
29
    ];
30
31
    /**
32
     * Set "provided options"
33
     * This also sets the internal options array, by merging in the default options
34
     *
35
     * @param   array $providedOptions (optional)
36
     * @return  void
37
     */
38 30
    public function setProvidedOptions($providedOptions = [])
39
    {
40 30
        $this->providedOptions = $providedOptions;
41
42 30
        if (isset($this->providedOptions['png'])) {
43
            if ($this->getMimeTypeOfSource() == 'image/png') {
44
                $this->providedOptions = array_merge($this->providedOptions, $this->providedOptions['png']);
45
//                $this->logLn(print_r($this->providedOptions, true));
46
            }
47
        }
48
49 30
        if (isset($this->providedOptions['jpeg'])) {
50
            if ($this->getMimeTypeOfSource() == 'image/jpeg') {
51
                $this->providedOptions = array_merge($this->providedOptions, $this->providedOptions['jpeg']);
52
            }
53
        }
54
        // -  Merge $defaultOptions into provided options
55 30
        $this->options = array_merge($this->getDefaultOptions(), $this->providedOptions);
56 30
    }
57
58
59 30
    public function getAllOptionDefinitions()
60
    {
61 30
        return array_merge(self::$optionDefinitionsBasic, $this->getOptionDefinitionsExtra());
62
    }
63
64 30
    public function getDefaultOptions()
65
    {
66 30
        $defaults = [];
67 30
        foreach ($this->getAllOptionDefinitions() as list($name, $type, $default)) {
68 30
            $defaults[$name] = $default;
69
        }
70 30
        if ($this->getMimeTypeOfSource() == 'image/png') {
71 5
            $defaults['lossless'] = 'auto';
72
        }
73 30
        return $defaults;
74
    }
75
76 16
    protected function checkOptions()
77
    {
78 16
        foreach ($this->getAllOptionDefinitions() as $def) {
79 16
            list($optionName, $optionType) = $def;
80
81 16
            if (isset($this->providedOptions[$optionName])) {
82
                //$this->logLn($optionName);
83
84 7
                $actualType = gettype($this->providedOptions[$optionName]);
85 7
                if ($actualType != $optionType) {
86 1
                    $optionType = str_replace('number', 'integer|double', $optionType);
87 1
                    if (!in_array($actualType, explode('|', $optionType))) {
88
                        throw new InvalidOptionTypeException(
89
                            'The provided ' . $optionName . ' option is not a ' . $optionType .
90
                                ' (it is a ' . $actualType . ')'
91
                        );
92
                    }
93
                }
94
95 7
                $optionValue = $this->providedOptions[$optionName];
96
97 7
                if ($optionName == 'quality') {
98 1
                    if ($actualType == 'string') {
99 1
                        if ($optionValue != 'auto') {
100
                            throw new InvalidOptionTypeException(
101
                                'Quality option must be either "auto" or a number between 0-100. ' .
102 1
                                'A string, "' . $optionValue . '" was given'
103
                            );
104
                        }
105
                    } else {
106
                        if (($optionValue < 0) || ($optionValue > 100)) {
107
                            throw new InvalidOptionTypeException(
108
                                'Quality option must be either "auto" or a number between 0-100. ' .
109
                                    'The number you provided (' . strval($optionValue) . ') is out of range.'
110
                            );
111
                        }
112
                    }
113
                }
114
115 7
                if (($optionName == 'lossless') && ($actualType == 'string')  && ($optionValue != 'auto')) {
116
                    throw new InvalidOptionTypeException(
117 16
                        'Lossless option must be true, false or "auto". It was set to: "' . $optionValue . '"'
118
                    );
119
                }
120
            }
121
        }
122
123 16
        if ($this->options['skip']) {            
124
            if (($this->getMimeTypeOfSource() == 'image/png') && isset($this->options['png']['skip'])) {
125
                throw new ConversionSkippedException(
126
                    'skipped conversion (configured to do so for PNG)'
127
                );
128
            } else {
129
                throw new ConversionSkippedException(
130
                    'skipped conversion (configured to do so)'
131
                );
132
133
            }
134
        }
135
136 16
    }
137
138
    /**
139
     * Prepare options.
140
     */
141
     /*
142
    private function prepareOptions()
143
    {
144
        //$defaultOptions = self::$defaultOptions;
145
146
        // -  Merge defaults of the converters extra options into the standard default options.
147
        //$defaultOptions = array_merge($defaultOptions, array_column(static::$extraOptions, 'default', 'name'));
148
        //print_r($this->getOptionDefinitionsExtra());
149
        //$extra = [];
150
        //$this->getDefaultOptionsExtra();
151
        //echo '<br>';
152
        //print_r(static::$extraOptions);
153
        //print_r(array_column(static::$extraOptions, 'default', 'name'));
154
        //$defaultOptions = array_merge($defaultOptions, $this->getDefaultOptionsExtra());
155
156
157
        //throw new \Exception('extra!' . print_r($this->getConverterDisplayName(), true));
158
159
        // -  Merge $defaultOptions into provided options
160
        //$this->options = array_merge($defaultOptions, $this->options);
161
        //$this->options = array_merge($this->getDefaultOptions(), $providedOptions);
162
163
        if ($this->getMimeTypeOfSource() == 'png') {
164
            // skip png's ?
165
            if ($this->options['skip-pngs']) {
166
                throw new ConversionSkippedException(
167
                    'PNG file skipped (configured to do so)'
168
                );
169
            }
170
        }
171
172
173
        // TODO: Here we could test if quality is 0-100 or auto.
174
        //       and if not, throw something extending InvalidArgumentException (which is a LogicException)
175
    }*/
176
}
177