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; } }