fix(composant) : scaffold skeleton slots on creation + explicit unique constraint errors
- Add ComposantProcessor: initializes piece/product/subcomponent slots from ModelType skeleton requirements when a composant is created - UniqueConstraintSubscriber: priority 256, French error messages, constraint name detection for specific feedback - Migration: scaffold missing slots for existing composants in prod Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
110
src/State/ComposantProcessor.php
Normal file
110
src/State/ComposantProcessor.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\State;
|
||||
|
||||
use ApiPlatform\Metadata\Operation;
|
||||
use ApiPlatform\Metadata\Post;
|
||||
use ApiPlatform\State\ProcessorInterface;
|
||||
use App\Entity\Composant;
|
||||
use App\Entity\ComposantPieceSlot;
|
||||
use App\Entity\ComposantProductSlot;
|
||||
use App\Entity\ComposantSubcomponentSlot;
|
||||
use App\Entity\ModelType;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||
|
||||
/**
|
||||
* Initializes skeleton slots when a Composant is created with a typeComposant.
|
||||
*
|
||||
* Reads the SkeletonPieceRequirements, SkeletonProductRequirements and
|
||||
* SkeletonSubcomponentRequirements from the ModelType and creates matching
|
||||
* ComposantPieceSlot / ComposantProductSlot / ComposantSubcomponentSlot rows.
|
||||
*/
|
||||
final class ComposantProcessor implements ProcessorInterface
|
||||
{
|
||||
public function __construct(
|
||||
#[Autowire(service: 'api_platform.doctrine.orm.state.persist_processor')]
|
||||
private readonly ProcessorInterface $decorated,
|
||||
private readonly EntityManagerInterface $entityManager,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $uriVariables
|
||||
* @param array<string, mixed> $context
|
||||
*/
|
||||
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): mixed
|
||||
{
|
||||
if (!$data instanceof Composant) {
|
||||
return $this->decorated->process($data, $operation, $uriVariables, $context);
|
||||
}
|
||||
|
||||
$isCreation = $operation instanceof Post;
|
||||
|
||||
// Persist the entity first
|
||||
$result = $this->decorated->process($data, $operation, $uriVariables, $context);
|
||||
|
||||
// On creation, scaffold slots from the ModelType skeleton
|
||||
if ($isCreation) {
|
||||
$this->initializeSlotsFromSkeleton($data);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function initializeSlotsFromSkeleton(Composant $composant): void
|
||||
{
|
||||
$modelType = $composant->getTypeComposant();
|
||||
|
||||
if (!$modelType) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Ensure the ModelType's skeleton collections are loaded
|
||||
$modelType = $this->entityManager->getRepository(ModelType::class)->find($modelType->getId());
|
||||
|
||||
if (!$modelType) {
|
||||
return;
|
||||
}
|
||||
|
||||
$hasNewSlots = false;
|
||||
|
||||
// Piece slots
|
||||
foreach ($modelType->getSkeletonPieceRequirements() as $req) {
|
||||
$slot = new ComposantPieceSlot();
|
||||
$slot->setComposant($composant);
|
||||
$slot->setTypePiece($req->getTypePiece());
|
||||
$slot->setPosition($req->getPosition());
|
||||
$this->entityManager->persist($slot);
|
||||
$hasNewSlots = true;
|
||||
}
|
||||
|
||||
// Product slots
|
||||
foreach ($modelType->getSkeletonProductRequirements() as $req) {
|
||||
$slot = new ComposantProductSlot();
|
||||
$slot->setComposant($composant);
|
||||
$slot->setTypeProduct($req->getTypeProduct());
|
||||
$slot->setFamilyCode($req->getFamilyCode());
|
||||
$slot->setPosition($req->getPosition());
|
||||
$this->entityManager->persist($slot);
|
||||
$hasNewSlots = true;
|
||||
}
|
||||
|
||||
// Subcomponent slots
|
||||
foreach ($modelType->getSkeletonSubcomponentRequirements() as $req) {
|
||||
$slot = new ComposantSubcomponentSlot();
|
||||
$slot->setComposant($composant);
|
||||
$slot->setAlias($req->getAlias());
|
||||
$slot->setFamilyCode($req->getFamilyCode());
|
||||
$slot->setTypeComposant($req->getTypeComposant());
|
||||
$slot->setPosition($req->getPosition());
|
||||
$this->entityManager->persist($slot);
|
||||
$hasNewSlots = true;
|
||||
}
|
||||
|
||||
if ($hasNewSlots) {
|
||||
$this->entityManager->flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user