Completed
Push — master ( 5f5fca...2fa1e0 )
by Bjørn
12:28 queued 02:24
created

OptionsTrait   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Test Coverage

Coverage 61.11%

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 110
ccs 33
cts 54
cp 0.6111
rs 10
c 0
b 0
f 0
wmc 22

4 Methods

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