| Conditions | 15 |
| Paths | 28 |
| Total Lines | 66 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 66 | public function execute($queue): void |
||
| 67 | { |
||
| 68 | // Let's save ourselves some trouble and just clear all the caches for this element class |
||
| 69 | Craft::$app->getElements()->invalidateCachesForElementType(Asset::class); |
||
| 70 | |||
| 71 | // Now find the affected element IDs |
||
| 72 | /** @var ElementQuery $query */ |
||
| 73 | $query = Asset::find(); |
||
| 74 | if (!empty($this->criteria)) { |
||
| 75 | Craft::configure($query, $this->criteria); |
||
| 76 | } |
||
| 77 | if (Craft::$app instanceof ConsoleApplication) { |
||
| 78 | echo $this->description . PHP_EOL; |
||
| 79 | } |
||
| 80 | // Use craft\db\Paginator to paginate the results so we don't exceed any memory limits |
||
| 81 | // See batch() and each() discussion here: https://github.com/yiisoft/yii2/issues/8420 |
||
| 82 | // and here: https://github.com/craftcms/cms/issues/7338 |
||
| 83 | $paginator = new Paginator($query, [ |
||
| 84 | 'pageSize' => self::ASSET_QUERY_PAGE_SIZE, |
||
| 85 | ]); |
||
| 86 | $currentElement = 0; |
||
| 87 | $totalElements = $paginator->getTotalResults(); |
||
| 88 | // Iterate through the paginated results |
||
| 89 | while ($currentElement < $totalElements) { |
||
| 90 | $elements = $paginator->getPageResults(); |
||
| 91 | if (Craft::$app instanceof ConsoleApplication) { |
||
| 92 | echo 'Query ' . $paginator->getCurrentPage() . '/' . $paginator->getTotalPages() |
||
| 93 | . ' - assets: ' . $paginator->getTotalResults() |
||
| 94 | . PHP_EOL; |
||
| 95 | } |
||
| 96 | /** @var ElementInterface $element */ |
||
| 97 | foreach ($elements as $element) { |
||
| 98 | $currentElement++; |
||
| 99 | // Find each OptimizedImages field and process it |
||
| 100 | $layout = $element->getFieldLayout(); |
||
| 101 | if ($layout !== null) { |
||
| 102 | $fields = $layout->getCustomFields(); |
||
| 103 | /** @var Field $field */ |
||
| 104 | foreach ($fields as $field) { |
||
| 105 | if ($field instanceof OptimizedImagesField && $element instanceof Asset) { |
||
| 106 | if ($this->fieldId === null || (int)$field->id === (int)$this->fieldId) { |
||
| 107 | if (Craft::$app instanceof ConsoleApplication) { |
||
| 108 | echo $currentElement . '/' . $totalElements |
||
| 109 | . ' - processing asset: ' . $element->title |
||
| 110 | . ' from field: ' . $field->name . PHP_EOL; |
||
| 111 | } |
||
| 112 | try { |
||
| 113 | ImageOptimize::$plugin->optimizedImages->updateOptimizedImageFieldData($field, $element, $this->force); |
||
| 114 | } catch (Exception $e) { |
||
| 115 | Craft::error($e->getMessage(), __METHOD__); |
||
| 116 | if (Craft::$app instanceof ConsoleApplication) { |
||
| 117 | echo '[error]: ' |
||
| 118 | . $e->getMessage() |
||
| 119 | . ' while processing ' |
||
| 120 | . $currentElement . '/' . $totalElements |
||
| 121 | . ' - processing asset: ' . $element->title |
||
| 122 | . ' from field: ' . $field->name . PHP_EOL; |
||
| 123 | } |
||
| 124 | } |
||
| 125 | } |
||
| 126 | } |
||
| 127 | } |
||
| 128 | } |
||
| 129 | $this->setProgress($queue, $currentElement / $totalElements); |
||
| 130 | } |
||
| 131 | $paginator->currentPage++; |
||
| 132 | } |
||
| 148 |