Passed
Push — develop ( 396370...e3c868 )
by Andrew
03:50
created

ImageTransform::purgeUrl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 2
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) 2018 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\imagetransforms;
12
13
use nystudio107\imageoptimize\helpers\UrlHelper;
14
15
use craft\base\SavableComponent;
16
use craft\elements\Asset;
17
use craft\helpers\FileHelper;
18
use craft\helpers\StringHelper;
19
use craft\models\AssetTransform;
20
21
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
22
 * @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...
23
 * @package   ImageOptimize
0 ignored issues
show
Coding Style introduced by
Tag value indented incorrectly; expected 1 spaces but found 3
Loading history...
24
 * @since     1.5.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...
25
 */
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...
26
abstract class ImageTransform extends SavableComponent implements ImageTransformInterface
27
{
28
    // Traits
29
    // =========================================================================
30
31
    use ImageTransformTrait;
32
33
    // Static Methods
34
    // =========================================================================
35
36
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
37
     * @inheritdoc
38
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
39
    public static function displayName(): string
40
    {
41
        return Craft::t('image-optimize', 'Generic Transform');
42
    }
43
44
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
45
     * @inheritdoc
46
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
47
    public static function getTemplatesRoot(): array
48
    {
49
        $reflect = new \ReflectionClass(static::class);
50
        $classPath = FileHelper::normalizePath(
51
            dirname($reflect->getFileName())
52
            . '/../templates'
53
        )
54
        . DIRECTORY_SEPARATOR;
55
        $id = StringHelper::toKebabCase($reflect->getShortName());
56
57
        return [
58
            $id, $classPath
59
        ];
60
    }
61
62
    // Public Methods
63
    // =========================================================================
64
65
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $asset should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $transform should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $params should have a doc-comment as per coding-style.
Loading history...
66
     * @inheritdoc
67
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
68
    public function getTransformUrl(Asset $asset, $transform, array $params = [])
69
    {
70
        $url = null;
71
72
        return $url;
73
    }
74
75
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $url should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $asset should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $transform should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $params should have a doc-comment as per coding-style.
Loading history...
76
     * @inheritdoc
77
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
78
    public function getWebPUrl(string $url, Asset $asset, $transform, array $params = []): string
79
    {
80
        return $url;
81
    }
82
83
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $asset should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $params should have a doc-comment as per coding-style.
Loading history...
84
     * @inheritdoc
85
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
86
    public function getPurgeUrl(Asset $asset, array $params = [])
87
    {
88
        $url = null;
89
90
        return $url;
91
    }
92
93
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $url should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $params should have a doc-comment as per coding-style.
Loading history...
94
     * @inheritdoc
95
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
96
    public function purgeUrl(string $url, array $params = []): bool
97
    {
98
        return true;
99
    }
100
101
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $asset should have a doc-comment as per coding-style.
Loading history...
102
     * @inheritdoc
103
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
104
    public function getAssetUri(Asset $asset)
105
    {
106
        $volume = $asset->getVolume();
107
        $assetPath = $asset->getPath();
108
109
        // Account for volume types with a subfolder setting
110
        // e.g. craftcms/aws-s3, craftcms/google-cloud
111
        if ($volume->subfolder ?? null) {
0 ignored issues
show
Bug introduced by
Accessing subfolder on the interface craft\base\VolumeInterface suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
112
            return rtrim($volume->subfolder, '/').'/'.$assetPath;
113
        }
114
115
        return $assetPath;
116
    }
117
118
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
119
     * @param string $url
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
120
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
121
    public function prefetchRemoteFile($url)
122
    {
123
        // Get an absolute URL with protocol that curl will be happy with
124
        $url = UrlHelper::absoluteUrlWithProtocol($url);
125
        $ch = curl_init($url);
126
        curl_setopt_array($ch, [
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...
127
            CURLOPT_RETURNTRANSFER => 1,
128
            CURLOPT_FOLLOWLOCATION => 1,
129
            CURLOPT_SSL_VERIFYPEER => 0,
130
            CURLOPT_NOBODY         => 1,
131
        ]);
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...
132
        curl_exec($ch);
133
        curl_close($ch);
134
    }
135
136
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
137
     * @inheritdoc
138
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
139
    public function getTransformParams(): array
140
    {
141
        $params = [
142
        ];
143
144
        return $params;
145
    }
146
147
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $pathOrUrl should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $extension should have a doc-comment as per coding-style.
Loading history...
148
     * Append an extension a passed url or path
149
     *
150
     * @param $pathOrUrl
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
151
     * @param $extension
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
152
     *
153
     * @return string
154
     */
155
    public function appendExtension($pathOrUrl, $extension): string
156
    {
157
        $path = $this->decomposeUrl($pathOrUrl);
158
        $path_parts = pathinfo($path['path']);
159
        $new_path = $path_parts['filename'] . '.' . $path_parts['extension'] . $extension;
160
        if (!empty($path_parts['dirname']) && $path_parts['dirname'] !== '.') {
161
            $new_path = $path_parts['dirname'] . DIRECTORY_SEPARATOR . $new_path;
162
            $new_path = preg_replace('/([^:])(\/{2,})/', '$1/', $new_path);
163
        }
164
        $output = $path['prefix'] . $new_path . $path['suffix'];
165
166
        return $output;
167
    }
168
169
    // Protected Methods
170
    // =========================================================================
171
172
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $pathOrUrl should have a doc-comment as per coding-style.
Loading history...
173
     * Decompose a url into a prefix, path, and suffix
174
     *
175
     * @param $pathOrUrl
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
176
     *
177
     * @return array
178
     */
179
    protected function decomposeUrl($pathOrUrl): array
180
    {
181
        $result = array();
182
183
        if (filter_var($pathOrUrl, FILTER_VALIDATE_URL)) {
184
            $url_parts = parse_url($pathOrUrl);
185
            $result['prefix'] = $url_parts['scheme'] . '://' . $url_parts['host'];
186
            $result['path'] = $url_parts['path'];
187
            $result['suffix'] = '';
188
            $result['suffix'] .= empty($url_parts['query']) ? '' : '?' . $url_parts['query'];
189
            $result['suffix'] .= empty($url_parts['fragment']) ? '' : '#' . $url_parts['fragment'];
190
        } else {
191
            $result['prefix'] = '';
192
            $result['path'] = $pathOrUrl;
193
            $result['suffix'] = '';
194
        }
195
196
        return $result;
197
    }
198
}
199