denyAccessUnlessGranted('ROLE_GESTIONNAIRE'); $slot = $this->entityManager->find(ComposantPieceSlot::class, $id); if (!$slot) { return $this->json(['success' => false, 'error' => 'Slot not found.'], 404); } $payload = json_decode($request->getContent(), true); if (!is_array($payload)) { return $this->json(['success' => false, 'error' => 'Invalid JSON payload.'], 400); } if (array_key_exists('quantity', $payload)) { $slot->setQuantity(max(1, (int) $payload['quantity'])); } if (array_key_exists('selectedPieceId', $payload)) { if (null === $payload['selectedPieceId']) { $slot->setSelectedPiece(null); } else { $piece = $this->entityManager->find(Piece::class, $payload['selectedPieceId']); if (!$piece) { return $this->json(['success' => false, 'error' => 'Pièce introuvable.'], 404); } $slotTypePiece = $slot->getTypePiece(); if ($slotTypePiece && $piece->getTypePiece()?->getId() !== $slotTypePiece->getId()) { return $this->json([ 'success' => false, 'error' => sprintf('La pièce doit être de type « %s ».', $slotTypePiece->getName()), ], 422); } $slot->setSelectedPiece($piece); } } $this->entityManager->flush(); return $this->json([ 'success' => true, 'id' => $slot->getId(), 'quantity' => $slot->getQuantity(), 'selectedPieceId' => $slot->getSelectedPiece()?->getId(), ]); } }