Passed
Push — develop ( 9f9355...e206b3 )
by Andrew
08:11
created

Settings::behaviors()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 16
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * ImageOptimize plugin for Craft CMS 3.x
4
 *
5
 * Automatically optimize images after they've been transformed
6
 *
7
 * @link      https://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2017 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\imageoptimize\models;
12
13
use nystudio107\imageoptimize\ImageOptimize;
14
use nystudio107\imageoptimize\imagetransforms\CraftImageTransform;
15
use nystudio107\imageoptimize\imagetransforms\ImageTransformInterface;
16
use nystudio107\imageoptimize\imagetransforms\ImgixImageTransform;
0 ignored issues
show
Bug introduced by
The type nystudio107\imageoptimiz...rms\ImgixImageTransform was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use nystudio107\imageoptimize\imagetransforms\ThumborImageTransform;
0 ignored issues
show
Bug introduced by
The type nystudio107\imageoptimiz...s\ThumborImageTransform was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
19
use craft\base\Model;
20
use craft\behaviors\EnvAttributeParserBehavior;
0 ignored issues
show
Bug introduced by
The type craft\behaviors\EnvAttributeParserBehavior was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use craft\validators\ArrayValidator;
22
23
use yii\behaviors\AttributeTypecastBehavior;
24
25
/**
26
 * ImageOptimize Settings model
27
 *
28
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
29
 * @package   ImageOptimize
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
30
 * @since     1.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
31
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
32
class Settings extends Model
33
{
34
    // Constants
35
    // =========================================================================
36
37
    const DEPRECATED_PROPERTIES = [
38
        'generatePlacholders',
39
        'transformMethod',
40
        'imgixDomain',
41
        'imgixApiKey',
42
        'imgixSecurityToken',
43
        'thumborBaseUrl',
44
        'thumborSecurityKey',
45
    ];
46
47
    // Public Properties
48
    // =========================================================================
49
50
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
51
     * @var string The image transform class to use for image transforms
52
     */
53
    public $transformClass = CraftImageTransform::class;
54
55
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
56
     * @var array Settings for the image transform components
57
     */
58
    public $imageTransformTypeSettings = [];
59
60
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
61
     * @var bool Should the image variants in an Asset Volume be automatically
62
     *      re-saved when saving an OptimizedImages field, saving an Asset
63
     *      Volume that has an OptimizedImages field in its layout, or saving
64
     *      the ImageOptimized settings. Set this to false only if you will be
65
     *      manually using the CLI console command to resave image variants
66
     */
67
    public $automaticallyResaveImageVariants = true;
68
69
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
70
     * @var bool Should image variant be created on Asset save (aka
71
     *      BeforePageLoad)
72
     */
73
    public $generateTransformsBeforePageLoad = true;
74
75
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
76
     * @var bool Set to false to disable all placeholder generation
77
     */
78
    public $generatePlaceholders = true;
79
80
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
81
     * @var bool Controls whether a dominant color palette should be created
82
     *      for image variants It takes a bit of time, so if you never plan to
83
     *      use it, you can turn it off
84
     */
85
    public $createColorPalette = true;
86
87
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
88
     * @var bool Controls whether SVG placeholder silhouettes should be created
89
     *      for image variants It takes a bit of time, so if you never plan to
90
     *      use them, you can turn it off
91
     */
92
    public $createPlaceholderSilhouettes = true;
93
94
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
95
     * @var bool Controls whether retina images are automatically created with
96
     *      reduced quality as per
97
     *      https://www.netvlies.nl/tips-updates/design-interactie/design-interactie/retina-revolution/
98
     */
99
    public $lowerQualityRetinaImageVariants = true;
100
101
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
102
     * @var bool Controls whether Optimized Image Variants are created that
103
     *      would be up-scaled to be larger than the original source image
104
     */
105
    public $allowUpScaledImageVariants = false;
106
107
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
108
     * @var bool Controls whether images scaled down >= 50% should be
109
     *      automatically sharpened
110
     */
111
    public $autoSharpenScaledImages = true;
112
113
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
114
     * @var ImageTransformInterface[] The default Image Transform type classes
115
     */
116
    public $defaultImageTransformTypes = [
117
    ];
118
119
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
120
     * @var array Default aspect ratios
121
     */
