Passed
Push — develop ( a40575...26f189 )
by Andrew
03:50
created

ImageTransform   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
dl 0
loc 95
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A prefetchRemoteFile() 0 13 1
A getPurgeUrl() 0 5 1
A purgeUrl() 0 3 1
A getAssetUri() 0 7 1
A getTransformUrl() 0 5 1
A getTransformParams() 0 6 1
A getWebPUrl() 0 3 1
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
8
 * @copyright Copyright (c) 2017 nystudio107
9
 */
10
11
namespace nystudio107\imageoptimize\imagetransforms;
12
13
use nystudio107\imageoptimize\helpers\UrlHelper;
14
15
use craft\elements\Asset;
16
use craft\helpers\Assets as AssetsHelper;
17
use craft\models\AssetTransform;
18
19
/**
20
 * @author    nystudio107
21
 * @package   ImageOptimize
22
 * @since     1.0.0
23
 */
24
abstract class ImageTransform implements ImageTransformInterface
25
{
26
    // Public Static Methods
27
    // =========================================================================
28
29
    /**
30
     * @param Asset               $asset
31
     * @param AssetTransform|null $transform
32
     * @param array               $params
33
     *
34
     * @return string|null
35
     */
36
    public static function getTransformUrl(Asset $asset, $transform, array $params = [])
37
    {
38
        $url = null;
39
40
        return $url;
41
    }
42
43
    /**
44
     * @param string $url
45
     *
46
     * @return string
47
     */
48
    public static function getWebPUrl(string $url): string
49
    {
50
        return $url;
51
    }
52
53
    /**
54
     * @param Asset $asset
55
     * @param array $params
56
     *
57
     * @return null|string
58
     */
59
    public static function getPurgeUrl(Asset $asset, array $params = [])
60
    {
61
        $url = null;
62
63
        return $url;
64
    }
65
66
    /**
67
     * @param string $url
68
     * @param array  $params
69
     *
70
     * @return bool
71
     */
72
    public static function purgeUrl(string $url, array $params = []): bool
73
    {
74
        return true;
75
    }
76
77
    /**
78
     * @return array
79
     */
80
    public static function getTransformParams(): array
81
    {
82
        $params = [
83
        ];
84
85
        return $params;
86
    }
87
88
    /**
89
     * @param Asset $asset
90
     *
91
     * @return mixed
92
     * @throws \yii\base\InvalidConfigException
93
     */
94
    public static function getAssetUri(Asset $asset)
95
    {
96
        $volume = $asset->getVolume();
97
        $assetUrl = AssetsHelper::generateUrl($volume, $asset);
98
        $assetUri = parse_url($assetUrl, PHP_URL_PATH);
99
100
        return $assetUri;
101
    }
102
103
    /**
104
     * @param string $url
105
     */
106
    public static function prefetchRemoteFile($url)
107
    {
108
        // Get an absolute URL with protocol that curl will be happy with
109
        $url = UrlHelper::absoluteUrlWithProtocol($url);
110
        $ch = curl_init($url);
111
        curl_setopt_array($ch, [
112
            CURLOPT_RETURNTRANSFER => 1,
113
            CURLOPT_FOLLOWLOCATION => 1,
114
            CURLOPT_SSL_VERIFYPEER => 0,
115
            CURLOPT_NOBODY         => 1,
116
        ]);
117
        curl_exec($ch);
118
        curl_close($ch);
119
    }
120
}
121