refactor(versioning) : update EntityVersionService to use ConstructeurLinks
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -6,16 +6,20 @@ namespace App\Service;
|
||||
|
||||
use App\Entity\AuditLog;
|
||||
use App\Entity\Composant;
|
||||
use App\Entity\ComposantConstructeurLink;
|
||||
use App\Entity\ComposantPieceSlot;
|
||||
use App\Entity\ComposantProductSlot;
|
||||
use App\Entity\ComposantSubcomponentSlot;
|
||||
use App\Entity\Machine;
|
||||
use App\Entity\MachineComponentLink;
|
||||
use App\Entity\MachineConstructeurLink;
|
||||
use App\Entity\MachinePieceLink;
|
||||
use App\Entity\MachineProductLink;
|
||||
use App\Entity\Piece;
|
||||
use App\Entity\PieceConstructeurLink;
|
||||
use App\Entity\PieceProductSlot;
|
||||
use App\Entity\Product;
|
||||
use App\Entity\ProductConstructeurLink;
|
||||
use App\Repository\AuditLogRepository;
|
||||
use App\Repository\ComposantRepository;
|
||||
use App\Repository\ConstructeurRepository;
|
||||
@@ -29,6 +33,7 @@ use App\Repository\SiteRepository;
|
||||
use DateTimeInterface;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use InvalidArgumentException;
|
||||
use LogicException;
|
||||
use Symfony\Component\HttpFoundation\RequestStack;
|
||||
use Throwable;
|
||||
|
||||
@@ -394,9 +399,9 @@ final class EntityVersionService
|
||||
|
||||
// Constructeurs
|
||||
$currentConstructeurIds = [];
|
||||
if (method_exists($entity, 'getConstructeurs')) {
|
||||
foreach ($entity->getConstructeurs() as $c) {
|
||||
$currentConstructeurIds[] = $c->getId();
|
||||
if (method_exists($entity, 'getConstructeurLinks')) {
|
||||
foreach ($entity->getConstructeurLinks() as $link) {
|
||||
$currentConstructeurIds[] = $link->getConstructeur()->getId();
|
||||
}
|
||||
}
|
||||
$snapshotConstructeurIds = [];
|
||||
@@ -543,37 +548,79 @@ final class EntityVersionService
|
||||
|
||||
private function restoreConstructeurs(object $entity, array $snapshot): void
|
||||
{
|
||||
if (!method_exists($entity, 'getConstructeurs') || !method_exists($entity, 'addConstructeur') || !method_exists($entity, 'removeConstructeur')) {
|
||||
if (!method_exists($entity, 'getConstructeurLinks')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$targetIds = [];
|
||||
$targetIds = [];
|
||||
$targetRefs = [];
|
||||
foreach ($snapshot['constructeurIds'] ?? [] as $entry) {
|
||||
$id = is_array($entry) ? ($entry['id'] ?? null) : $entry;
|
||||
if ($id) {
|
||||
$targetIds[] = $id;
|
||||
$targetIds[] = $id;
|
||||
$targetRefs[$id] = is_array($entry) ? ($entry['supplierReference'] ?? null) : null;
|
||||
}
|
||||
}
|
||||
|
||||
// Remove current constructeurs not in snapshot
|
||||
foreach ($entity->getConstructeurs()->toArray() as $c) {
|
||||
if (!in_array($c->getId(), $targetIds, true)) {
|
||||
$entity->removeConstructeur($c);
|
||||
// Remove current links not in snapshot
|
||||
foreach ($entity->getConstructeurLinks()->toArray() as $link) {
|
||||
$cId = $link->getConstructeur()->getId();
|
||||
if (!in_array($cId, $targetIds, true)) {
|
||||
$this->em->remove($link);
|
||||
} else {
|
||||
// Update supplierReference if present in snapshot
|
||||
$link->setSupplierReference($targetRefs[$cId] ?? null);
|
||||
}
|
||||
}
|
||||
|
||||
// Add missing constructeurs from snapshot
|
||||
$currentIds = array_map(fn ($c) => $c->getId(), $entity->getConstructeurs()->toArray());
|
||||
// Add missing constructeur links from snapshot
|
||||
$currentIds = array_map(
|
||||
fn ($link) => $link->getConstructeur()->getId(),
|
||||
$entity->getConstructeurLinks()->toArray(),
|
||||
);
|
||||
foreach ($targetIds as $id) {
|
||||
if (!in_array($id, $currentIds, true)) {
|
||||
$constructeur = $this->constructeurs->find($id);
|
||||
if (null !== $constructeur) {
|
||||
$entity->addConstructeur($constructeur);
|
||||
$link = $this->createConstructeurLink($entity);
|
||||
$link->setConstructeur($constructeur);
|
||||
$link->setSupplierReference($targetRefs[$id] ?? null);
|
||||
$this->em->persist($link);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function createConstructeurLink(object $entity): object
|
||||
{
|
||||
if ($entity instanceof Machine) {
|
||||
$link = new MachineConstructeurLink();
|
||||
$link->setMachine($entity);
|
||||
|
||||
return $link;
|
||||
}
|
||||
if ($entity instanceof Piece) {
|
||||
$link = new PieceConstructeurLink();
|
||||
$link->setPiece($entity);
|
||||
|
||||
return $link;
|
||||
}
|
||||
if ($entity instanceof Composant) {
|
||||
$link = new ComposantConstructeurLink();
|
||||
$link->setComposant($entity);
|
||||
|
||||
return $link;
|
||||
}
|
||||
if ($entity instanceof Product) {
|
||||
$link = new ProductConstructeurLink();
|
||||
$link->setProduct($entity);
|
||||
|
||||
return $link;
|
||||
}
|
||||
|
||||
throw new LogicException('Unsupported entity type for constructeur link');
|
||||
}
|
||||
|
||||
private function restoreMachineLinks(Machine $machine, array $snapshot): void
|
||||
{
|
||||
// Remove all existing links
|
||||
|
||||
Reference in New Issue
Block a user