| Conditions | 12 |
| Paths | 1 |
| Total Lines | 71 |
| 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 |
||
| 170 | protected function addElementEventListeners() |
||
| 171 | { |
||
| 172 | // Invalidate flagged caches when elements are saved |
||
| 173 | Event::on( |
||
| 174 | Elements::class, |
||
| 175 | Elements::EVENT_AFTER_SAVE_ELEMENT, |
||
| 176 | function (ElementEvent $event) { |
||
| 177 | $element = $event->element; |
||
| 178 | if (!$element || ElementHelper::isDraftOrRevision($element)) { |
||
| 179 | return; |
||
| 180 | } |
||
| 181 | CacheFlag::$plugin->cacheFlag->invalidateFlaggedCachesByElement($element); |
||
| 182 | } |
||
| 183 | ); |
||
| 184 | |||
| 185 | // Invalidate flagged caches when elements are deleted |
||
| 186 | Event::on( |
||
| 187 | Elements::class, |
||
| 188 | Elements::EVENT_BEFORE_DELETE_ELEMENT, |
||
| 189 | function (ElementEvent $event) { |
||
| 190 | $element = $event->element; |
||
| 191 | if (!$element || ElementHelper::isDraftOrRevision($element)) { |
||
| 192 | return; |
||
| 193 | } |
||
| 194 | CacheFlag::$plugin->cacheFlag->invalidateFlaggedCachesByElement($element); |
||
| 195 | } |
||
| 196 | ); |
||
| 197 | |||
| 198 | // Invalidate flagged caches when structure entries are moved |
||
| 199 | Event::on( |
||
| 200 | Structures::class, |
||
| 201 | Structures::EVENT_AFTER_MOVE_ELEMENT, |
||
| 202 | function (MoveElementEvent $event) { |
||
| 203 | $element = $event->element; |
||
| 204 | if (!$element || ElementHelper::isDraftOrRevision($element)) { |
||
| 205 | return; |
||
| 206 | } |
||
| 207 | CacheFlag::$plugin->cacheFlag->invalidateFlaggedCachesByElement($element); |
||
| 208 | } |
||
| 209 | ); |
||
| 210 | |||
| 211 | // Invalidate flagged caches when elements change status |
||
| 212 | Event::on( |
||
| 213 | Elements::class, |
||
| 214 | Elements::EVENT_AFTER_PERFORM_ACTION, |
||
| 215 | function (ElementActionEvent $event) { |
||
| 216 | |||
| 217 | /* @var ElementQueryInterface|null $criteria */ |
||
| 218 | $criteria = $event->criteria; |
||
| 219 | |||
| 220 | if (!$criteria) { |
||
| 221 | return; |
||
| 222 | } |
||
| 223 | |||
| 224 | /* @var ElementActionInterface|null $action */ |
||
| 225 | $action = $event->action; |
||
| 226 | |||
| 227 | if (!$action || !\in_array(\get_class($action), [ |
||
| 228 | SetStatus::class, |
||
| 229 | ])) { |
||
| 230 | return; |
||
| 231 | } |
||
| 232 | |||
| 233 | /** @var ElementInterface[] $elements */ |
||
| 234 | $elements = $criteria->all(); |
||
| 235 | |||
| 236 | foreach ($elements as $element) { |
||
| 237 | if (ElementHelper::isDraftOrRevision($element)) { |
||
| 238 | continue; |
||
| 239 | } |
||
| 240 | CacheFlag::$plugin->cacheFlag->invalidateFlaggedCachesByElement($element); |
||
| 241 | } |
||
| 247 |