feat(mcp) : add CRUD tools for Pieces, Composants, Machines

- 5 tools each: list, get, create, update, delete
- Piece: includes typePiece, constructeurs, prix (string)
- Composant: includes typeComposant, constructeurs, prix (string)
- Machine: includes site (required), constructeurs
- 40 MCP tests pass total

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Matthieu
2026-03-16 14:38:55 +01:00
parent 4f1e136dc5
commit 2f173e766d
18 changed files with 1277 additions and 0 deletions

View File

@@ -0,0 +1,80 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Composant;
use App\Entity\Composant;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\ConstructeurRepository;
use App\Repository\ModelTypeRepository;
use Doctrine\ORM\EntityManagerInterface;
use Mcp\Capability\Attribute\McpTool;
use Symfony\Bundle\SecurityBundle\Security;
#[McpTool(
name: 'create_composant',
description: 'Create a new composant. prix must be a string (e.g. "12.50"). Requires ROLE_GESTIONNAIRE.',
)]
class CreateComposantTool
{
use McpToolHelper;
public function __construct(
private readonly EntityManagerInterface $em,
private readonly Security $security,
private readonly ModelTypeRepository $modelTypes,
private readonly ConstructeurRepository $constructeurs,
) {}
/**
* @param string[] $constructeurIds
*/
public function __invoke(
string $name,
string $reference = '',
string $description = '',
string $prix = '',
string $modelTypeId = '',
array $constructeurIds = [],
): array {
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
$composant = new Composant();
$composant->setName($name);
if ('' !== $reference) {
$composant->setReference($reference);
}
if ('' !== $description) {
$composant->setDescription($description);
}
if ('' !== $prix) {
$composant->setPrix($prix);
}
if ('' !== $modelTypeId) {
$modelType = $this->modelTypes->find($modelTypeId);
if (!$modelType) {
$this->mcpError('not_found', "ModelType not found: {$modelTypeId}");
}
$composant->setTypeComposant($modelType);
}
foreach ($constructeurIds as $cId) {
$c = $this->constructeurs->find($cId);
if (!$c) {
$this->mcpError('not_found', "Constructeur not found: {$cId}");
}
$composant->addConstructeur($c);
}
$this->em->persist($composant);
$this->em->flush();
return $this->jsonResponse([
'id' => $composant->getId(),
'name' => $composant->getName(),
]);
}
}

View File

@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Composant;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\ComposantRepository;
use Doctrine\ORM\EntityManagerInterface;
use Mcp\Capability\Attribute\McpTool;
use Symfony\Bundle\SecurityBundle\Security;
#[McpTool(
name: 'delete_composant',
description: 'Delete a composant by ID. Requires ROLE_GESTIONNAIRE.',
)]
class DeleteComposantTool
{
use McpToolHelper;
public function __construct(
private readonly ComposantRepository $composants,
private readonly EntityManagerInterface $em,
private readonly Security $security,
) {}
public function __invoke(string $composantId): array
{
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
$composant = $this->composants->find($composantId);
if (!$composant) {
$this->mcpError('not_found', "Composant not found: {$composantId}");
}
$this->em->remove($composant);
$this->em->flush();
return $this->jsonResponse(['deleted' => true, 'id' => $composantId]);
}
}

View File

@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Composant;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\ComposantRepository;
use Mcp\Capability\Attribute\McpTool;
#[McpTool(
name: 'get_composant',
description: 'Get a single composant by ID with all its details, including typeComposant and constructeurs.',
)]
class GetComposantTool
{
use McpToolHelper;
public function __construct(
private readonly ComposantRepository $composants,
) {}
public function __invoke(string $composantId): array
{
$composant = $this->composants->find($composantId);
if (!$composant) {
$this->mcpError('not_found', "Composant not found: {$composantId}");
}
$constructeurs = [];
foreach ($composant->getConstructeurs() as $c) {
$constructeurs[] = [
'id' => $c->getId(),
'name' => $c->getName(),
];
}
$typeComposant = null;
if ($composant->getTypeComposant()) {
$typeComposant = [
'id' => $composant->getTypeComposant()->getId(),
'name' => $composant->getTypeComposant()->getName(),
];
}
return $this->jsonResponse([
'id' => $composant->getId(),
'name' => $composant->getName(),
'reference' => $composant->getReference(),
'description' => $composant->getDescription(),
'prix' => $composant->getPrix(),
'typeComposant' => $typeComposant,
'constructeurs' => $constructeurs,
'createdAt' => $composant->getCreatedAt()->format('Y-m-d H:i:s'),
'updatedAt' => $composant->getUpdatedAt()->format('Y-m-d H:i:s'),
]);
}
}