122
    public $defaultAspectRatios = [
123
        ['x' => 16, 'y' => 9],
124
        ['x' => 8, 'y' => 5],
125
        ['x' => 4, 'y' => 3],
126
        ['x' => 5, 'y' => 4],
127
        ['x' => 1, 'y' => 1],
128
        ['x' => 9, 'y' => 16],
129
        ['x' => 5, 'y' => 8],
130
        ['x' => 3, 'y' => 4],
131
        ['x' => 4, 'y' => 5],
132
    ];
133
134
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
135
     * @var array Default variants
136
     */
137
    public $defaultVariants = [
138
        [
139
            'width'          => 1200,
140
            'useAspectRatio' => true,
141
            'aspectRatioX'   => 16.0,
142
            'aspectRatioY'   => 9.0,
143
            'retinaSizes'    => ['1'],
144
            'quality'        => 82,
145
            'format'         => 'jpg',
146
        ],
147
        [
148
            'width'          => 992,
149
            'useAspectRatio' => true,
150
            'aspectRatioX'   => 16.0,
151
            'aspectRatioY'   => 9.0,
152
            'retinaSizes'    => ['1'],
153
            'quality'        => 82,
154
            'format'         => 'jpg',
155
        ],
156
        [
157
            'width'          => 768,
158
            'useAspectRatio' => true,
159
            'aspectRatioX'   => 4.0,
160
            'aspectRatioY'   => 3.0,
161
            'retinaSizes'    => ['1'],
162
            'quality'        => 60,
163
            'format'         => 'jpg',
164
        ],
165
        [
166
            'width'          => 576,
167
            'useAspectRatio' => true,
168
            'aspectRatioX'   => 4.0,
169
            'aspectRatioY'   => 3.0,
170
            'retinaSizes'    => ['1'],
171
            'quality'        => 60,
172
            'format'         => 'jpg',
173
        ],
174
    ];
175
176
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
177
     * @var array Active image processors
178
     */
179
    public $activeImageProcessors = [
180
        'jpg' => [
181
            'jpegoptim',
182
        ],
183
        'png' => [
184
            'optipng',
185
        ],
186
        'svg' => [
187
            'svgo',
188
        ],
189
        'gif' => [
190
            'gifsicle',
191
        ],
192
    ];
193
194
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
195
     * @var array Active image variant creators
196
     */
197
    public $activeImageVariantCreators = [
198
        'jpg' => [
199
            'cwebp',
200
        ],
201
        'png' => [
202
            'cwebp',
203
        ],
204
        'gif' => [
205
            'cwebp',
206
        ],
207
    ];
208
209
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
210
     * @var array Preset image processors
211
     */
212
    public $imageProcessors = [
213
        // jpeg optimizers
214
        'jpegoptim' => [
215
            'commandPath'           => '/usr/bin/jpegoptim',
216
            'commandOptions'        => '-s',
217
            'commandOutputFileFlag' => '',
218
        ],
219
        'mozjpeg'   => [
220
            'commandPath'           => '/usr/bin/mozjpeg',
221
            'commandOptions'        => '-optimize -copy none',
222
            'commandOutputFileFlag' => '-outfile',
223
        ],
224
        'jpegtran'  => [
225
            'commandPath'           => '/usr/bin/jpegtran',
226
            'commandOptions'        => '-optimize -copy none',
227
            'commandOutputFileFlag' => '',
228
        ],
229
        // png optimizers
230
        'optipng'   => [
231
            'commandPath'           => '/usr/bin/optipng',
232
            'commandOptions'        => '-o3 -strip all',
233
            'commandOutputFileFlag' => '',
234
        ],
235
        'pngcrush'  => [
236
            'commandPath'           => '/usr/bin/pngcrush',
237
            'commandOptions'        => '-brute -ow',
238
            'commandOutputFileFlag' => '',
239
        ],
240
        'pngquant'  => [
241
            'commandPath'           => '/usr/bin/pngquant',
242
            'commandOptions'        => '--strip --skip-if-larger',
243
            'commandOutputFileFlag' => '',
244
        ],
245
        // svg optimizers
246
        'svgo'      => [
247
            'commandPath'           => '/usr/bin/svgo',
248
            'commandOptions'        => '',
249
            'commandOutputFileFlag' => '',
250
        ],
251
        // gif optimizers
252
        'gifsicle'  => [
253
            'commandPath'           => '/usr/bin/gifsicle',
254
            'commandOptions'        => '-O3 -k 256',
255
            'commandOutputFileFlag' => '',
256
        ],
257
    ];
