fix : BovineProcessor utilise setBovineType avec auto-create

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) <noreply@anthropic.com>
This commit is contained in:
2026-04-29 10:02:24 +02:00
parent 486247bf86
commit 9d3dbf98c1

View File

@@ -7,6 +7,8 @@ namespace App\State\Bovin;
use ApiPlatform\Metadata\Operation; use ApiPlatform\Metadata\Operation;
use ApiPlatform\State\ProcessorInterface; use ApiPlatform\State\ProcessorInterface;
use App\Entity\Bovine; use App\Entity\Bovine;
use App\Entity\BovineType;
use Doctrine\ORM\EntityManagerInterface;
use Malio\EdnotifBundle\Bovin\Api\BovinApiInterface; use Malio\EdnotifBundle\Bovin\Api\BovinApiInterface;
use Symfony\Component\DependencyInjection\Attribute\Autowire; use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Throwable; use Throwable;
@@ -15,6 +17,7 @@ final class BovineProcessor implements ProcessorInterface
{ {
public function __construct( public function __construct(
private readonly BovinApiInterface $bovinApi, private readonly BovinApiInterface $bovinApi,
private readonly EntityManagerInterface $em,
#[Autowire(service: 'api_platform.doctrine.orm.state.persist_processor')] #[Autowire(service: 'api_platform.doctrine.orm.state.persist_processor')]
private readonly ProcessorInterface $persistProcessor, private readonly ProcessorInterface $persistProcessor,
) {} ) {}
@@ -41,28 +44,35 @@ final class BovineProcessor implements ProcessorInterface
return; return;
} }
$bovine->setSex($identification->sex);
$bovine->setWorkNumber($identification->workNumber); $bovine->setWorkNumber($identification->workNumber);
$bovine->setBirthDate($identification->birthDate?->date); $bovine->setBirthDate($identification->birthDate?->date);
$bovine->setBreedCode($this->normalizeBreedCode($identification->breedType)); $bovine->setBovineType($this->resolveBovineType($identification->breedType));
} catch (Throwable) { } catch (Throwable) {
// External service unavailable — persist bovine without enrichment. // 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; return null;
} }
if (is_numeric($breedType)) { $existing = $this->em->getRepository(BovineType::class)->findOneBy(['code' => $code]);
return (string) $breedType; if (null !== $existing) {
return $existing;
} }
if (is_string($breedType) && preg_match('/\d+/', $breedType, $matches)) { $bovineType = new BovineType();
return $matches[0]; $bovineType->setCode($code);
} $bovineType->setLabel(sprintf('À renommer (%s)', $code));
$this->em->persist($bovineType);
return null; return $bovineType;
} }
} }