View File

@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Composant;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\ComposantRepository;
use Mcp\Capability\Attribute\McpTool;
#[McpTool(
name: 'list_composants',
description: 'List composants with pagination. Filterable by name or reference.',
)]
class ListComposantsTool
{
use McpToolHelper;
public function __construct(
private readonly ComposantRepository $composants,
) {}
public function __invoke(int $page = 1, int $limit = 30, string $search = ''): array
{
$p = $this->paginationParams($page, $limit);
$countQb = $this->composants->createQueryBuilder('c')
->select('COUNT(c.id)')
;
$qb = $this->composants->createQueryBuilder('c')
->select('c.id', 'c.name', 'c.reference', 'c.prix')
->orderBy('c.name', 'ASC')
;
if ('' !== $search) {
$countQb->andWhere('LOWER(c.name) LIKE LOWER(:search) OR LOWER(c.reference) LIKE LOWER(:search)')
->setParameter('search', "%{$search}%")
;
$qb->andWhere('LOWER(c.name) LIKE LOWER(:search) OR LOWER(c.reference) LIKE LOWER(:search)')
->setParameter('search', "%{$search}%")
;
}
$total = (int) $countQb->getQuery()->getSingleScalarResult();
$items = $qb->setFirstResult($p['offset'])
->setMaxResults($p['limit'])
->getQuery()
->getArrayResult()
;
return $this->paginatedResponse($items, $total, $p['page'], $p['limit']);
}
}

View File

@@ -0,0 +1,93 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Composant;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\ComposantRepository;
use App\Repository\ConstructeurRepository;
use App\Repository\ModelTypeRepository;
use Doctrine\ORM\EntityManagerInterface;
use Mcp\Capability\Attribute\McpTool;
use Symfony\Bundle\SecurityBundle\Security;
#[McpTool(
name: 'update_composant',
description: 'Update an existing composant. Only provided fields are changed. prix must be a string. Requires ROLE_GESTIONNAIRE.',
)]
class UpdateComposantTool
{
use McpToolHelper;
public function __construct(
private readonly ComposantRepository $composants,
private readonly EntityManagerInterface $em,
private readonly Security $security,
private readonly ModelTypeRepository $modelTypes,
private readonly ConstructeurRepository $constructeurs,
) {}
/**
* @param null|string[] $constructeurIds
*/
public function __invoke(
string $composantId,
?string $name = null,
?string $reference = null,
?string $description = null,
?string $prix = null,
?string $modelTypeId = null,
?array $constructeurIds = null,
): array {
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
$composant = $this->composants->find($composantId);
if (!$composant) {
$this->mcpError('not_found', "Composant not found: {$composantId}");
}
if (null !== $name) {
$composant->setName($name);
}
if (null !== $reference) {
$composant->setReference($reference);
}
if (null !== $description) {
$composant->setDescription($description);
}
if (null !== $prix) {
$composant->setPrix($prix);
}
if (null !== $modelTypeId) {
if ('' === $modelTypeId) {
$composant->setTypeComposant(null);
} else {
$modelType = $this->modelTypes->find($modelTypeId);
if (!$modelType) {
$this->mcpError('not_found', "ModelType not found: {$modelTypeId}");
}
$composant->setTypeComposant($modelType);
}
}
if (null !== $constructeurIds) {
foreach ($composant->getConstructeurs()->toArray() as $existing) {
$composant->removeConstructeur($existing);
}
foreach ($constructeurIds as $cId) {
$c = $this->constructeurs->find($cId);
if (!$c) {
$this->mcpError('not_found', "Constructeur not found: {$cId}");
}
$composant->addConstructeur($c);
}
}
$this->em->flush();
return $this->jsonResponse(['id' => $composant->getId(), 'name' => $composant->getName()]);
}
}

