| Total Complexity | 52 |
| Total Lines | 284 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like EventLoggerSubscriber 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 EventLoggerSubscriber, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 71 | class EventLoggerSubscriber implements EventSubscriber |
||
| 72 | { |
||
| 73 | /** |
||
| 74 | * @var array The given fields will not be saved, because they contain sensitive informations |
||
| 75 | */ |
||
| 76 | protected const FIELD_BLACKLIST = [ |
||
| 77 | User::class => ['password', 'need_pw_change', 'googleAuthenticatorSecret', 'backupCodes', 'trustedDeviceCookieVersion', 'pw_reset_token', 'backupCodesGenerationDate'], |
||
| 78 | ]; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var array If elements of the given class are deleted, a log for the given fields will be triggered |
||
| 82 | */ |
||
| 83 | protected const TRIGGER_ASSOCIATION_LOG_WHITELIST = [ |
||
| 84 | PartLot::class => ['part'], |
||
| 85 | Orderdetail::class => ['part'], |
||
| 86 | Pricedetail::class => ['orderdetail'], |
||
| 87 | Attachment::class => ['element'], |
||
| 88 | AbstractParameter::class => ['element'], |
||
| 89 | ]; |
||
| 90 | |||
| 91 | protected const MAX_STRING_LENGTH = 2000; |
||
| 92 | |||
| 93 | protected $logger; |
||
| 94 | protected $serializer; |
||
| 95 | protected $eventCommentHelper; |
||
| 96 | protected $eventUndoHelper; |
||
| 97 | protected $save_changed_fields; |
||
| 98 | protected $save_changed_data; |
||
| 99 | protected $save_removed_data; |
||
| 100 | protected $propertyAccessor; |
||
| 101 | |||
| 102 | public function __construct(EventLogger $logger, SerializerInterface $serializer, EventCommentHelper $commentHelper, |
||
| 115 | } |
||
| 116 | |||
| 117 | public function onFlush(OnFlushEventArgs $eventArgs): void |
||
| 118 | { |
||
| 119 | $em = $eventArgs->getEntityManager(); |
||
| 120 | $uow = $em->getUnitOfWork(); |
||
| 121 | |||
| 122 | /* |
||
| 123 | * Log changes and deletions of entites. |
||
| 124 | * We can not log persist here, because the entities do not have IDs yet... |
||
| 125 | */ |
||
| 126 | |||
| 127 | foreach ($uow->getScheduledEntityUpdates() as $entity) { |
||
| 128 | if ($this->validEntity($entity)) { |
||
| 129 | $this->logElementEdited($entity, $em); |
||
| 130 | } |
||
| 131 | } |
||
| 132 | |||
| 133 | foreach ($uow->getScheduledEntityDeletions() as $entity) { |
||
| 134 | if ($this->validEntity($entity)) { |
||
| 135 | $this->logElementDeleted($entity, $em); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | $uow->computeChangeSets(); |
||
| 140 | } |
||
| 141 | |||
| 142 | public function postPersist(LifecycleEventArgs $args): void |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | public function postFlush(PostFlushEventArgs $args): void |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Check if the given element class has restrictions to its fields. |
||
| 187 | * |
||
| 188 | * @return bool True if there are restrictions, and further checking is needed |
||
| 189 | */ |
||
| 190 | public function hasFieldRestrictions(AbstractDBElement $element): bool |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Checks if the field of the given element should be saved (if it is not blacklisted). |
||
| 203 | */ |
||
| 204 | public function shouldFieldBeSaved(AbstractDBElement $element, string $field_name): bool |
||
| 205 | { |
||
| 206 | foreach (static::FIELD_BLACKLIST as $class => $blacklist) { |
||
| 207 | if (is_a($element, $class) && in_array($field_name, $blacklist, true)) { |
||
| 208 | return false; |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | //By default allow every field. |
||
| 213 | return true; |
||
| 214 | } |
||
| 215 | |||
| 216 | public function getSubscribedEvents() |
||
| 217 | { |
||
| 218 | return[ |
||
| 219 | Events::onFlush, |
||
| 220 | Events::postPersist, |
||
| 221 | Events::postFlush, |
||
| 222 | ]; |
||
| 223 | } |
||
| 224 | |||
| 225 | protected function logElementDeleted(AbstractDBElement $entity, EntityManagerInterface $em): void |
||
| 226 | { |
||
| 227 | $log = new ElementDeletedLogEntry($entity); |
||
| 228 | //Add user comment to log entry |
||
| 229 | if ($this->eventCommentHelper->isMessageSet()) { |
||
| 230 | $log->setComment($this->eventCommentHelper->getMessage()); |
||
| 231 | } |
||
| 232 | if ($this->eventUndoHelper->isUndo()) { |
||
| 233 | $log->setUndoneEvent($this->eventUndoHelper->getUndoneEvent(), $this->eventUndoHelper->getMode()); |
||
| 234 | } |
||
| 235 | if ($this->save_removed_data) { |
||
| 236 | //The 4th param is important here, as we delete the element... |
||
| 237 | $this->saveChangeSet($entity, $log, $em, true); |
||
| 238 | } |
||
| 239 | $this->logger->log($log); |
||
| 240 | |||
| 241 | //Check if we have to log CollectionElementDeleted entries |
||
| 242 | if ($this->save_changed_data) { |
||
| 243 | $metadata = $em->getClassMetadata(get_class($entity)); |
||
| 244 | $mappings = $metadata->getAssociationMappings(); |
||
| 245 | //Check if class is whitelisted for CollectionElementDeleted entry |
||
| 246 | foreach (static::TRIGGER_ASSOCIATION_LOG_WHITELIST as $class => $whitelist) { |
||
| 247 | if (is_a($entity, $class)) { |
||
| 248 | //Check names |
||
| 249 | foreach ($mappings as $field => $mapping) { |
||
| 250 | if (in_array($field, $whitelist, true)) { |
||
| 251 | $changed = $this->propertyAccessor->getValue($entity, $field); |
||
| 252 | $log = new CollectionElementDeleted($changed, $mapping['inversedBy'], $entity); |
||
| 253 | if ($this->eventUndoHelper->isUndo()) { |
||
| 254 | $log->setUndoneEvent($this->eventUndoHelper->getUndoneEvent(), $this->eventUndoHelper->getMode()); |
||
| 255 | } |
||
| 256 | $this->logger->log($log); |
||
| 257 | } |
||
| 258 | } |
||
| 259 | } |
||
| 260 | } |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | protected function logElementEdited(AbstractDBElement $entity, EntityManagerInterface $em): void |
||
| 265 | { |
||
| 266 | $uow = $em->getUnitOfWork(); |
||
| 267 | |||
| 268 | /* We have to call that here again, so the foreign entity timestamps, that were changed in updateTimestamp |
||
| 269 | get persisted */ |
||
| 270 | $changeSet = $uow->getEntityChangeSet($entity); |
||
| 271 | |||
| 272 | //Skip log entry, if only the lastModified field has changed... |
||
| 273 | if (isset($changeSet['lastModified']) && count($changeSet)) { |
||
| 274 | return; |
||
| 275 | } |
||
| 276 | |||
| 277 | $log = new ElementEditedLogEntry($entity); |
||
| 278 | if ($this->save_changed_data) { |
||
| 279 | $this->saveChangeSet($entity, $log, $em); |
||
| 280 | } elseif ($this->save_changed_fields) { |
||
| 281 | $changed_fields = array_keys($uow->getEntityChangeSet($entity)); |
||
| 282 | //Remove lastModified field, as this is always changed (gives us no additional info) |
||
| 283 | $changed_fields = array_diff($changed_fields, ['lastModified']); |
||
| 284 | $log->setChangedFields($changed_fields); |
||
| 285 | } |
||
| 286 | //Add user comment to log entry |
||
| 287 | if ($this->eventCommentHelper->isMessageSet()) { |
||
| 288 | $log->setComment($this->eventCommentHelper->getMessage()); |
||
| 289 | } |
||
| 290 | if ($this->eventUndoHelper->isUndo()) { |
||
| 291 | $log->setUndoneEvent($this->eventUndoHelper->getUndoneEvent(), $this->eventUndoHelper->getMode()); |
||
| 292 | } |
||
| 293 | $this->logger->log($log); |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Filter out every forbidden field and return the cleaned array. |
||
| 298 | */ |
||
| 299 | protected function filterFieldRestrictions(AbstractDBElement $element, array $fields): array |
||
| 315 | } |
||
| 316 | |||
| 317 | protected function saveChangeSet(AbstractDBElement $entity, AbstractLogEntry $logEntry, EntityManagerInterface $em, bool $element_deleted = false): void |
||
| 318 | { |
||
| 319 | $uow = $em->getUnitOfWork(); |
||
| 320 | |||
| 321 | if (!$logEntry instanceof ElementEditedLogEntry && !$logEntry instanceof ElementDeletedLogEntry) { |
||
| 322 | throw new \InvalidArgumentException('$logEntry must be ElementEditedLogEntry or ElementDeletedLogEntry!'); |
||
| 323 | } |
||
| 324 | |||
| 325 | if ($element_deleted) { //If the element was deleted we can use getOriginalData to save its content |
||
| 326 | $old_data = $uow->getOriginalEntityData($entity); |
||
| 327 | } else { //Otherwise we have to get it from entity changeset |
||
| 328 | $changeSet = $uow->getEntityChangeSet($entity); |
||
| 329 | $old_data = array_combine(array_keys($changeSet), array_column($changeSet, 0)); |
||
| 330 | } |
||
| 331 | $old_data = $this->filterFieldRestrictions($entity, $old_data); |
||
| 332 | |||
| 333 | //Restrict length of string fields, to save memory... |
||
| 334 | $old_data = array_map( |
||
| 335 | static function ($value) { |
||
| 336 | if (is_string($value)) { |
||
| 337 | return mb_strimwidth($value, 0, self::MAX_STRING_LENGTH, '...'); |
||
| 338 | } |
||
| 339 | |||
| 340 | return $value; |
||
| 341 | }, $old_data); |
||
| 342 | |||
| 343 | $logEntry->setOldData($old_data); |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Check if the given entity can be logged. |
||
| 348 | * |
||
| 349 | * @return bool true, if the given entity can be logged |
||
| 350 | */ |
||
| 351 | protected function validEntity(object $entity): bool |
||
| 355 | } |
||
| 356 | } |
||
| 357 |