entityManager->getRepository(Bovine::class)->findBy(['workNumber' => null]); if (0 === count($bovines)) { $io->success('Tous les bovins sont déjà enrichis.'); return Command::SUCCESS; } $io->info(sprintf('%d bovin(s) à enrichir.', count($bovines))); $enriched = 0; $failed = 0; foreach ($bovines as $bovine) { try { $animalFile = $this->bovinApi->getAnimalFile( nationalNumber: $bovine->getNationalNumber(), countryCode: 'FR', ); $identification = $animalFile->identification; if (null === $identification) { $io->warning(sprintf(' %s — pas d\'identification retournée.', $bovine->getNationalNumber())); ++$failed; continue; } $bovine->setWorkNumber($identification->workNumber); $bovine->setBirthDate($identification->birthDate?->date); $bovine->setBreedCode($this->normalizeBreedCode($identification->breedType)); ++$enriched; $io->text(sprintf(' ✓ %s → n° travail %s', $bovine->getNationalNumber(), $identification->workNumber ?? '—')); } catch (Throwable $e) { ++$failed; $io->warning(sprintf(' %s — erreur : %s', $bovine->getNationalNumber(), $e->getMessage())); } } $this->entityManager->flush(); $io->success(sprintf('%d enrichi(s), %d échoué(s).', $enriched, $failed)); return Command::SUCCESS; } private function normalizeBreedCode(mixed $breedType): ?string { if (null === $breedType) { return null; } if (is_numeric($breedType)) { return (string) $breedType; } if (is_string($breedType) && preg_match('/\d+/', $breedType, $matches)) { return $matches[0]; } return null; } }