View File

@@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Machine;
use App\Entity\Machine;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\ConstructeurRepository;
use App\Repository\SiteRepository;
use Doctrine\ORM\EntityManagerInterface;
use Mcp\Capability\Attribute\McpTool;
use Symfony\Bundle\SecurityBundle\Security;
#[McpTool(
name: 'create_machine',
description: 'Create a new machine. siteId is required. prix must be a string (e.g. "12.50"). Requires ROLE_GESTIONNAIRE.',
)]
class CreateMachineTool
{
use McpToolHelper;
public function __construct(
private readonly EntityManagerInterface $em,
private readonly Security $security,
private readonly SiteRepository $sites,
private readonly ConstructeurRepository $constructeurs,
) {}
/**
* @param string[] $constructeurIds
*/
public function __invoke(
string $name,
string $siteId,
string $reference = '',
string $prix = '',
array $constructeurIds = [],
): array {
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
$site = $this->sites->find($siteId);
if (!$site) {
$this->mcpError('not_found', "Site not found: {$siteId}");
}
$machine = new Machine();
$machine->setName($name);
$machine->setSite($site);
if ('' !== $reference) {
$machine->setReference($reference);
}
if ('' !== $prix) {
$machine->setPrix($prix);
}
foreach ($constructeurIds as $cId) {
$c = $this->constructeurs->find($cId);
if (!$c) {
$this->mcpError('not_found', "Constructeur not found: {$cId}");
}
$machine->addConstructeur($c);
}
$this->em->persist($machine);
$this->em->flush();
return $this->jsonResponse([
'id' => $machine->getId(),
'name' => $machine->getName(),
]);
}
}

View File

@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Machine;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\MachineRepository;
use Doctrine\ORM\EntityManagerInterface;
use Mcp\Capability\Attribute\McpTool;
use Symfony\Bundle\SecurityBundle\Security;
#[McpTool(
name: 'delete_machine',
description: 'Delete a machine by ID. Requires ROLE_GESTIONNAIRE.',
)]
class DeleteMachineTool
{
use McpToolHelper;
public function __construct(
private readonly MachineRepository $machines,
private readonly EntityManagerInterface $em,
private readonly Security $security,
) {}
public function __invoke(string $machineId): array
{
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
$machine = $this->machines->find($machineId);
if (!$machine) {
$this->mcpError('not_found', "Machine not found: {$machineId}");
}
$this->em->remove($machine);
$this->em->flush();
return $this->jsonResponse(['deleted' => true, 'id' => $machineId]);
}
}

View File

@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Machine;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\MachineRepository;
use Mcp\Capability\Attribute\McpTool;
#[McpTool(
name: 'get_machine',
description: 'Get a single machine by ID with all its details, including site and constructeurs.',
)]
class GetMachineTool
{
use McpToolHelper;
public function __construct(
private readonly MachineRepository $machines,
) {}
public function __invoke(string $machineId): array
{
$machine = $this->machines->find($machineId);
if (!$machine) {
$this->mcpError('not_found', "Machine not found: {$machineId}");
}
$constructeurs = [];
foreach ($machine->getConstructeurs() as $c) {
$constructeurs[] = [
'id' => $c->getId(),
'name' => $c->getName(),
];
}
$site = null;
if ($machine->getSite()) {
$site = [
'id' => $machine->getSite()->getId(),
'name' => $machine->getSite()->getName(),
];
}
return $this->jsonResponse([
'id' => $machine->getId(),
'name' => $machine->getName(),
'reference' => $machine->getReference(),
'prix' => $machine->getPrix(),
'site' => $site,
'constructeurs' => $constructeurs,
'createdAt' => $machine->getCreatedAt()->format('Y-m-d H:i:s'),
'updatedAt' => $machine->getUpdatedAt()->format('Y-m-d H:i:s'),
]);
}
}

