Passed
Push — v1 ( 5688db...40928d )
by Andrew
10:01 queued 06:18
created

ImageTransform::getTemplatesRoot()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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