| Total Complexity | 67 |
| Total Lines | 455 |
| Duplicated Lines | 0 % |
| Changes | 15 | ||
| Bugs | 0 | Features | 0 |
Complex classes like OptimizedImages 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 OptimizedImages, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class OptimizedImages extends Component |
||
| 39 | { |
||
| 40 | // Constants |
||
| 41 | // ========================================================================= |
||
| 42 | |||
| 43 | // Public Properties |
||
| 44 | // ========================================================================= |
||
| 45 | |||
| 46 | // Public Methods |
||
| 47 | // ========================================================================= |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @param Asset $asset |
||
| 51 | * @param array $variants |
||
| 52 | * |
||
| 53 | * @return OptimizedImage|null |
||
| 54 | */ |
||
| 55 | public function createOptimizedImages(Asset $asset, array $variants = []) |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param Asset $asset |
||
| 74 | * @param array $variants |
||
| 75 | * @param OptimizedImage $model |
||
| 76 | */ |
||
| 77 | public function populateOptimizedImageModel(Asset $asset, $variants, OptimizedImage $model) |
||
| 146 | } |
||
| 147 | |||
| 148 | // Protected Methods |
||
| 149 | // ========================================================================= |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @param Field $field |
||
| 153 | * @param ElementInterface $asset |
||
| 154 | * |
||
| 155 | * @throws \yii\db\Exception |
||
| 156 | * @throws InvalidConfigException |
||
| 157 | */ |
||
| 158 | public function updateOptimizedImageFieldData(Field $field, ElementInterface $asset) |
||
| 159 | { |
||
| 160 | /** @var Asset $asset */ |
||
| 161 | if ($asset instanceof Asset && $field instanceof OptimizedImagesField) { |
||
| 162 | $createVariants = true; |
||
| 163 | Craft::info(print_r($field->fieldVolumeSettings, true), __METHOD__); |
||
| 164 | // See if we're ignoring files in this dir |
||
| 165 | if (!empty($field->fieldVolumeSettings)) { |
||
| 166 | foreach ($field->fieldVolumeSettings as $volumeHandle => $subfolders) { |
||
| 167 | if ($asset->getVolume()->handle === $volumeHandle) { |
||
| 168 | if (is_string($subfolders) && $subfolders === '*') { |
||
| 169 | $createVariants = true; |
||
| 170 | Craft::info("Matched '*' wildcard ", __METHOD__); |
||
| 171 | } else { |
||
| 172 | $createVariants = false; |
||
| 173 | if (is_array($subfolders)) { |
||
| 174 | foreach ($subfolders as $subfolder) { |
||
| 175 | if ($asset->getFolder()->uid === $subfolder) { |
||
| 176 | Craft::info('Matched subfolder uid: '.print_r($subfolder, true), __METHOD__); |
||
| 177 | $createVariants = true; |
||
| 178 | } |
||
| 179 | } |
||
| 180 | } |
||
| 181 | } |
||
| 182 | } |
||
| 183 | } |
||
| 184 | } |
||
| 185 | // See if we should ignore this type of file |
||
| 186 | $sourceType = $asset->getMimeType(); |
||
| 187 | if (!empty($field->ignoreFilesOfType) && $sourceType !== null) { |
||
| 188 | if (\in_array($sourceType, array_values($field->ignoreFilesOfType), false)) { |
||
| 189 | $createVariants = false; |
||
| 190 | } |
||
| 191 | } |
||
| 192 | Craft::info(print_r($sourceType, true), 'image-optimize'); |
||
| 193 | // Create a new OptimizedImage model and populate it |
||
| 194 | $model = new OptimizedImage(); |
||
| 195 | // Empty our the optimized image URLs |
||
| 196 | $model->optimizedImageUrls = []; |
||
| 197 | $model->optimizedWebPImageUrls = []; |
||
| 198 | $model->variantSourceWidths = []; |
||
| 199 | $model->placeholderWidth = 0; |
||
| 200 | $model->placeholderHeight = 0; |
||
| 201 | if ($asset !== null && $createVariants) { |
||
| 202 | $this->populateOptimizedImageModel( |
||
| 203 | $asset, |
||
| 204 | $field->variants, |
||
| 205 | $model |
||
| 206 | ); |
||
| 207 | } |
||
| 208 | // Save our field data directly into the content table |
||
| 209 | if ($field->handle !== null) { |
||
| 210 | $asset->setFieldValue($field->handle, $field->serializeValue($model)); |
||
| 211 | $table = $asset->getContentTable(); |
||
| 212 | $column = $asset->getFieldColumnPrefix().$field->handle; |
||
| 213 | $data = Json::encode($field->serializeValue($asset->getFieldValue($field->handle), $asset)); |
||
| 214 | Craft::$app->db->createCommand() |
||
| 215 | ->update($table, [ |
||
| 216 | $column => $data, |
||
| 217 | ], [ |
||
| 218 | 'elementId' => $asset->getId(), |
||
| 219 | ], [], false) |
||
| 220 | ->execute(); |
||
| 221 | } |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Re-save all of the assets in all of the volumes |
||
| 227 | * |
||
| 228 | * @throws \yii\base\InvalidConfigException |
||
| 229 | */ |
||
| 230 | public function resaveAllVolumesAssets() |
||
| 231 | { |
||
| 232 | $volumes = Craft::$app->getVolumes()->getAllVolumes(); |
||
| 233 | foreach ($volumes as $volume) { |
||
| 234 | if (is_subclass_of($volume, Volume::class)) { |
||
| 235 | /** @var Volume $volume */ |
||
| 236 | $this->resaveVolumeAssets($volume); |
||
| 237 | } |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Re-save all of the Asset elements in the Volume $volume that have an |
||
| 243 | * OptimizedImages field in the FieldLayout |
||
| 244 | * |
||
| 245 | * @param Volume $volume |
||
| 246 | * |
||
| 247 | * @throws \yii\base\InvalidConfigException |
||
| 248 | */ |
||
| 249 | public function resaveVolumeAssets(Volume $volume) |
||
| 250 | { |
||
| 251 | $needToReSave = false; |
||
| 252 | /** @var FieldLayout $fieldLayout */ |
||
| 253 | $fieldLayout = $volume->getFieldLayout(); |
||
| 254 | // Loop through the fields in the layout to see if there is an OptimizedImages field |
||
| 255 | if ($fieldLayout) { |
||
| 256 | $fields = $fieldLayout->getFields(); |
||
| 257 | foreach ($fields as $field) { |
||
| 258 | if ($field instanceof OptimizedImagesField) { |
||
| 259 | $needToReSave = true; |
||
| 260 | } |
||
| 261 | } |
||
| 262 | } |
||
| 263 | if ($needToReSave) { |
||
| 264 | try { |
||
| 265 | $siteId = Craft::$app->getSites()->getPrimarySite()->id; |
||
| 266 | } catch (SiteNotFoundException $e) { |
||
| 267 | $siteId = 0; |
||
| 268 | Craft::error( |
||
| 269 | 'Failed to get primary site: '.$e->getMessage(), |
||
| 270 | __METHOD__ |
||
| 271 | ); |
||
| 272 | } |
||
| 273 | |||
| 274 | $queue = Craft::$app->getQueue(); |
||
| 275 | $jobId = $queue->push(new ResaveOptimizedImages([ |
||
| 276 | 'description' => Craft::t('image-optimize', 'Optimizing images in {name}', ['name' => $volume->name]), |
||
| 277 | 'criteria' => [ |
||
| 278 | 'siteId' => $siteId, |
||
| 279 | 'volumeId' => $volume->id, |
||
| 280 | 'status' => null, |
||
| 281 | 'enabledForSite' => false, |
||
| 282 | ], |
||
| 283 | ])); |
||
| 284 | Craft::debug( |
||
| 285 | Craft::t( |
||
| 286 | 'image-optimize', |
||
| 287 | 'Started resaveVolumeAssets queue job id: {jobId}', |
||
| 288 | [ |
||
| 289 | 'jobId' => $jobId, |
||
| 290 | ] |
||
| 291 | ), |
||
| 292 | __METHOD__ |
||
| 293 | ); |
||
| 294 | } |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Re-save an individual asset |
||
| 299 | * |
||
| 300 | * @param int $id |
||
| 301 | */ |
||
| 302 | public function resaveAsset(int $id) |
||
| 303 | { |
||
| 304 | $queue = Craft::$app->getQueue(); |
||
| 305 | $jobId = $queue->push(new ResaveOptimizedImages([ |
||
| 306 | 'description' => Craft::t('image-optimize', 'Optimizing image id {id}', ['id' => $id]), |
||
| 307 | 'criteria' => [ |
||
| 308 | 'id' => $id, |
||
| 309 | 'status' => null, |
||
| 310 | 'enabledForSite' => false, |
||
| 311 | ], |
||
| 312 | ])); |
||
| 313 | Craft::debug( |
||
| 314 | Craft::t( |
||
| 315 | 'image-optimize', |
||
| 316 | 'Started resaveAsset queue job id: {jobId} Element id: {elementId}', |
||
| 317 | [ |
||
| 318 | 'elementId' => $id, |
||
| 319 | 'jobId' => $jobId, |
||
| 320 | ] |
||
| 321 | ), |
||
| 322 | __METHOD__ |
||
| 323 | ); |
||
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Create an optimized SVG data uri |
||
| 328 | * See: https://codepen.io/tigt/post/optimizing-svgs-in-data-uris |
||
| 329 | * |
||
| 330 | * @param string $uri |
||
| 331 | * |
||
| 332 | * @return string |
||
| 333 | */ |
||
| 334 | public function encodeOptimizedSVGDataUri(string $uri): string |
||
| 335 | { |
||
| 336 | // First, uri encode everything |
||
| 337 | $uri = rawurlencode($uri); |
||
| 338 | $replacements = [ |
||
| 339 | // remove newlines |
||
| 340 | '/%0A/' => '', |
||
| 341 | // put spaces back in |
||
| 342 | '/%20/' => ' ', |
||
| 343 | // put equals signs back in |
||
| 344 | '/%3D/' => '=', |
||
| 345 | // put colons back in |
||
| 346 | '/%3A/' => ':', |
||
| 347 | // put slashes back in |
||
| 348 | '/%2F/' => '/', |
||
| 349 | // replace quotes with apostrophes (may break certain SVGs) |
||
| 350 | '/%22/' => "'", |
||
| 351 | ]; |
||
| 352 | foreach ($replacements as $pattern => $replacement) { |
||
| 353 | $uri = preg_replace($pattern, $replacement, $uri); |
||
| 354 | } |
||
| 355 | |||
| 356 | return $uri; |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @param Asset $element |
||
| 361 | * @param OptimizedImage $model |
||
| 362 | * @param $aspectRatio |
||
| 363 | */ |
||
| 364 | protected function generatePlaceholders(Asset $element, OptimizedImage $model, $aspectRatio) |
||
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @param Asset $asset |
||
| 397 | * @param $variant |
||
| 398 | * @param $retinaSize |
||
| 399 | * |
||
| 400 | * @return array |
||
| 401 | */ |
||
| 402 | protected function getTransformFromVariant(Asset $asset, $variant, $retinaSize): array |
||
| 442 | } |
||
| 443 | |||
| 444 | /** |
||
| 445 | * @param Asset $asset |
||
| 446 | * @param OptimizedImage $model |
||
| 447 | * @param $transform |
||
| 448 | * @param $variant |
||
| 449 | * @param $aspectRatio |
||
| 450 | */ |
||
| 451 | protected function addVariantImageToModel(Asset $asset, OptimizedImage $model, $transform, $variant, $aspectRatio) |
||
| 495 |