View File

@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Machine;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\MachineRepository;
use Mcp\Capability\Attribute\McpTool;
#[McpTool(
name: 'list_machines',
description: 'List machines with pagination. Filterable by name or reference.',
)]
class ListMachinesTool
{
use McpToolHelper;
public function __construct(
private readonly MachineRepository $machines,
) {}
public function __invoke(int $page = 1, int $limit = 30, string $search = ''): array
{
$p = $this->paginationParams($page, $limit);
$countQb = $this->machines->createQueryBuilder('m')
->select('COUNT(m.id)')
;
$qb = $this->machines->createQueryBuilder('m')
->select('m.id', 'm.name', 'm.reference', 'm.prix')
->orderBy('m.name', 'ASC')
;
if ('' !== $search) {
$countQb->andWhere('LOWER(m.name) LIKE LOWER(:search) OR LOWER(m.reference) LIKE LOWER(:search)')
->setParameter('search', "%{$search}%")
;
$qb->andWhere('LOWER(m.name) LIKE LOWER(:search) OR LOWER(m.reference) LIKE LOWER(:search)')
->setParameter('search', "%{$search}%")
;
}
$total = (int) $countQb->getQuery()->getSingleScalarResult();
$items = $qb->setFirstResult($p['offset'])
->setMaxResults($p['limit'])
->getQuery()
->getArrayResult()
;
return $this->paginatedResponse($items, $total, $p['page'], $p['limit']);
}
}

View File

@@ -0,0 +1,85 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Machine;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\ConstructeurRepository;
use App\Repository\MachineRepository;
use App\Repository\SiteRepository;
use Doctrine\ORM\EntityManagerInterface;
use Mcp\Capability\Attribute\McpTool;
use Symfony\Bundle\SecurityBundle\Security;
#[McpTool(
name: 'update_machine',
description: 'Update an existing machine. Only provided fields are changed. prix must be a string. Requires ROLE_GESTIONNAIRE.',
)]
class UpdateMachineTool
{
use McpToolHelper;
public function __construct(
private readonly MachineRepository $machines,
private readonly EntityManagerInterface $em,
private readonly Security $security,
private readonly SiteRepository $sites,
private readonly ConstructeurRepository $constructeurs,
) {}
/**
* @param null|string[] $constructeurIds
*/
public function __invoke(
string $machineId,
?string $name = null,
?string $reference = null,
?string $prix = null,
?string $siteId = null,
?array $constructeurIds = null,
): array {
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
$machine = $this->machines->find($machineId);
if (!$machine) {
$this->mcpError('not_found', "Machine not found: {$machineId}");
}
if (null !== $name) {
$machine->setName($name);
}
if (null !== $reference) {
$machine->setReference($reference);
}
if (null !== $prix) {
$machine->setPrix($prix);
}
if (null !== $siteId) {
$site = $this->sites->find($siteId);
if (!$site) {
$this->mcpError('not_found', "Site not found: {$siteId}");
}
$machine->setSite($site);
}
if (null !== $constructeurIds) {
foreach ($machine->getConstructeurs()->toArray() as $existing) {
$machine->removeConstructeur($existing);
}
foreach ($constructeurIds as $cId) {
$c = $this->constructeurs->find($cId);
if (!$c) {
$this->mcpError('not_found', "Constructeur not found: {$cId}");
}
$machine->addConstructeur($c);
}
}
$this->em->flush();
return $this->jsonResponse(['id' => $machine->getId(), 'name' => $machine->getName()]);
}
}

View File

