mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2026-03-18 03:38:05 +00:00
- Removed unused `StoredObjectRepository` dependency and improved transaction management by replacing `beganTx` flag with `isTransactionActive` checks. - Adjusted entity locking logic to separate `find()` and `lock()` calls for `StoredObject`. - Simplified `AuditEventDumper` by removing redundant assertions and unused `AuditTrail` import. - Standardized subject display configuration in `AuditEventDumper` and ensured consistent data formatting.
106 lines
3.5 KiB
PHP
106 lines
3.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/*
|
|
* Chill is a software for social workers
|
|
*
|
|
* For the full copyright and license information, please view
|
|
* the LICENSE file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Chill\MainBundle\Audit\Messenger;
|
|
|
|
use Chill\DocStoreBundle\Entity\StoredObject;
|
|
use Chill\MainBundle\Audit\AuditEventDumper;
|
|
use Chill\MainBundle\Audit\Exception\AuditDumpAlreadyGeneratedException;
|
|
use Chill\MainBundle\Audit\Exception\AuditDumpTooMuchLines;
|
|
use Chill\MainBundle\Audit\Subject;
|
|
use Chill\MainBundle\Repository\UserRepositoryInterface;
|
|
use Doctrine\DBAL\LockMode;
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
use Symfony\Component\Messenger\Exception\UnrecoverableMessageHandlingException;
|
|
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;
|
|
|
|
final readonly class AuditDumpRequestHandler implements MessageHandlerInterface
|
|
{
|
|
public function __construct(
|
|
private AuditEventDumper $auditEventDumper,
|
|
private EntityManagerInterface $entityManager,
|
|
private UserRepositoryInterface $userRepository,
|
|
) {}
|
|
|
|
public function __invoke(AuditDumpRequestMessage $message): void
|
|
{
|
|
$conn = $this->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();
|
|
}
|
|
}
|
|
}
|