Files
Central/src/State/EnvironmentCreateProcessor.php
tristan a3cd1f6b74 fix : use custom EnvironmentCreateProcessor to properly add environments
API Platform's default sub-resource POST was replacing instead of adding.
Custom processor with read:false + Link + manual persist fixes this.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-06 16:49:44 +02:00

45 lines
1.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\State;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface;
use App\Entity\Environment;
use App\Repository\ApplicationRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
final readonly class EnvironmentCreateProcessor implements ProcessorInterface
{
public function __construct(
private ApplicationRepository $applicationRepository,
private EntityManagerInterface $entityManager,
) {}
/**
* @param Environment $data
*/
public function process(mixed $data, Operation $operation, array $uriVariables = [], array $context = []): Environment
{
$slug = $uriVariables['slug']
?? $context['request']?->attributes->get('slug')
?? $context['request']?->attributes->get('_route_params')['slug']
?? '';
$application = $this->applicationRepository->findOneBy(['slug' => $slug]);
if (null === $application) {
throw new NotFoundHttpException(sprintf('Application "%s" not found.', $slug));
}
$data->setApplication($application);
$this->entityManager->persist($data);
$this->entityManager->flush();
return $data;
}
}