refactor(audit) : update audit subscribers to use ConstructeurLinks

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Matthieu
2026-03-31 14:23:35 +02:00
parent d85272208a
commit a249a5b785
5 changed files with 62 additions and 70 deletions

View File

@@ -20,7 +20,6 @@ use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\PersistentCollection;
use Doctrine\ORM\UnitOfWork;
use Error;
use Symfony\Bundle\SecurityBundle\Security;
@@ -88,8 +87,7 @@ abstract class AbstractAuditSubscriber implements EventSubscriber
}
/**
* Whether this subscriber tracks constructeur collection changes.
* Override to return true for entities with a constructeurs ManyToMany.
* Whether this subscriber uses the complex onFlush path (collection + custom field tracking).
*/
protected function hasCollectionTracking(): bool
{
@@ -413,50 +411,16 @@ abstract class AbstractAuditSubscriber implements EventSubscriber
}
}
/**
* No-op: constructeurs are now tracked as ConstructeurLink entities (OneToMany),
* so Doctrine no longer fires collection update events for them.
*/
private function collectCollectionUpdate(
object $collection,
array &$pendingUpdates,
array &$pendingSnapshots,
array &$pendingEntities,
): void {
if (!$collection instanceof PersistentCollection) {
return;
}
$owner = $collection->getOwner();
if (null === $owner || !$this->supports($owner)) {
return;
}
$ownerId = (string) $owner->getId();
if ('' === $ownerId) {
return;
}
$mapping = $collection->getMapping();
$fieldName = $mapping['fieldName'] ?? null;
if ('constructeurs' !== $fieldName) {
return;
}
$before = $this->normalizeCollection($collection->getSnapshot());
$after = $this->normalizeCollection($collection->toArray());
if ($before === $after) {
return;
}
$diff = [
'constructeurIds' => [
'from' => $before,
'to' => $after,
],
];
$pendingUpdates[$ownerId] = $this->mergeDiffs($pendingUpdates[$ownerId] ?? [], $diff);
$pendingSnapshots[$ownerId] = $this->snapshotEntity($owner);
$pendingEntities[$ownerId] = $owner;
}
): void {}
private function collectCustomFieldValueChanges(
UnitOfWork $uow,

View File

@@ -79,14 +79,21 @@ final class ComposantAuditSubscriber extends AbstractAuditSubscriber
}
return [
'id' => $entity->getId(),
'name' => $this->safeGet($entity, 'getName'),
'reference' => $this->safeGet($entity, 'getReference'),
'description' => $this->safeGet($entity, 'getDescription'),
'prix' => $this->safeGet($entity, 'getPrix'),
'typeComposant' => $this->normalizeValue($this->safeGet($entity, 'getTypeComposant')),
'product' => $this->normalizeValue($this->safeGet($entity, 'getProduct')),
'constructeurIds' => $this->normalizeCollection($entity->getConstructeurs()),
'id' => $entity->getId(),
'name' => $this->safeGet($entity, 'getName'),
'reference' => $this->safeGet($entity, 'getReference'),
'description' => $this->safeGet($entity, 'getDescription'),
'prix' => $this->safeGet($entity, 'getPrix'),
'typeComposant' => $this->normalizeValue($this->safeGet($entity, 'getTypeComposant')),
'product' => $this->normalizeValue($this->safeGet($entity, 'getProduct')),
'constructeurIds' => array_map(
fn ($link) => [
'id' => $link->getConstructeur()->getId(),
'name' => $link->getConstructeur()->getName(),
'supplierReference' => $link->getSupplierReference(),
],
$entity->getConstructeurLinks()->toArray(),
),
'pieceSlots' => $pieceSlots,
'subcomponentSlots' => $subcomponentSlots,
'productSlots' => $productSlots,

View File

@@ -99,12 +99,19 @@ final class MachineAuditSubscriber extends AbstractAuditSubscriber
}
return [
'id' => $entity->getId(),
'name' => $this->safeGet($entity, 'getName'),
'reference' => $this->safeGet($entity, 'getReference'),
'prix' => $this->safeGet($entity, 'getPrix'),
'site' => $this->normalizeValue($this->safeGet($entity, 'getSite')),
'constructeurIds' => $this->normalizeCollection($entity->getConstructeurs()),
'id' => $entity->getId(),
'name' => $this->safeGet($entity, 'getName'),
'reference' => $this->safeGet($entity, 'getReference'),
'prix' => $this->safeGet($entity, 'getPrix'),
'site' => $this->normalizeValue($this->safeGet($entity, 'getSite')),
'constructeurIds' => array_map(
fn ($link) => [
'id' => $link->getConstructeur()->getId(),
'name' => $link->getConstructeur()->getName(),
'supplierReference' => $link->getSupplierReference(),
],
$entity->getConstructeurLinks()->toArray(),
),
'customFieldValues' => $customFieldValues,
'componentLinks' => $componentLinks,
'pieceLinks' => $pieceLinks,

View File

@@ -56,14 +56,21 @@ final class PieceAuditSubscriber extends AbstractAuditSubscriber
}
return [
'id' => $entity->getId(),
'name' => $this->safeGet($entity, 'getName'),
'reference' => $this->safeGet($entity, 'getReference'),
'description' => $this->safeGet($entity, 'getDescription'),
'prix' => $this->safeGet($entity, 'getPrix'),
'typePiece' => $this->normalizeValue($this->safeGet($entity, 'getTypePiece')),
'product' => $this->normalizeValue($this->safeGet($entity, 'getProduct')),
'constructeurIds' => $this->normalizeCollection($entity->getConstructeurs()),
'id' => $entity->getId(),
'name' => $this->safeGet($entity, 'getName'),
'reference' => $this->safeGet($entity, 'getReference'),
'description' => $this->safeGet($entity, 'getDescription'),
'prix' => $this->safeGet($entity, 'getPrix'),
'typePiece' => $this->normalizeValue($this->safeGet($entity, 'getTypePiece')),
'product' => $this->normalizeValue($this->safeGet($entity, 'getProduct')),
'constructeurIds' => array_map(
fn ($link) => [
'id' => $link->getConstructeur()->getId(),
'name' => $link->getConstructeur()->getName(),
'supplierReference' => $link->getSupplierReference(),
],
$entity->getConstructeurLinks()->toArray(),
),
'productSlots' => $productSlots,
'customFieldValues' => $customFieldValues,
'version' => $this->safeGet($entity, 'getVersion'),

View File

@@ -45,12 +45,19 @@ final class ProductAuditSubscriber extends AbstractAuditSubscriber
}
return [
'id' => $entity->getId(),
'name' => $this->safeGet($entity, 'getName'),
'reference' => $this->safeGet($entity, 'getReference'),
'supplierPrice' => $this->safeGet($entity, 'getSupplierPrice'),
'typeProduct' => $this->normalizeValue($this->safeGet($entity, 'getTypeProduct')),
'constructeurIds' => $this->normalizeCollection($entity->getConstructeurs()),
'id' => $entity->getId(),
'name' => $this->safeGet($entity, 'getName'),
'reference' => $this->safeGet($entity, 'getReference'),
'supplierPrice' => $this->safeGet($entity, 'getSupplierPrice'),
'typeProduct' => $this->normalizeValue($this->safeGet($entity, 'getTypeProduct')),
'constructeurIds' => array_map(
fn ($link) => [
'id' => $link->getConstructeur()->getId(),
'name' => $link->getConstructeur()->getName(),
'supplierReference' => $link->getSupplierReference(),
],
$entity->getConstructeurLinks()->toArray(),
),
'customFieldValues' => $customFieldValues,
'version' => $this->safeGet($entity, 'getVersion'),
];