Passed
Push — master ( 26c56b...7d441f )
by Bjørn
08:31
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 50
    public function setProvidedOptions($providedOptions = [])
39
    {
40 50
        $this->providedOptions = $providedOptions;
41
42 50
        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 50
        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 50
        $this->options = array_merge($this->getDefaultOptions(), $this->providedOptions);
56 50
    }
57
58
59 50
    public function getAllOptionDefinitions()
60
    {
61 50
        return array_merge(self::$optionDefinitionsBasic, $this->getOptionDefinitionsExtra());
62
    }
63
64 50
    public function getDefaultOptions()
65
    {
66 50
        $defaults = [];
67 50
        foreach ($this->getAllOptionDefinitions() as list($name, $type, $default)) {
68 50
            $defaults[$name] = $default;
69
        }
70 50
        if ($this->getMimeTypeOfSource() == 'image/png') {
71 34
            $defaults['lossless'] = 'auto';
72
        }
73 50
        return $defaults;
74
    }
75
76 22
    protected function checkOptions()
77
    {
78 22
        foreach ($this->getAllOptionDefinitions() as $def) {
79 22
            list($optionName, $optionType) = $def;
80
81 22
            if (isset($this->providedOptions[$optionName])) {
82
                //$this->logLn($optionName);
83
84 19
                $actualType = gettype($this->providedOptions[$optionName]);
85 19
                if ($actualType != $optionType) {
86 14
                    $optionType = str_replace('number', 'integer|double', $optionType);
87 14
                    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 19
                $optionValue = $this->providedOptions[$optionName];
96
97 19
                if ($optionName == 'quality') {
98 4
                    if ($actualType == 'string') {
99 4
                        if ($optionValue != 'auto') {
100
                            throw new InvalidOptionTypeException(
101
                                'Quality option must be either "auto" or a number between 0-100. ' .
102 4
                                '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 19
                if (($optionName == 'lossless') && ($actualType == 'string')  && ($optionValue != 'auto')) {
116
                    throw new InvalidOptionTypeException(
117 22
                        'Lossless option must be true, false or "auto". It was set to: "' . $optionValue . '"'
118
                    );
119
                }
120
            }
121
        }
122
123 22
        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 22
    }
135
136
    /**
137
     * Prepare options.
138
     */
139
     /*
140
    private function prepareOptions()
141
    {
142
        //$defaultOptions = self::$defaultOptions;
143
144
        // -  Merge defaults of the converters extra options into the standard default options.
145
        //$defaultOptions = array_merge($defaultOptions, array_column(static::$extraOptions, 'default', 'name'));
146
        //print_r($this->getOptionDefinitionsExtra());
147
        //$extra = [];
148
        //$this->getDefaultOptionsExtra();
149
        //echo '<br>';
150
        //print_r(static::$extraOptions);
151
        //print_r(array_column(static::$extraOptions, 'default', 'name'));
152
        //$defaultOptions = array_merge($defaultOptions, $this->getDefaultOptionsExtra());
153
154
155
        //throw new \Exception('extra!' . print_r($this->getConverterDisplayName(), true));
156
157
        // -  Merge $defaultOptions into provided options
158
        //$this->options = array_merge($defaultOptions, $this->options);
159
        //$this->options = array_merge($this->getDefaultOptions(), $providedOptions);
160
161
        if ($this->getMimeTypeOfSource() == 'png') {
162
            // skip png's ?
163
            if ($this->options['skip-pngs']) {
164
                throw new ConversionSkippedException(
165
                    'PNG file skipped (configured to do so)'
166
                );
167
            }
168
        }
169
170
171
        // TODO: Here we could test if quality is 0-100 or auto.
172
        //       and if not, throw something extending InvalidArgumentException (which is a LogicException)
173
    }*/
174
}
175