@@ -0,0 +1,80 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Piece;
use App\Entity\Piece;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\ConstructeurRepository;
use App\Repository\ModelTypeRepository;
use Doctrine\ORM\EntityManagerInterface;
use Mcp\Capability\Attribute\McpTool;
use Symfony\Bundle\SecurityBundle\Security;
#[McpTool(
name: 'create_piece',
description: 'Create a new piece. prix must be a string (e.g. "12.50"). Requires ROLE_GESTIONNAIRE.',
)]
class CreatePieceTool
{
use McpToolHelper;
public function __construct(
private readonly EntityManagerInterface $em,
private readonly Security $security,
private readonly ModelTypeRepository $modelTypes,
private readonly ConstructeurRepository $constructeurs,
) {}
/**
* @param string[] $constructeurIds
*/
public function __invoke(
string $name,
string $reference = '',
string $description = '',
string $prix = '',
string $modelTypeId = '',
array $constructeurIds = [],
): array {
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
$piece = new Piece();
$piece->setName($name);
if ('' !== $reference) {
$piece->setReference($reference);
}
if ('' !== $description) {
$piece->setDescription($description);
}
if ('' !== $prix) {
$piece->setPrix($prix);
}
if ('' !== $modelTypeId) {
$modelType = $this->modelTypes->find($modelTypeId);
if (!$modelType) {
$this->mcpError('not_found', "ModelType not found: {$modelTypeId}");
}
$piece->setTypePiece($modelType);
}
foreach ($constructeurIds as $cId) {
$c = $this->constructeurs->find($cId);
if (!$c) {
$this->mcpError('not_found', "Constructeur not found: {$cId}");
}
$piece->addConstructeur($c);
}
$this->em->persist($piece);
$this->em->flush();
return $this->jsonResponse([
'id' => $piece->getId(),
'name' => $piece->getName(),
]);
}
}

View File

@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Piece;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\PieceRepository;
use Doctrine\ORM\EntityManagerInterface;
use Mcp\Capability\Attribute\McpTool;
use Symfony\Bundle\SecurityBundle\Security;
#[McpTool(
name: 'delete_piece',
description: 'Delete a piece by ID. Requires ROLE_GESTIONNAIRE.',
)]
class DeletePieceTool
{
use McpToolHelper;
public function __construct(
private readonly PieceRepository $pieces,
private readonly EntityManagerInterface $em,
private readonly Security $security,
) {}
public function __invoke(string $pieceId): array
{
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
$piece = $this->pieces->find($pieceId);
if (!$piece) {
$this->mcpError('not_found', "Piece not found: {$pieceId}");
}
$this->em->remove($piece);
$this->em->flush();
return $this->jsonResponse(['deleted' => true, 'id' => $pieceId]);
}
}

View File

@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Piece;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\PieceRepository;
use Mcp\Capability\Attribute\McpTool;
#[McpTool(
name: 'get_piece',
description: 'Get a single piece by ID with all its details, including typePiece and constructeurs.',
)]
class GetPieceTool
{
use McpToolHelper;
public function __construct(
private readonly PieceRepository $pieces,
) {}
public function __invoke(string $pieceId): array
{
$piece = $this->pieces->find($pieceId);
if (!$piece) {
$this->mcpError('not_found', "Piece not found: {$pieceId}");
}
$constructeurs = [];
foreach ($piece->getConstructeurs() as $c) {
$constructeurs[] = [
'id' => $c->getId(),
'name' => $c->getName(),
];
}
$typePiece = null;
if ($piece->getTypePiece()) {
$typePiece = [
'id' => $piece->getTypePiece()->getId(),
'name' => $piece->getTypePiece()->getName(),
];
}
return $this->jsonResponse([
'id' => $piece->getId(),
'name' => $piece->getName(),
'reference' => $piece->getReference(),
'description' => $piece->getDescription(),
'prix' => $piece->getPrix(),
'typePiece' => $typePiece,
'constructeurs' => $constructeurs,
'createdAt' => $piece->getCreatedAt()->format('Y-m-d H:i:s'),
'updatedAt' => $piece->getUpdatedAt()->format('Y-m-d H:i:s'),
]);
}
}

View File

