Files
Inventory/src/Mcp/Tool/Composant/UpdateComposantTool.php
Matthieu 2f173e766d 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>
2026-03-16 14:38:55 +01:00

94 lines
2.9 KiB
PHP

<?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()]);
}
}