Passed
Push — master ( e20300...385468 )
by Bjørn
02:06
created

OptionsTrait::checkOptions()   C

Complexity

Conditions 13
Paths 19

Size

Total Lines 42
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 25
nc 19
nop 0
dl 0
loc 42
rs 6.6166
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
20
    public static $optionDefinitionsBasic = [
21
        ['quality', 'number|string', 'auto'],
22
        ['max-quality', 'number', 85],
23
        ['default-quality', 'number', 75],
24
        ['metadata', 'string', 'none'],
25
        ['lossless', 'boolean|string', false],
26
        ['skip-pngs', 'boolean', false],
27
    ];
28
29
    /**
30
     * Set "provided options"
31
     * This also sets the internal options array, by merging in the default options
32
     *
33
     * @param   array $providedOptions (optional)
34
     * @return  void
35
     */
36
    public function setProvidedOptions($providedOptions = [])
37
    {
38
        $this->providedOptions = $providedOptions;
39
40
        if (isset($this->providedOptions['png'])) {
41
            if ($this->getMimeTypeOfSource() == 'image/png') {
0 ignored issues
show
Bug introduced by
It seems like getMimeTypeOfSource() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
            if ($this->/** @scrutinizer ignore-call */ getMimeTypeOfSource() == 'image/png') {
Loading history...
42
                $this->providedOptions = array_merge($this->providedOptions, $this->providedOptions['png']);
43
//                $this->logLn(print_r($this->providedOptions, true));
44
            }
45
        }
46
47
        if (isset($this->providedOptions['jpeg'])) {
48
            if ($this->getMimeTypeOfSource() == 'image/jpeg') {
49
                $this->providedOptions = array_merge($this->providedOptions, $this->providedOptions['jpeg']);
50
            }
51
        }
52
        // -  Merge $defaultOptions into provided options
53
        $this->options = array_merge($this->getDefaultOptions(), $this->providedOptions);
54
    }
55
56
57
    public function getAllOptionDefinitions()
58
    {
59
        return array_merge(self::$optionDefinitionsBasic, $this->getOptionDefinitionsExtra());
60
    }
61
62
    public function getDefaultOptions()
63
    {
64
        $defaults = [];
65
        foreach ($this->getAllOptionDefinitions() as list($name, $type, $default)) {
66
            $defaults[$name] = $default;
67
        }
68
        if ($this->getMimeTypeOfSource() == 'image/png') {
69
            $defaults['lossless'] = 'auto';
70
        }
71
        return $defaults;
72
    }
73
74
    protected function checkOptions()
75
    {
76
        foreach ($this->getAllOptionDefinitions() as $def) {
77
            list($optionName, $optionType) = $def;
78
79
            if (isset($this->providedOptions[$optionName])) {
80
                //$this->logLn($optionName);
81
82
                $actualType = gettype($this->providedOptions[$optionName]);
83
                if ($actualType != $optionType) {
84
                    $optionType = str_replace('number', 'integer|double', $optionType);
85
                    if (!in_array($actualType, explode('|', $optionType))) {
86
                        throw new InvalidOptionTypeException(
87
                            'The provided ' . $optionName . ' option is not a ' . $optionType .
88
                                ' (it is a ' . $actualType . ')'
89
                        );
90
                    }
91
                }
92
93
                $optionValue = $this->providedOptions[$optionName];
94
95
                if ($optionName == 'quality') {
96
                    if ($actualType == 'string') {
97
                        if ($optionValue != 'auto') {
98
                            throw new InvalidOptionTypeException(
99
                                'Quality option must be either "auto" or a number between 0-100. ' .
100
                                'A string, "' . $optionValue . '" was given'
101
                            );
102
                        }
103
                    } else {
104
                        if (($optionValue < 0) || ($optionValue > 100)) {
105
                            throw new InvalidOptionTypeException(
106
                                'Quality option must be either "auto" or a number between 0-100. ' .
107
                                    'The number you provided (' . strval($optionValue) . ') is out of range.'
108
                            );
109
                        }
110
                    }
111
                }
112
113
                if (($optionName == 'lossless') && ($actualType == 'string')  && ($optionValue != 'auto')) {
114
                    throw new InvalidOptionTypeException(
115
                        'Lossless option must be true, false or "auto". It was set to: "' . $optionValue . '"'
116
                    );
117
                }
118
            }
119
        }
120
    }
121
122
    /**
123
     * Prepare options.
124
     */
125
     /*
126
    private function prepareOptions()
127
    {
128
        //$defaultOptions = self::$defaultOptions;
129
130
        // -  Merge defaults of the converters extra options into the standard default options.
131
        //$defaultOptions = array_merge($defaultOptions, array_column(static::$extraOptions, 'default', 'name'));
132
        //print_r($this->getOptionDefinitionsExtra());
133
        //$extra = [];
134
        //$this->getDefaultOptionsExtra();
135
        //echo '<br>';
136
        //print_r(static::$extraOptions);
137
        //print_r(array_column(static::$extraOptions, 'default', 'name'));
138
        //$defaultOptions = array_merge($defaultOptions, $this->getDefaultOptionsExtra());
139
140
141
        //throw new \Exception('extra!' . print_r($this->getConverterDisplayName(), true));
142
143
        // -  Merge $defaultOptions into provided options
144
        //$this->options = array_merge($defaultOptions, $this->options);
145
        //$this->options = array_merge($this->getDefaultOptions(), $providedOptions);
146
147
        if ($this->getMimeTypeOfSource() == 'png') {
148
            // skip png's ?
149
            if ($this->options['skip-pngs']) {
150
                throw new ConversionDeclinedException(
151
                    'PNG file skipped (configured to do so)'
152
                );
153
            }
154
155
            // Force lossless option to true for PNG images
156
            $this->options['lossless'] = true;
157
        }
158
159
160
        // TODO: Here we could test if quality is 0-100 or auto.
161
        //       and if not, throw something extending InvalidArgumentException (which is a LogicException)
162
    }*/
163
}
164