| Total Complexity | 40 |
| Total Lines | 310 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ImgixImageTransform often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ImgixImageTransform, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class ImgixImageTransform extends ImageTransform |
||
| 32 | { |
||
| 33 | // Constants |
||
| 34 | // ========================================================================= |
||
| 35 | |||
| 36 | const TRANSFORM_ATTRIBUTES_MAP = [ |
||
| 37 | 'width' => 'w', |
||
| 38 | 'height' => 'h', |
||
| 39 | 'quality' => 'q', |
||
| 40 | 'format' => 'fm', |
||
| 41 | ]; |
||
| 42 | |||
| 43 | const IMGIX_PURGE_ENDPOINT = 'https://api.imgix.com/v2/image/purger'; |
||
| 44 | |||
| 45 | // Static Methods |
||
| 46 | // ========================================================================= |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @inheritdoc |
||
| 50 | */ |
||
| 51 | public static function displayName(): string |
||
| 54 | } |
||
| 55 | |||
| 56 | // Public Properties |
||
| 57 | // ========================================================================= |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | public $domain; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | public $apiKey; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | public $securityToken; |
||
| 73 | |||
| 74 | // Public Methods |
||
| 75 | // ========================================================================= |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param Asset $asset |
||
| 79 | * @param AssetTransform|null $transform |
||
| 80 | * @param array $params |
||
| 81 | * |
||
| 82 | * @return string|null |
||
| 83 | * @throws \yii\base\Exception |
||
| 84 | * @throws \yii\base\InvalidConfigException |
||
| 85 | */ |
||
| 86 | public function getTransformUrl(Asset $asset, $transform, array $params = []) |
||
| 87 | { |
||
| 88 | $url = null; |
||
| 89 | $settings = ImageOptimize::$plugin->getSettings(); |
||
| 90 | |||
| 91 | $domain = $params['domain'] ?? 'demos.imgix.net'; |
||
| 92 | $builder = new UrlBuilder($domain); |
||
| 93 | if ($asset && $builder) { |
||
| 94 | $builder->setUseHttps(true); |
||
| 95 | if ($transform) { |
||
| 96 | // Map the transform properties |
||
| 97 | foreach (self::TRANSFORM_ATTRIBUTES_MAP as $key => $value) { |
||
| 98 | if (!empty($transform[$key])) { |
||
| 99 | $params[$value] = $transform[$key]; |
||
| 100 | } |
||
| 101 | } |
||
| 102 | // Remove any 'AUTO' settings |
||
| 103 | ArrayHelper::removeValue($params, 'AUTO'); |
||
| 104 | // Handle the Imgix auto setting for compression/format |
||
| 105 | $autoParams = []; |
||
| 106 | if (empty($params['q'])) { |
||
| 107 | $autoParams[] = 'compress'; |
||
| 108 | } |
||
| 109 | if (empty($params['fm'])) { |
||
| 110 | $autoParams[] = 'format'; |
||
| 111 | } |
||
| 112 | if (!empty($autoParams)) { |
||
| 113 | $params['auto'] = implode(',', $autoParams); |
||
| 114 | } |
||
| 115 | // Handle interlaced images |
||
| 116 | if (property_exists($transform, 'interlace')) { |
||
| 117 | if (($transform->interlace != 'none') |
||
| 118 | && (!empty($params['fm'])) |
||
| 119 | && ($params['fm'] == 'jpg') |
||
| 120 | ) { |
||
| 121 | $params['fm'] = 'pjpg'; |
||
| 122 | } |
||
| 123 | } |
||
| 124 | if ($settings->autoSharpenScaledImages) { |
||
| 125 | // See if the image has been scaled >= 50% |
||
| 126 | $widthScale = $asset->getWidth() / ($transform->width ?? $asset->getWidth()); |
||
| 127 | $heightScale = $asset->getHeight() / ($transform->height ?? $asset->getHeight()); |
||
| 128 | if (($widthScale >= 2.0) || ($heightScale >= 2.0)) { |
||
| 129 | $params['usm'] = 50.0; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | // Handle the mode |
||
| 133 | switch ($transform->mode) { |
||
| 134 | case 'fit': |
||
| 135 | $params['fit'] = 'clamp'; |
||
| 136 | break; |
||
| 137 | |||
| 138 | case 'stretch': |
||
| 139 | $params['fit'] = 'scale'; |
||
| 140 | break; |
||
| 141 | |||
| 142 | default: |
||
| 143 | // Set a sane default |
||
| 144 | if (empty($transform->position)) { |
||
| 145 | $transform->position = 'center-center'; |
||
| 146 | } |
||
| 147 | // Fit mode |
||
| 148 | $params['fit'] = 'crop'; |
||
| 149 | $cropParams = []; |
||
| 150 | // Handle the focal point |
||
| 151 | $focalPoint = $asset->getFocalPoint(); |
||
| 152 | if (!empty($focalPoint)) { |
||
| 153 | $params['fp-x'] = $focalPoint['x']; |
||
| 154 | $params['fp-y'] = $focalPoint['y']; |
||
| 155 | $cropParams[] = 'focalpoint'; |
||
| 156 | } elseif (preg_match('/(top|center|bottom)-(left|center|right)/', $transform->position)) { |
||
| 157 | // Imgix defaults to 'center' if no param is present |
||
| 158 | $filteredCropParams = explode('-', $transform->position); |
||
| 159 | $filteredCropParams = array_diff($filteredCropParams, ['center']); |
||
| 160 | $cropParams[] = $filteredCropParams; |
||
| 161 | } |
||
| 162 | // Imgix |
||
| 163 | if (!empty($cropParams) && $transform->position !== 'center-center') { |
||
| 164 | $params['crop'] = implode(',', $cropParams); |
||
| 165 | } |
||
| 166 | break; |
||
| 167 | } |
||
| 168 | } else { |
||
| 169 | // No transform was passed in; so just auto all the things |
||
| 170 | $params['auto'] = 'format,compress'; |
||
| 171 | } |
||
| 172 | // Remove the api-key param |
||
| 173 | unset($params['api-key']); |
||
| 174 | // Apply the Security Token, if set |
||
| 175 | if (!empty($settings->imgixSecurityToken)) { |
||
| 176 | $builder->setSignKey($settings->imgixSecurityToken); |
||
| 177 | } |
||
| 178 | // Finally, create the Imgix URL for this transformed image |
||
| 179 | $assetUri = $this->getAssetUri($asset); |
||
| 180 | $url = $builder->createURL($assetUri, $params); |
||
| 181 | Craft::debug( |
||
| 182 | 'Imgix transform created for: '.$assetUri.' - Params: '.print_r($params, true).' - URL: '.$url, |
||
| 183 | __METHOD__ |
||
| 184 | ); |
||
| 185 | } |
||
| 186 | |||
| 187 | return $url; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param string $url |
||
| 192 | * @param Asset $asset |
||
| 193 | * @param AssetTransform|null $transform |
||
| 194 | * @param array $params |
||
| 195 | * |
||
| 196 | * @return string |
||
| 197 | */ |
||
| 198 | public function getWebPUrl(string $url, Asset $asset, $transform, array $params = []): string |
||
| 199 | { |
||
| 200 | $url = preg_replace('/fm=[^&]*/', 'fm=webp', $url); |
||
| 201 | |||
| 202 | return $url; |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param Asset $asset |
||
| 207 | * @param array $params |
||
| 208 | * |
||
| 209 | * @return null|string |
||
| 210 | * @throws \yii\base\InvalidConfigException |
||
| 211 | */ |
||
| 212 | public function getPurgeUrl(Asset $asset, array $params = []) |
||
| 213 | { |
||
| 214 | $url = null; |
||
| 215 | |||
| 216 | $domain = isset($params['domain']) |
||
| 217 | ? $params['domain'] |
||
| 218 | : 'demos.imgix.net'; |
||
| 219 | $builder = new UrlBuilder($domain); |
||
| 220 | if ($asset && $builder) { |
||
| 221 | $builder->setUseHttps(true); |
||
| 222 | // Create the Imgix URL for purging this image |
||
| 223 | $assetUri = $this->getAssetUri($asset); |
||
| 224 | $url = $builder->createURL($assetUri, $params); |
||
| 225 | // Strip the query string so we just pass in the raw URL |
||
| 226 | $url = UrlHelper::stripQueryString($url); |
||
| 227 | } |
||
| 228 | |||
| 229 | return $url; |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param string $url |
||
| 234 | * @param array $params |
||
| 235 | * |
||
| 236 | * @return bool |
||
| 237 | */ |
||
| 238 | public function purgeUrl(string $url, array $params = []): bool |
||
| 239 | { |
||
| 240 | $result = false; |
||
| 241 | $apiKey = isset($params['api-key']) |
||
| 242 | ? $params['api-key'] |
||
| 243 | : ''; |
||
| 244 | // create new guzzle client |
||
| 245 | $guzzleClient = Craft::createGuzzleClient(['timeout' => 120, 'connect_timeout' => 120]); |
||
| 246 | // Submit the sitemap index to each search engine |
||
| 247 | try { |
||
| 248 | /** @var ResponseInterface $response */ |
||
| 249 | $response = $guzzleClient->post(self::IMGIX_PURGE_ENDPOINT, [ |
||
| 250 | 'auth' => [ |
||
| 251 | $apiKey, |
||
| 252 | '', |
||
| 253 | ], |
||
| 254 | 'form_params' => [ |
||
| 255 | 'url' => $url, |
||
| 256 | ], |
||
| 257 | ]); |
||
| 258 | // See if it succeeded |
||
| 259 | if (($response->getStatusCode() >= 200) |
||
| 260 | && ($response->getStatusCode() < 400) |
||
| 261 | ) { |
||
| 262 | $result = true; |
||
| 263 | } |
||
| 264 | Craft::info( |
||
| 265 | 'URL purged: '.$url.' - Response code: '.$response->getStatusCode(), |
||
| 266 | __METHOD__ |
||
| 267 | ); |
||
| 268 | } catch (\Exception $e) { |
||
| 269 | Craft::error( |
||
| 270 | 'Error purging URL: '.$url.' - '.$e->getMessage(), |
||
| 271 | __METHOD__ |
||
| 272 | ); |
||
| 273 | } |
||
| 274 | |||
| 275 | return $result; |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @return array |
||
| 280 | */ |
||
| 281 | public function getTransformParams(): array |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * @param Asset $asset |
||
| 294 | * |
||
| 295 | * @return mixed |
||
| 296 | * @throws \yii\base\InvalidConfigException |
||
| 297 | */ |
||
| 298 | public function getAssetUri(Asset $asset) |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @inheritdoc |
||
| 321 | */ |
||
| 322 | public function getSettingsHtml() |
||
| 326 | ]); |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @inheritdoc |
||
| 331 | */ |
||
| 332 | public function rules() |
||
| 343 |