entityManager->getConnection(); if (!$conn->isTransactionActive()) { $conn->beginTransaction(); } try { // Lock the StoredObject until the end of the process $storedObject = $this->entityManager->find(StoredObject::class, $message->storedObjectId); if (!$storedObject instanceof StoredObject) { // Nothing to do if stored object does not exist anymore if ($conn->isTransactionActive()) { $conn->commit(); } return; } $this->entityManager->lock($storedObject, LockMode::PESSIMISTIC_WRITE); // Build criteria expected by AuditTrailRepository $criteria = []; if (null !== $message->from) { $criteria['from_date'] = $message->from; } if (null !== $message->to) { $criteria['to_date'] = $message->to; } $subjects = []; foreach ($message->subjects as $s) { if (is_array($s)) { $subjects[] = Subject::fromArray($s); } } if ([] !== $subjects) { $criteria['subjects'] = $subjects; } $byUsers = []; foreach ($message->byUsers as $userId) { $byUsers[] = $this->userRepository->find($userId); } if ([] !== $byUsers) { $criteria['by_users'] = $byUsers; } $this->auditEventDumper->dump($criteria, $message->lang, $storedObject); $this->entityManager->flush(); if ($conn->isTransactionActive()) { $conn->commit(); } } catch (AuditDumpTooMuchLines|AuditDumpAlreadyGeneratedException $e) { if ($conn->isTransactionActive()) { $this->entityManager->flush(); $conn->commit(); } throw new UnrecoverableMessageHandlingException(previous: $e); } catch (\Throwable $e) { if ($conn->isTransactionActive()) { $conn->rollBack(); } throw $e; } finally { // Clear the EntityManager state at the end $this->entityManager->clear(); } } }