Total Complexity | 53 |
Total Lines | 291 |
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 |
||
171 | { |
||
172 | $em = $args->getEntityManager(); |
||
173 | $uow = $em->getUnitOfWork(); |
||
174 | // If the we have added any ElementCreatedLogEntries added in postPersist, we flush them here. |
||
175 | $uow->computeChangeSets(); |
||
176 | if ($uow->hasPendingInsertions() || !empty($uow->getScheduledEntityUpdates())) { |
||
177 | $em->flush(); |
||
178 | } |
||
179 | |||
180 | //Clear the message provided by user. |
||
181 | $this->eventCommentHelper->clearMessage(); |
||
182 | $this->eventUndoHelper->clearUndoneEvent(); |
||
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 |
||
191 | { |
||
192 | foreach (array_keys(static::FIELD_BLACKLIST) as $class) { |
||
193 | if (is_a($element, $class)) { |
||
194 | return true; |
||
195 | } |
||
196 | } |
||
197 | |||
198 | return false; |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * Checks if the field of the given element should be saved (if it is not blacklisted). |
||
203 | * |
||
204 | * @return bool |
||
205 | */ |
||
206 | public function shouldFieldBeSaved(AbstractDBElement $element, string $field_name): bool |
||
207 | { |
||
208 | foreach (static::FIELD_BLACKLIST as $class => $blacklist) { |
||
209 | if (is_a($element, $class) && in_array($field_name, $blacklist, true)) { |
||
210 | return false; |
||
211 | } |
||
212 | } |
||
213 | |||
214 | //By default allow every field. |
||
215 | return true; |
||
216 | } |
||
217 | |||
218 | public function getSubscribedEvents() |
||
219 | { |
||
220 | return[ |
||
221 | Events::onFlush, |
||
222 | Events::postPersist, |
||
223 | Events::postFlush, |
||
224 | ]; |
||
225 | } |
||
226 | |||
227 | protected function logElementDeleted(AbstractDBElement $entity, EntityManagerInterface $em): void |
||
228 | { |
||
229 | $log = new ElementDeletedLogEntry($entity); |
||
230 | //Add user comment to log entry |
||
231 | if ($this->eventCommentHelper->isMessageSet()) { |
||
232 | $log->setComment($this->eventCommentHelper->getMessage()); |
||
233 | } |
||
234 | if ($this->eventUndoHelper->isUndo()) { |
||
235 | $log->setUndoneEvent($this->eventUndoHelper->getUndoneEvent(), $this->eventUndoHelper->getMode()); |
||
236 | } |
||
237 | if ($this->save_removed_data) { |
||
238 | //The 4th param is important here, as we delete the element... |
||
239 | $this->saveChangeSet($entity, $log, $em, true); |
||
240 | } |
||
241 | $this->logger->log($log); |
||
242 | |||
243 | //Check if we have to log CollectionElementDeleted entries |
||
244 | if ($this->save_changed_data) { |
||
245 | $metadata = $em->getClassMetadata(get_class($entity)); |
||
246 | $mappings = $metadata->getAssociationMappings(); |
||
247 | //Check if class is whitelisted for CollectionElementDeleted entry |
||
248 | foreach (static::TRIGGER_ASSOCIATION_LOG_WHITELIST as $class => $whitelist) { |
||
249 | if (is_a($entity, $class)) { |
||
250 | //Check names |
||
251 | foreach ($mappings as $field => $mapping) { |
||
252 | if (in_array($field, $whitelist, true)) { |
||
253 | $changed = $this->propertyAccessor->getValue($entity, $field); |
||
254 | $log = new CollectionElementDeleted($changed, $mapping['inversedBy'], $entity); |
||
255 | if ($this->eventUndoHelper->isUndo()) { |
||
256 | $log->setUndoneEvent($this->eventUndoHelper->getUndoneEvent(), $this->eventUndoHelper->getMode()); |
||
257 | } |
||
258 | $this->logger->log($log); |
||
259 | } |
||
260 | } |
||
261 | } |
||
262 | } |
||
263 | } |
||
264 | } |
||
265 | |||
266 | protected function logElementEdited(AbstractDBElement $entity, EntityManagerInterface $em): void |
||
267 | { |
||
268 | $uow = $em->getUnitOfWork(); |
||
269 | |||
270 | /* We have to call that here again, so the foreign entity timestamps, that were changed in updateTimestamp |
||
271 | get persisted */ |
||
272 | $changeSet = $uow->getEntityChangeSet($entity); |
||
273 | |||
274 | //Skip log entry, if only the lastModified field has changed... |
||
275 | if (isset($changeSet['lastModified']) && count($changeSet)) { |
||
276 | return; |
||
277 | } |
||
278 | |||
279 | $log = new ElementEditedLogEntry($entity); |
||
280 | if ($this->save_changed_data) { |
||
281 | $this->saveChangeSet($entity, $log, $em); |
||
282 | } elseif ($this->save_changed_fields) { |
||
283 | $changed_fields = array_keys($uow->getEntityChangeSet($entity)); |
||
284 | //Remove lastModified field, as this is always changed (gives us no additional info) |
||
285 | $changed_fields = array_diff($changed_fields, ['lastModified']); |
||
286 | $log->setChangedFields($changed_fields); |
||
287 | } |
||
288 | //Add user comment to log entry |
||
289 | if ($this->eventCommentHelper->isMessageSet()) { |
||
290 | $log->setComment($this->eventCommentHelper->getMessage()); |
||
291 | } |
||
292 | if ($this->eventUndoHelper->isUndo()) { |
||
293 | $log->setUndoneEvent($this->eventUndoHelper->getUndoneEvent(), $this->eventUndoHelper->getMode()); |
||
294 | } |
||
295 | $this->logger->log($log); |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Filter out every forbidden field and return the cleaned array. |
||
300 | * |
||
301 | * @return array |
||
302 | */ |
||
303 | protected function filterFieldRestrictions(AbstractDBElement $element, array $fields): array |
||
304 | { |
||
305 | unset($fields['lastModified']); |
||
306 | |||
307 | if (! $this->hasFieldRestrictions($element)) { |
||
308 | return $fields; |
||
309 | } |
||
310 | |||
311 | return array_filter($fields, function ($value, $key) use ($element) { |
||
312 | //Associative array (save changed data) case |
||
313 | if (is_string($key)) { |
||
314 | return $this->shouldFieldBeSaved($element, $key); |
||
315 | } |
||
316 | |||
317 | return $this->shouldFieldBeSaved($element, $value); |
||
318 | }, ARRAY_FILTER_USE_BOTH); |
||
319 | } |
||
320 | |||
321 | protected function saveChangeSet(AbstractDBElement $entity, AbstractLogEntry $logEntry, EntityManagerInterface $em, bool $element_deleted = false): void |
||
322 | { |
||
323 | $uow = $em->getUnitOfWork(); |
||
324 | |||
325 | if (! $logEntry instanceof ElementEditedLogEntry && ! $logEntry instanceof ElementDeletedLogEntry) { |
||
326 | throw new \InvalidArgumentException('$logEntry must be ElementEditedLogEntry or ElementDeletedLogEntry!'); |
||
327 | } |
||
328 | |||
329 | if ($element_deleted) { //If the element was deleted we can use getOriginalData to save its content |
||
330 | $old_data = $uow->getOriginalEntityData($entity); |
||
331 | } else { //Otherwise we have to get it from entity changeset |
||
332 | $changeSet = $uow->getEntityChangeSet($entity); |
||
333 | $old_data = array_combine(array_keys($changeSet), array_column($changeSet, 0)); |
||
334 | } |
||
335 | $old_data = $this->filterFieldRestrictions($entity, $old_data); |
||
336 | |||
337 | //Restrict length of string fields, to save memory... |
||
338 | $old_data = array_map(function ($value) { |
||
339 | if (is_string($value)) { |
||
340 | return mb_strimwidth($value, 0, self::MAX_STRING_LENGTH, '...'); |
||
341 | } |
||
342 | |||
343 | return $value; |
||
344 | }, $old_data); |
||
345 | |||
346 | $logEntry->setOldData($old_data); |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * Check if the given entity can be logged. |
||
351 | * |
||
352 | * @return bool True, if the given entity can be logged. |
||
353 | */ |
||
354 | protected function validEntity(object $entity): bool |
||
362 | } |
||
363 | } |
||
364 |