Passed
Push — develop ( dc584b...07feac )
by Andrew
06:36
created

ImageOptimizeVariable   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 12
dl 0
loc 76
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A placeholderBox() 0 3 1
A serverSupportsWebP() 0 11 4
A createOptimizedImages() 0 9 1
A createImageTransformType() 0 3 1
A craft31() 0 3 1
1
<?php
2
/**
3
 * Image Optimize 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\variables;
12
13
use nystudio107\imageoptimize\ImageOptimize;
14
use nystudio107\imageoptimize\imagetransforms\ImageTransformInterface;
15
use nystudio107\imageoptimize\models\OptimizedImage;
16
17
use craft\elements\Asset;
18
use craft\helpers\Template;
19
20
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
21
 * @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 indented incorrectly; expected 2 spaces but found 4
Loading history...
22
 * @package   ImageOptimize
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 3
Loading history...
23
 * @since     1.4.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 indented incorrectly; expected 3 spaces but found 5
Loading history...
24
 */
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...
25
class ImageOptimizeVariable extends ManifestVariable
26
{
27
    // Public Methods
28
    // =========================================================================
29
30
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $width should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $height should have a doc-comment as per coding-style.
Loading history...
31
     * Return an SVG box as a placeholder image
32
     *
33
     * @param             $width
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 13
Loading history...
34
     * @param             $height
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 13
Loading history...
35
     * @param string|null $color
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
36
     *
37
     * @return \Twig_Markup|null
38
     */
39
    public function placeholderBox($width, $height, $color = null)
40
    {
41
        return Template::raw(ImageOptimize::$plugin->placeholder->generatePlaceholderBox($width, $height, $color));
42
    }
43
44
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
45
     * @param Asset $asset
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
46
     * @param array $variants
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
47
     * @param bool  $generatePlaceholders
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
48
     *
49
     * @return OptimizedImage|null
50
     */
51
    public function createOptimizedImages(
52
        Asset $asset,
53
        $variants = null,
54
        $generatePlaceholders = false
55
    ) {
56
        // Override our settings for lengthy operations, since we're doing this via Twig
57
        ImageOptimize::$generatePlaceholders = $generatePlaceholders;
58
59
        return ImageOptimize::$plugin->optimizedImages->createOptimizedImages($asset, $variants);
0 ignored issues
show
Bug introduced by
It seems like $variants can also be of type null; however, parameter $variants of nystudio107\imageoptimiz...createOptimizedImages() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

59
        return ImageOptimize::$plugin->optimizedImages->createOptimizedImages($asset, /** @scrutinizer ignore-type */ $variants);
Loading history...
60
    }
61
62
    /**
63
     * Returns whether `.webp` is a format supported by the server
64
     *
65
     * @return bool
66
     */
67
    public function serverSupportsWebP(): bool
68
    {
69
        $result = false;
70
        $variantCreators = ImageOptimize::$plugin->optimize->getActiveVariantCreators();
71
        foreach ($variantCreators as $variantCreator) {
72
            if ($variantCreator['creator'] === 'cwebp' && $variantCreator['installed']) {
73
                $result = true;
74
            }
75
        }
76
77
        return $result;
78
    }
79
80
    /**
81
     * Creates an Image Transform with a given config.
82
     *
83
     * @param mixed $config The Image Transform’s class name, or its config,
84
     *                      with a `type` value and optionally a `settings` value
85
     *
86
     * @return null|ImageTransformInterface The Image Transform
87
     */
88
    public function createImageTransformType($config): ImageTransformInterface
89
    {
90
        return ImageOptimize::$plugin->optimize->createImageTransformType($config);
91
    }
92
93
    /**
94
     * Return whether we are running Craft 3.1 or later
95
     *
96
     * @return bool
97
     */
98
    public function craft31(): bool
99
    {
100
        return ImageOptimize::$craft31;
101
    }
102
}
103