| Total Complexity | 54 |
| Total Lines | 419 |
| Duplicated Lines | 0 % |
| Changes | 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 |
||
| 35 | class OptimizedImages extends Component |
||
| 36 | { |
||
| 37 | // Constants |
||
| 38 | // ========================================================================= |
||
| 39 | |||
| 40 | // Public Properties |
||
| 41 | // ========================================================================= |
||
| 42 | |||
| 43 | // Public Methods |
||
| 44 | // ========================================================================= |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @param Asset $asset |
||
| 48 | * @param array $variants |
||
| 49 | * |
||
| 50 | * @return OptimizedImage|null |
||
| 51 | */ |
||
| 52 | public function createOptimizedImages(Asset $asset, array $variants = []) |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param Asset $asset |
||
| 71 | * @param array $variants |
||
| 72 | * @param OptimizedImage $model |
||
| 73 | */ |
||
| 74 | public function populateOptimizedImageModel(Asset $asset, $variants, OptimizedImage $model) |
||
| 144 | } |
||
| 145 | |||
| 146 | // Protected Methods |
||
| 147 | // ========================================================================= |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param Field $field |
||
| 151 | * @param ElementInterface $asset |
||
| 152 | * |
||
| 153 | * @throws \yii\db\Exception |
||
| 154 | */ |
||
| 155 | public function updateOptimizedImageFieldData(Field $field, ElementInterface $asset) |
||
| 156 | { |
||
| 157 | /** @var Asset $asset */ |
||
| 158 | if ($asset instanceof Asset && $field instanceof OptimizedImagesField) { |
||
| 159 | $createVariants = true; |
||
| 160 | $sourceType = $asset->getMimeType(); |
||
| 161 | if (!empty($field->ignoreFilesOfType) && $sourceType !== null) { |
||
| 162 | if (\in_array($sourceType, array_values($field->ignoreFilesOfType), false)) { |
||
| 163 | $createVariants = false; |
||
| 164 | } |
||
| 165 | } |
||
| 166 | Craft::error(print_r($sourceType, true), 'image-optimize'); |
||
| 167 | // Create a new OptimizedImage model and populate it |
||
| 168 | $model = new OptimizedImage(); |
||
| 169 | // Empty our the optimized image URLs |
||
| 170 | $model->optimizedImageUrls = []; |
||
| 171 | $model->optimizedWebPImageUrls = []; |
||
| 172 | $model->variantSourceWidths = []; |
||
| 173 | $model->placeholderWidth = 0; |
||
| 174 | $model->placeholderHeight = 0; |
||
| 175 | if ($asset !== null && $createVariants) { |
||
| 176 | $this->populateOptimizedImageModel( |
||
| 177 | $asset, |
||
| 178 | $field->variants, |
||
| 179 | $model |
||
| 180 | ); |
||
| 181 | } |
||
| 182 | // Save our field data directly into the content table |
||
| 183 | if ($field->handle !== null) { |
||
| 184 | $asset->setFieldValue($field->handle, $field->serializeValue($model)); |
||
| 185 | $table = $asset->getContentTable(); |
||
| 186 | $column = $asset->getFieldColumnPrefix().$field->handle; |
||
| 187 | $data = Json::encode($field->serializeValue($asset->getFieldValue($field->handle), $asset)); |
||
| 188 | Craft::$app->db->createCommand() |
||
| 189 | ->update($table, [ |
||
| 190 | $column => $data, |
||
| 191 | ], [ |
||
| 192 | 'elementId' => $asset->getId(), |
||
| 193 | ], [], false) |
||
| 194 | ->execute(); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Re-save all of the assets in all of the volumes |
||
| 201 | * |
||
| 202 | * @throws \yii\base\InvalidConfigException |
||
| 203 | */ |
||
| 204 | public function resaveAllVolumesAssets() |
||
| 205 | { |
||
| 206 | $volumes = Craft::$app->getVolumes()->getAllVolumes(); |
||
| 207 | foreach ($volumes as $volume) { |
||
| 208 | if (is_subclass_of($volume, Volume::class)) { |
||
| 209 | /** @var Volume $volume */ |
||
| 210 | $this->resaveVolumeAssets($volume); |
||
| 211 | } |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Re-save all of the Asset elements in the Volume $volume that have an |
||
| 217 | * OptimizedImages field in the FieldLayout |
||
| 218 | * |
||
| 219 | * @param Volume $volume |
||
| 220 | * |
||
| 221 | * @throws \yii\base\InvalidConfigException |
||
| 222 | */ |
||
| 223 | public function resaveVolumeAssets(Volume $volume) |
||
| 224 | { |
||
| 225 | $needToReSave = false; |
||
| 226 | /** @var FieldLayout $fieldLayout */ |
||
| 227 | $fieldLayout = $volume->getFieldLayout(); |
||
| 228 | // Loop through the fields in the layout to see if there is an OptimizedImages field |
||
| 229 | if ($fieldLayout) { |
||
| 230 | $fields = $fieldLayout->getFields(); |
||
| 231 | foreach ($fields as $field) { |
||
| 232 | if ($field instanceof OptimizedImagesField) { |
||
| 233 | $needToReSave = true; |
||
| 234 | } |
||
| 235 | } |
||
| 236 | } |
||
| 237 | if ($needToReSave) { |
||
| 238 | try { |
||
| 239 | $siteId = Craft::$app->getSites()->getPrimarySite()->id; |
||
| 240 | } catch (SiteNotFoundException $e) { |
||
| 241 | $siteId = 0; |
||
| 242 | Craft::error( |
||
| 243 | 'Failed to get primary site: '.$e->getMessage(), |
||
| 244 | __METHOD__ |
||
| 245 | ); |
||
| 246 | } |
||
| 247 | |||
| 248 | $queue = Craft::$app->getQueue(); |
||
| 249 | $jobId = $queue->push(new ResaveOptimizedImages([ |
||
| 250 | 'description' => Craft::t('image-optimize', 'Optimizing images in {name}', ['name' => $volume->name]), |
||
| 251 | 'criteria' => [ |
||
| 252 | 'siteId' => $siteId, |
||
| 253 | 'volumeId' => $volume->id, |
||
| 254 | 'status' => null, |
||
| 255 | 'enabledForSite' => false, |
||
| 256 | ], |
||
| 257 | ])); |
||
| 258 | Craft::debug( |
||
| 259 | Craft::t( |
||
| 260 | 'image-optimize', |
||
| 261 | 'Started resaveVolumeAssets queue job id: {jobId}', |
||
| 262 | [ |
||
| 263 | 'jobId' => $jobId, |
||
| 264 | ] |
||
| 265 | ), |
||
| 266 | __METHOD__ |
||
| 267 | ); |
||
| 268 | } |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Re-save an individual asset |
||
| 273 | * |
||
| 274 | * @param int $id |
||
| 275 | */ |
||
| 276 | public function resaveAsset(int $id) |
||
| 277 | { |
||
| 278 | $queue = Craft::$app->getQueue(); |
||
| 279 | $jobId = $queue->push(new ResaveOptimizedImages([ |
||
| 280 | 'description' => Craft::t('image-optimize', 'Optimizing image id {id}', ['id' => $id]), |
||
| 281 | 'criteria' => [ |
||
| 282 | 'id' => $id, |
||
| 283 | 'status' => null, |
||
| 284 | 'enabledForSite' => false, |
||
| 285 | ], |
||
| 286 | ])); |
||
| 287 | Craft::debug( |
||
| 288 | Craft::t( |
||
| 289 | 'image-optimize', |
||
| 290 | 'Started resaveAsset queue job id: {jobId} Element id: {elementId}', |
||
| 291 | [ |
||
| 292 | 'elementId' => $id, |
||
| 293 | 'jobId' => $jobId, |
||
| 294 | ] |
||
| 295 | ), |
||
| 296 | __METHOD__ |
||
| 297 | ); |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Create an optimized SVG data uri |
||
| 302 | * See: https://codepen.io/tigt/post/optimizing-svgs-in-data-uris |
||
| 303 | * |
||
| 304 | * @param string $uri |
||
| 305 | * |
||
| 306 | * @return string |
||
| 307 | */ |
||
| 308 | public function encodeOptimizedSVGDataUri(string $uri): string |
||
| 309 | { |
||
| 310 | // First, uri encode everything |
||
| 311 | $uri = rawurlencode($uri); |
||
| 312 | $replacements = [ |
||
| 313 | // remove newlines |
||
| 314 | '/%0A/' => '', |
||
| 315 | // put spaces back in |
||
| 316 | '/%20/' => ' ', |
||
| 317 | // put equals signs back in |
||
| 318 | '/%3D/' => '=', |
||
| 319 | // put colons back in |
||
| 320 | '/%3A/' => ':', |
||
| 321 | // put slashes back in |
||
| 322 | '/%2F/' => '/', |
||
| 323 | // replace quotes with apostrophes (may break certain SVGs) |
||
| 324 | '/%22/' => "'", |
||
| 325 | ]; |
||
| 326 | foreach ($replacements as $pattern => $replacement) { |
||
| 327 | $uri = preg_replace($pattern, $replacement, $uri); |
||
| 328 | } |
||
| 329 | |||
| 330 | return $uri; |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param Asset $element |
||
| 335 | * @param OptimizedImage $model |
||
| 336 | * @param $aspectRatio |
||
| 337 | */ |
||
| 338 | protected function generatePlaceholders(Asset $element, OptimizedImage $model, $aspectRatio) |
||
| 366 | } |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @param Asset $asset |
||
| 370 | * @param $variant |
||
| 371 | * @param $retinaSize |
||
| 372 | * |
||
| 373 | * @return array |
||
| 374 | */ |
||
| 375 | protected function getTransformFromVariant(Asset $asset, $variant, $retinaSize): array |
||
| 401 | } |
||
| 402 | |||
| 403 | /** |
||
| 404 | * @param Asset $asset |
||
| 405 | * @param OptimizedImage $model |
||
| 406 | * @param $transform |
||
| 407 | * @param $variant |
||
| 408 | * @param $aspectRatio |
||
| 409 | */ |
||
| 410 | protected function addVariantImageToModel(Asset $asset, OptimizedImage $model, $transform, $variant, $aspectRatio) |
||
| 456 |