@@ -0,0 +1,55 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Piece;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\PieceRepository;
use Mcp\Capability\Attribute\McpTool;
#[McpTool(
name: 'list_pieces',
description: 'List pieces with pagination. Filterable by name or reference.',
)]
class ListPiecesTool
{
use McpToolHelper;
public function __construct(
private readonly PieceRepository $pieces,
) {}
public function __invoke(int $page = 1, int $limit = 30, string $search = ''): array
{
$p = $this->paginationParams($page, $limit);
$countQb = $this->pieces->createQueryBuilder('pi')
->select('COUNT(pi.id)')
;
$qb = $this->pieces->createQueryBuilder('pi')
->select('pi.id', 'pi.name', 'pi.reference', 'pi.prix')
->orderBy('pi.name', 'ASC')
;
if ('' !== $search) {
$countQb->andWhere('LOWER(pi.name) LIKE LOWER(:search) OR LOWER(pi.reference) LIKE LOWER(:search)')
->setParameter('search', "%{$search}%")
;
$qb->andWhere('LOWER(pi.name) LIKE LOWER(:search) OR LOWER(pi.reference) LIKE LOWER(:search)')
->setParameter('search', "%{$search}%")
;
}
$total = (int) $countQb->getQuery()->getSingleScalarResult();
$items = $qb->setFirstResult($p['offset'])
->setMaxResults($p['limit'])
->getQuery()
->getArrayResult()
;
return $this->paginatedResponse($items, $total, $p['page'], $p['limit']);
}
}

View File

@@ -0,0 +1,93 @@
<?php
declare(strict_types=1);
namespace App\Mcp\Tool\Piece;
use App\Mcp\Tool\McpToolHelper;
use App\Repository\ConstructeurRepository;
use App\Repository\ModelTypeRepository;
use App\Repository\PieceRepository;
use Doctrine\ORM\EntityManagerInterface;
use Mcp\Capability\Attribute\McpTool;
use Symfony\Bundle\SecurityBundle\Security;
#[McpTool(
name: 'update_piece',
description: 'Update an existing piece. Only provided fields are changed. prix must be a string. Requires ROLE_GESTIONNAIRE.',
)]
class UpdatePieceTool
{
use McpToolHelper;
public function __construct(
private readonly PieceRepository $pieces,
private readonly EntityManagerInterface $em,
private readonly Security $security,
private readonly ModelTypeRepository $modelTypes,
private readonly ConstructeurRepository $constructeurs,
) {}
/**
* @param null|string[] $constructeurIds
*/
public function __invoke(
string $pieceId,
?string $name = null,
?string $reference = null,
?string $description = null,
?string $prix = null,
?string $modelTypeId = null,
?array $constructeurIds = null,
): array {
$this->requireRole($this->security, 'ROLE_GESTIONNAIRE');
$piece = $this->pieces->find($pieceId);
if (!$piece) {
$this->mcpError('not_found', "Piece not found: {$pieceId}");
}
if (null !== $name) {
$piece->setName($name);
}
if (null !== $reference) {
$piece->setReference($reference);
}
if (null !== $description) {
$piece->setDescription($description);
}
if (null !== $prix) {
$piece->setPrix($prix);
}
if (null !== $modelTypeId) {
if ('' === $modelTypeId) {
$piece->setTypePiece(null);
} else {
$modelType = $this->modelTypes->find($modelTypeId);
if (!$modelType) {
$this->mcpError('not_found', "ModelType not found: {$modelTypeId}");
}
$piece->setTypePiece($modelType);
}
}
if (null !== $constructeurIds) {
foreach ($piece->getConstructeurs()->toArray() as $existing) {
$piece->removeConstructeur($existing);
}
foreach ($constructeurIds as $cId) {
$c = $this->constructeurs->find($cId);
if (!$c) {
$this->mcpError('not_found', "Constructeur not found: {$cId}");
}
$piece->addConstructeur($c);
}
}
$this->em->flush();
return $this->jsonResponse(['id' => $piece->getId(), 'name' => $piece->getName()]);
}
}