Conditions | 12 |
Paths | 1 |
Total Lines | 67 |
Code Lines | 40 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
125 | if (!$element || ElementHelper::isDraftOrRevision($element)) { |
||
126 | return; |
||
127 | } |
||
128 | CacheFlag::$plugin->cacheFlag->invalidateFlaggedCachesByElement($element); |
||
129 | } |
||
130 | ); |
||
131 | |||
132 | // Invalidate flagged caches when elements change status |
||
133 | Event::on( |
||
134 | Elements::class, |
||
135 | Elements::EVENT_AFTER_PERFORM_ACTION, |
||
136 | function (ElementActionEvent $event) { |
||
137 | |||
138 | /* @var ElementQueryInterface|null $criteria */ |
||
139 | $criteria = $event->criteria; |
||
140 | |||
141 | if (!$criteria) { |
||
142 | return; |
||
143 | } |
||
144 | |||
145 | /* @var ElementActionInterface|null $action */ |
||
146 | $action = $event->action; |
||
147 | |||
148 | if (!$action || !\in_array(\get_class($action), [ |
||
149 | SetStatus::class, |
||
150 | ])) { |
||
151 | return; |
||
152 | } |
||
153 | |||
154 | /** @var ElementInterface[] $elements */ |
||
155 | $elements = $criteria->all(); |
||
156 | |||
157 | foreach ($elements as $element) { |
||
158 | if (ElementHelper::isDraftOrRevision($element)) { |
||
159 | continue; |
||
160 | } |
||
161 | CacheFlag::$plugin->cacheFlag->invalidateFlaggedCachesByElement($element); |
||
162 | } |
||
163 | } |
||
164 | ); |
||
165 | |||
166 | // Add option to the Clear Caches utility to invalidate all flagged caches |
||
167 | Event::on(ClearCaches::class, ClearCaches::EVENT_REGISTER_CACHE_OPTIONS, |
||
168 | static function (RegisterCacheOptionsEvent $event) { |
||
169 | $event->options[] = [ |
||
170 | 'key' => 'cacheflag-flagged-caches', |
||
171 | 'label' => Craft::t('cache-flag', 'Flagged template caches'), |
||
172 | 'action' => [CacheFlag::getInstance()->cacheFlag, 'invalidateAllFlaggedCaches'], |
||
173 | 'info' => Craft::t('cache-flag', 'All template caches flagged using Cache Flag'), |
||
174 | ]; |
||
175 | } |
||
176 | ); |
||
177 | |||
178 | // Register CP variable |
||
179 | Event::on( |
||
180 | CraftVariable::class, |
||
181 | CraftVariable::EVENT_INIT, |
||
182 | function (Event $event) { |
||
183 | /** @var CraftVariable $variable */ |
||
184 | $variable = $event->sender; |
||
185 | $variable->set('cacheFlagCp', CpVariable::class); |
||
186 | } |
||
187 | ); |
||
188 | |||
189 | // Support Project Config rebuild |
||
190 | Event::on( |
||
191 | ProjectConfig::class, |
||
192 | ProjectConfig::EVENT_REBUILD, |
||
223 |