From 9d3dbf98c19e3dd0cfddc3e85f934aee8da415c9 Mon Sep 17 00:00:00 2001 From: tristan Date: Wed, 29 Apr 2026 10:02:24 +0200 Subject: [PATCH] fix : BovineProcessor utilise setBovineType avec auto-create MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Le processor appelait setBreedCode() qui n'existe plus depuis la migration vers la relation BovineType. Remplacé par setBovineType() avec un resolveBovineType() qui findOneBy(code) ou crée un placeholder réutilisable. Ajout de setSex() oublié au passage. Co-Authored-By: Claude Opus 4.7 (1M context) --- src/State/Bovin/BovineProcessor.php | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/src/State/Bovin/BovineProcessor.php b/src/State/Bovin/BovineProcessor.php index 2f058a3..69b00b2 100644 --- a/src/State/Bovin/BovineProcessor.php +++ b/src/State/Bovin/BovineProcessor.php @@ -7,6 +7,8 @@ namespace App\State\Bovin; use ApiPlatform\Metadata\Operation; use ApiPlatform\State\ProcessorInterface; use App\Entity\Bovine; +use App\Entity\BovineType; +use Doctrine\ORM\EntityManagerInterface; use Malio\EdnotifBundle\Bovin\Api\BovinApiInterface; use Symfony\Component\DependencyInjection\Attribute\Autowire; use Throwable; @@ -15,6 +17,7 @@ final class BovineProcessor implements ProcessorInterface { public function __construct( private readonly BovinApiInterface $bovinApi, + private readonly EntityManagerInterface $em, #[Autowire(service: 'api_platform.doctrine.orm.state.persist_processor')] private readonly ProcessorInterface $persistProcessor, ) {} @@ -41,28 +44,35 @@ final class BovineProcessor implements ProcessorInterface return; } + $bovine->setSex($identification->sex); $bovine->setWorkNumber($identification->workNumber); $bovine->setBirthDate($identification->birthDate?->date); - $bovine->setBreedCode($this->normalizeBreedCode($identification->breedType)); + $bovine->setBovineType($this->resolveBovineType($identification->breedType)); } catch (Throwable) { // External service unavailable — persist bovine without enrichment. } } - private function normalizeBreedCode(mixed $breedType): ?string + /** + * Trouve un BovineType par code, sinon en crée un placeholder + * (l'admin pourra le renommer ensuite dans /admin/bovin/bovin-list). + */ + private function resolveBovineType(?string $code): ?BovineType { - if (null === $breedType) { + if (null === $code || '' === $code) { return null; } - if (is_numeric($breedType)) { - return (string) $breedType; + $existing = $this->em->getRepository(BovineType::class)->findOneBy(['code' => $code]); + if (null !== $existing) { + return $existing; } - if (is_string($breedType) && preg_match('/\d+/', $breedType, $matches)) { - return $matches[0]; - } + $bovineType = new BovineType(); + $bovineType->setCode($code); + $bovineType->setLabel(sprintf('À renommer (%s)', $code)); + $this->em->persist($bovineType); - return null; + return $bovineType; } }