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\EntityManagerInterface;
use Doctrine\ORM\Event\OnFlushEventArgs; use Doctrine\ORM\Event\OnFlushEventArgs;
use Doctrine\ORM\Events; use Doctrine\ORM\Events;
use Doctrine\ORM\PersistentCollection;
use Doctrine\ORM\UnitOfWork; use Doctrine\ORM\UnitOfWork;
use Error; use Error;
use Symfony\Bundle\SecurityBundle\Security; use Symfony\Bundle\SecurityBundle\Security;
@@ -88,8 +87,7 @@ abstract class AbstractAuditSubscriber implements EventSubscriber
} }
/** /**
* Whether this subscriber tracks constructeur collection changes. * Whether this subscriber uses the complex onFlush path (collection + custom field tracking).
* Override to return true for entities with a constructeurs ManyToMany.
*/ */
protected function hasCollectionTracking(): bool 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( private function collectCollectionUpdate(
object $collection, object $collection,
array &$pendingUpdates, array &$pendingUpdates,
array &$pendingSnapshots, array &$pendingSnapshots,
array &$pendingEntities, array &$pendingEntities,
): void { ): 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;
}
private function collectCustomFieldValueChanges( private function collectCustomFieldValueChanges(
UnitOfWork $uow, UnitOfWork $uow,

View File

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

View File

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

View File

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

View File

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