258
259
    public $imageVariantCreators = [
260
        // webp variant creator
261
        'cwebp' => [
262
            'commandPath'           => '/usr/bin/cwebp',
263
            'commandOptions'        => '',
264
            'commandOutputFileFlag' => '-o',
265
            'commandQualityFlag'    => '-q',
266
            'imageVariantExtension' => 'webp',
267
        ],
268
    ];
269
270
    // Public Methods
271
    // =========================================================================
272
273
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $config should have a doc-comment as per coding-style.
Loading history...
274
     * @inheritdoc
275
     */
276
    public function __construct(array $config = [])
277
    {
278
        // Unset any deprecated properties
279
        if (!empty($config)) {
280
            // Handle migrating old Imagix settings
281
            if (isset($config['imgixDomain'])) {
282
                $config['imageTransformTypeSettings'][ImgixImageTransform::class]['domain'] = $config['imgixDomain'];
283
            }
284
            if (isset($config['imgixApiKey'])) {
285
                $config['imageTransformTypeSettings'][ImgixImageTransform::class]['apiKey'] = $config['imgixApiKey'];
286
            }
287
            if (isset($config['imgixSecurityToken'])) {
288
                $config['imageTransformTypeSettings'][ImgixImageTransform::class]['securityToken'] = $config['imgixSecurityToken'];
289
            }
290
            // Handle migrating old Thumbor settings
291
            if (isset($config['thumborBaseUrl'])) {
292
                $config['imageTransformTypeSettings'][ThumborImageTransform::class]['baseUrl'] = $config['thumborBaseUrl'];
293
            }
294
            if (isset($config['thumborSecurityKey'])) {
295
                $config['imageTransformTypeSettings'][ThumborImageTransform::class]['securityKey'] = $config['thumborSecurityKey'];
296
            }
297
            // Remove deprecated properties
298
            foreach (self::DEPRECATED_PROPERTIES as $prop) {
299
                unset($config[$prop]);
300
            }
301
        }
302
303
        parent::__construct($config);
304
    }
305
306
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
307
     * @inheritdoc
308
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
309
    public function rules()
310
    {
311
        return [
312
            ['transformClass', 'string'],
313
            ['transformClass', 'default', 'value' => CraftImageTransform::class],
314
            [
315
                [
316
                    'automaticallyResaveImageVariants',
317
                    'generateTransformsBeforePageLoad',
318
                    'createColorPalette',
319
                    'createPlaceholderSilhouettes',
320
                    'lowerQualityRetinaImageVariants',
321
                    'allowUpScaledImageVariants',
322
                    'autoSharpenScaledImages',
323
                ],
324
                'boolean',
325
            ],
326
            [
327
                [
328
                    'defaultVariants',
329
                    'activeImageProcessors',
330
                    'activeImageVariantCreators',
331
                    'imageProcessors',
332
                    'imageVariantCreators',
333
                ],
334
                'required',
335
            ],
336
            [
337
                [
338
                    'imageTransformTypeSettings',
339
                    'defaultImageTransformTypes',
340
                    'defaultVariants',
341
                    'activeImageProcessors',
342
                    'activeImageVariantCreators',
343
                    'imageProcessors',
344
                    'imageVariantCreators',
345
                ],
346
                ArrayValidator::class,
347
            ],
348
        ];
349
    }
350
351
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
352
     * @inheritdoc
353
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
354
    public function fields()
355
    {
356
        // Only return user-editable settings
357
        $fields = [
358
            'transformClass',
359
            'imageTransformTypeSettings',
360
            'createColorPalette',
361
            'createPlaceholderSilhouettes',
362
            'lowerQualityRetinaImageVariants',
363
            'allowUpScaledImageVariants',
364
            'autoSharpenScaledImages',
365
        ];
366
367
        return $fields;
368
    }
369
370
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
371
     * @return array
372
     */
373
    public function behaviors()
374
    {
375
        $craft31Behaviors = [];
376
        if (ImageOptimize::$craft31) {
377
            $craft31Behaviors = [
378
                'parser' => [
379
                    'class' => EnvAttributeParserBehavior::class,
380
                    'attributes' => [
381
                    ],
382
                ]
383
            ];
384
        }
385
386
        return array_merge($craft31Behaviors, [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
387
            'typecast' => [
388
                'class' => AttributeTypecastBehavior::class,
389
                // 'attributeTypes' will be composed automatically according to `rules()`
390
            ],
391
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
392
    }
393
394
}
395