Total Complexity | 50 |
Total Lines | 280 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 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 |
||
49 | class EventLoggerSubscriber implements EventSubscriber |
||
50 | { |
||
51 | /** @var array The given fields will not be saved, because they contain sensitive informations */ |
||
52 | protected const FIELD_BLACKLIST = [ |
||
53 | User::class => ['password', 'need_pw_change', 'googleAuthenticatorSecret', 'backupCodes', 'trustedDeviceCookieVersion', 'pw_reset_token', 'backupCodesGenerationDate'], |
||
54 | ]; |
||
55 | |||
56 | /** @var array If elements of the given class are deleted, a log for the given fields will be triggered */ |
||
57 | protected const TRIGGER_ASSOCIATION_LOG_WHITELIST = [ |
||
58 | PartLot::class => ['part'], |
||
59 | Orderdetail::class => ['part'], |
||
60 | Pricedetail::class => ['orderdetail'], |
||
61 | Attachment::class => ['element'], |
||
62 | ]; |
||
63 | |||
64 | protected const MAX_STRING_LENGTH = 2000; |
||
65 | |||
66 | protected $logger; |
||
67 | protected $serializer; |
||
68 | protected $eventCommentHelper; |
||
69 | protected $eventUndoHelper; |
||
70 | protected $save_changed_fields; |
||
71 | protected $save_changed_data; |
||
72 | protected $save_removed_data; |
||
73 | protected $propertyAccessor; |
||
74 | |||
75 | public function __construct(EventLogger $logger, SerializerInterface $serializer, EventCommentHelper $commentHelper, |
||
76 | bool $save_changed_fields, bool $save_changed_data, bool $save_removed_data, PropertyAccessorInterface $propertyAccessor, |
||
77 | EventUndoHelper $eventUndoHelper) |
||
78 | { |
||
79 | $this->logger = $logger; |
||
80 | $this->serializer = $serializer; |
||
81 | $this->eventCommentHelper = $commentHelper; |
||
82 | $this->propertyAccessor = $propertyAccessor; |
||
83 | $this->eventUndoHelper = $eventUndoHelper; |
||
84 | |||
85 | $this->save_changed_fields = $save_changed_fields; |
||
86 | $this->save_changed_data = $save_changed_data; |
||
87 | $this->save_removed_data = $save_removed_data; |
||
88 | } |
||
89 | |||
90 | public function onFlush(OnFlushEventArgs $eventArgs) |
||
91 | { |
||
92 | $em = $eventArgs->getEntityManager(); |
||
93 | $uow = $em->getUnitOfWork(); |
||
94 | |||
95 | /* |
||
96 | * Log changes and deletions of entites. |
||
97 | * We can not log persist here, because the entities do not have IDs yet... |
||
98 | */ |
||
99 | |||
100 | foreach ($uow->getScheduledEntityUpdates() as $entity) { |
||
101 | if ($this->validEntity($entity)) { |
||
102 | $this->logElementEdited($entity, $em); |
||
103 | } |
||
104 | } |
||
105 | |||
106 | foreach ($uow->getScheduledEntityDeletions() as $entity) { |
||
107 | if ($this->validEntity($entity)) { |
||
108 | $this->logElementDeleted($entity, $em); |
||
109 | } |
||
110 | } |
||
111 | |||
112 | $uow->computeChangeSets(); |
||
113 | } |
||
114 | |||
115 | public function postPersist(LifecycleEventArgs $args) |
||
116 | { |
||
117 | //Create an log entry, we have to do this post persist, cause we have to know the ID |
||
118 | |||
119 | /** @var AbstractDBElement $entity */ |
||
120 | $entity = $args->getObject(); |
||
121 | if ($this->validEntity($entity)) { |
||
122 | $log = new ElementCreatedLogEntry($entity); |
||
123 | //Add user comment to log entry |
||
124 | if ($this->eventCommentHelper->isMessageSet()) { |
||
125 | $log->setComment($this->eventCommentHelper->getMessage()); |
||
126 | } |
||
127 | if ($this->eventUndoHelper->isUndo()) { |
||
128 | $undoEvent = $this->eventUndoHelper->getUndoneEvent(); |
||
129 | |||
130 | $log->setUndoneEvent($undoEvent, $this->eventUndoHelper->getMode()); |
||
131 | |||
132 | if($undoEvent instanceof ElementDeletedLogEntry && $undoEvent->getTargetClass() === $log->getTargetClass()) { |
||
133 | $log->setTargetElementID($undoEvent->getTargetID()); |
||
|
|||
134 | } |
||
135 | if($undoEvent instanceof CollectionElementDeleted && $undoEvent->getDeletedElementClass() === $log->getTargetClass()) { |
||
136 | $log->setTargetElementID($undoEvent->getDeletedElementID()); |
||
137 | } |
||
138 | } |
||
139 | $this->logger->log($log); |
||
140 | } |
||
141 | } |
||
142 | |||
143 | public function postFlush(PostFlushEventArgs $args) |
||
144 | { |
||
145 | $em = $args->getEntityManager(); |
||
146 | $uow = $em->getUnitOfWork(); |
||
147 | // If the we have added any ElementCreatedLogEntries added in postPersist, we flush them here. |
||
148 | if ($uow->hasPendingInsertions()) { |
||
149 | $em->flush(); |
||
150 | } |
||
151 | |||
152 | //Clear the message provided by user. |
||
153 | $this->eventCommentHelper->clearMessage(); |
||
154 | $this->eventUndoHelper->clearUndoneEvent(); |
||
155 | } |
||
156 | |||
157 | protected function logElementDeleted(AbstractDBElement $entity, EntityManagerInterface $em): void |
||
189 | } |
||
190 | } |
||
191 | } |
||
192 | } |
||
193 | } |
||
194 | } |
||
195 | |||
196 | protected function logElementEdited(AbstractDBElement $entity, EntityManagerInterface $em): void |
||
197 | { |
||
198 | $uow = $em->getUnitOfWork(); |
||
199 | |||
200 | $log = new ElementEditedLogEntry($entity); |
||
201 | if ($this->save_changed_data) { |
||
202 | $this->saveChangeSet($entity, $log, $em); |
||
203 | } elseif ($this->save_changed_fields) { |
||
204 | $changed_fields = array_keys($uow->getEntityChangeSet($entity)); |
||
205 | //Remove lastModified field, as this is always changed (gives us no additional info) |
||
206 | $changed_fields = array_diff($changed_fields, ['lastModified']); |
||
207 | $log->setChangedFields($changed_fields); |
||
208 | } |
||
209 | //Add user comment to log entry |
||
210 | if ($this->eventCommentHelper->isMessageSet()) { |
||
211 | $log->setComment($this->eventCommentHelper->getMessage()); |
||
212 | } |
||
213 | if ($this->eventUndoHelper->isUndo()) { |
||
214 | $log->setUndoneEvent($this->eventUndoHelper->getUndoneEvent(), $this->eventUndoHelper->getMode()); |
||
215 | } |
||
216 | $this->logger->log($log); |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Check if the given element class has restrictions to its fields |
||
221 | * @param AbstractDBElement $element |
||
222 | * @return bool True if there are restrictions, and further checking is needed |
||
223 | */ |
||
224 | public function hasFieldRestrictions(AbstractDBElement $element): bool |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Filter out every forbidden field and return the cleaned array. |
||
237 | * @param AbstractDBElement $element |
||
238 | * @param array $fields |
||
239 | * @return array |
||
240 | */ |
||
241 | protected function filterFieldRestrictions(AbstractDBElement $element, array $fields): array |
||
242 | { |
||
243 | unset($fields['lastModified']); |
||
244 | |||
245 | if (!$this->hasFieldRestrictions($element)) { |
||
246 | return $fields; |
||
247 | } |
||
248 | |||
249 | return array_filter($fields, function ($value, $key) use ($element) { |
||
250 | //Associative array (save changed data) case |
||
251 | if (is_string($key)) { |
||
252 | return $this->shouldFieldBeSaved($element, $key); |
||
253 | } |
||
254 | |||
255 | return $this->shouldFieldBeSaved($element, $value); |
||
256 | }, ARRAY_FILTER_USE_BOTH); |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * Checks if the field of the given element should be saved (if it is not blacklisted). |
||
261 | * @param AbstractDBElement $element |
||
262 | * @param string $field_name |
||
263 | * @return bool |
||
264 | */ |
||
265 | public function shouldFieldBeSaved(AbstractDBElement $element, string $field_name): bool |
||
266 | { |
||
267 | foreach (static::FIELD_BLACKLIST as $class => $blacklist) { |
||
268 | if (is_a($element, $class) && in_array($field_name, $blacklist)) { |
||
269 | return false; |
||
270 | } |
||
271 | } |
||
272 | |||
273 | //By default allow every field. |
||
274 | return true; |
||
275 | } |
||
276 | |||
277 | protected function saveChangeSet(AbstractDBElement $entity, AbstractLogEntry $logEntry, EntityManagerInterface $em, $element_deleted = false): void |
||
278 | { |
||
279 | $uow = $em->getUnitOfWork(); |
||
280 | |||
281 | if (!$logEntry instanceof ElementEditedLogEntry && !$logEntry instanceof ElementDeletedLogEntry) { |
||
282 | throw new \InvalidArgumentException('$logEntry must be ElementEditedLogEntry or ElementDeletedLogEntry!'); |
||
283 | } |
||
284 | |||
285 | if ($element_deleted) { //If the element was deleted we can use getOriginalData to save its content |
||
286 | $old_data = $uow->getOriginalEntityData($entity); |
||
287 | } else { //Otherwise we have to get it from entity changeset |
||
288 | $changeSet = $uow->getEntityChangeSet($entity); |
||
289 | $old_data = array_combine(array_keys($changeSet), array_column($changeSet, 0)); |
||
290 | } |
||
291 | $old_data = $this->filterFieldRestrictions($entity, $old_data); |
||
292 | |||
293 | //Restrict length of string fields, to save memory... |
||
294 | $old_data = array_map(function ($value) { |
||
295 | if (is_string($value)) { |
||
296 | return mb_strimwidth($value, 0, self::MAX_STRING_LENGTH, '...'); |
||
297 | } |
||
298 | |||
299 | return $value; |
||
300 | }, $old_data); |
||
301 | |||
302 | $logEntry->setOldData($old_data); |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * Check if the given entity can be logged. |
||
307 | * @param object $entity |
||
308 | * @return bool True, if the given entity can be logged. |
||
309 | */ |
||
310 | protected function validEntity(object $entity): bool |
||
311 | { |
||
312 | //Dont log logentries itself! |
||
313 | if ($entity instanceof AbstractDBElement && !$entity instanceof AbstractLogEntry) { |
||
314 | return true; |
||
315 | } |
||
316 | |||
317 | return false; |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * @inheritDoc |
||
322 | */ |
||
323 | public function getSubscribedEvents() |
||
329 | ]; |
||
330 | } |
||
331 | } |