Passed
Push — develop ( 5e1c96...396370 )
by Andrew
04:41
created

ImageTransform::getTemplatesRoot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 1
nc 1
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) 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
     * @return array
38
     * @throws \ReflectionException
39
     */
40
    public static function getTemplatesRoot(): array
41
    {
42
        $reflect = new \ReflectionClass(static::class);
43
        $classPath = FileHelper::normalizePath(
44
            dirname($reflect->getFileName())
45
            . '/../templates'
46
        )
47
        . DIRECTORY_SEPARATOR;
48
        $id = StringHelper::toKebabCase($reflect->getShortName());
49
50
        return [
51
            $id, $classPath
52
        ];
53
    }
54
55
    // Public Methods
56
    // =========================================================================
57
58
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
59
     * @param Asset               $asset
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
60
     * @param AssetTransform|null $transform
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
61
     * @param array               $params
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
62
     *
63
     * @return string|null
64
     */
65
    public function getTransformUrl(Asset $asset, $transform, array $params = [])
66
    {
67
        $url = null;
68
69
        return $url;
70
    }
71
72
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
73
     * @param string              $url
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
74
     * @param Asset               $asset
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
75
     * @param AssetTransform|null $transform
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
76
     * @param array               $params
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
77
     *
78
     * @return string
79
     */
80
    public function getWebPUrl(string $url, Asset $asset, $transform, array $params = []): string
81
    {
82
        return $url;
83
    }
84
85
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
86
     * @param Asset $asset
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
87
     * @param array $params
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
88
     *
89
     * @return null|string
90
     */
91
    public function getPurgeUrl(Asset $asset, array $params = [])
92
    {
93
        $url = null;
94
95
        return $url;
96
    }
97
98
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
99
     * @param string $url
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
100
     * @param array  $params
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
101
     *
102
     * @return bool
103
     */
104
    public function purgeUrl(string $url, array $params = []): bool
105
    {
106
        return true;
107
    }
108
109
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
110
     * @return array
111
     */
112
    public function getTransformParams(): array
113
    {
114
        $params = [
115
        ];
116
117
        return $params;
118
    }
119
120
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
121
     * @param Asset $asset
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
122
     *
123
     * @return mixed
124
     * @throws \yii\base\InvalidConfigException
125
     */
126
    public function getAssetUri(Asset $asset)
127
    {
128
        $volume = $asset->getVolume();
129
        $assetPath = $asset->getPath();
130
131
        // Account for volume types with a subfolder setting
132
        // e.g. craftcms/aws-s3, craftcms/google-cloud
133
        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...
134
            return rtrim($volume->subfolder, '/').'/'.$assetPath;
135
        }
136
137
        return $assetPath;
138
    }
139
140
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
141
     * @param string $url
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
142
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
143
    public function prefetchRemoteFile($url)
144
    {
145
        // Get an absolute URL with protocol that curl will be happy with
146
        $url = UrlHelper::absoluteUrlWithProtocol($url);
147
        $ch = curl_init($url);
148
        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...
149
            CURLOPT_RETURNTRANSFER => 1,
150
            CURLOPT_FOLLOWLOCATION => 1,
151
            CURLOPT_SSL_VERIFYPEER => 0,
152
            CURLOPT_NOBODY         => 1,
153
        ]);
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...
154
        curl_exec($ch);
155
        curl_close($ch);
156
    }
157
158
    /**
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...
159
     * Append an extension a passed url or path
160
     *
161
     * @param $pathOrUrl
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
162
     * @param $extension
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
163
     *
164
     * @return string
165
     */
166
    public function appendExtension($pathOrUrl, $extension): string
167
    {
168
        $path = $this->decomposeUrl($pathOrUrl);
169
        $path_parts = pathinfo($path['path']);
170
        $new_path = $path_parts['filename'] . '.' . $path_parts['extension'] . $extension;
171
        if (!empty($path_parts['dirname']) && $path_parts['dirname'] !== '.') {
172
            $new_path = $path_parts['dirname'] . DIRECTORY_SEPARATOR . $new_path;
173
            $new_path = preg_replace('/([^:])(\/{2,})/', '$1/', $new_path);
174
        }
175
        $output = $path['prefix'] . $new_path . $path['suffix'];
176
177
        return $output;
178
    }
179
180
    // Protected Methods
181
    // =========================================================================
182
183
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $pathOrUrl should have a doc-comment as per coding-style.
Loading history...
184
     * Decompose a url into a prefix, path, and suffix
185
     *
186
     * @param $pathOrUrl
0 ignored issues
show
Coding Style Documentation introduced by
Missing parameter name
Loading history...
187
     *
188
     * @return array
189
     */
190
    protected function decomposeUrl($pathOrUrl): array
191
    {
192
        $result = array();
193
194
        if (filter_var($pathOrUrl, FILTER_VALIDATE_URL)) {
195
            $url_parts = parse_url($pathOrUrl);
196
            $result['prefix'] = $url_parts['scheme'] . '://' . $url_parts['host'];
197
            $result['path'] = $url_parts['path'];
198
            $result['suffix'] = '';
199
            $result['suffix'] .= empty($url_parts['query']) ? '' : '?' . $url_parts['query'];
200
            $result['suffix'] .= empty($url_parts['fragment']) ? '' : '#' . $url_parts['fragment'];
201
        } else {
202
            $result['prefix'] = '';
203
            $result['path'] = $pathOrUrl;
204
            $result['suffix'] = '';
205
        }
206
207
        return $result;
208
    }
209
}
210