feat : restructuration des dossiers pour implementer plus facilement la suite des WS
All checks were successful
Auto Tag Develop / tag (push) Successful in 5s
Build Release Artefact / build (push) Successful in 36s

This commit is contained in:
2026-01-23 16:19:50 +01:00
parent b279f1ac47
commit 7e0c084ebe
36 changed files with 2633 additions and 226 deletions

View File

@@ -0,0 +1,235 @@
<?php
declare(strict_types=1);
namespace Malio\EdnotifBundle\Bovin\Mapper;
use DateTimeImmutable;
use Malio\EdnotifBundle\Bovin\Dto\AnimalFileDto;
use Malio\EdnotifBundle\Bovin\Dto\BovinIdentificationDto;
use Malio\EdnotifBundle\Bovin\Dto\BovinRef;
use Malio\EdnotifBundle\Bovin\Dto\DateValueDto;
use Malio\EdnotifBundle\Bovin\Dto\ExploitationRef;
use Malio\EdnotifBundle\Bovin\Dto\MovementDto;
use Malio\EdnotifBundle\Bovin\Dto\ParentInfoDto;
use Malio\EdnotifBundle\Bovin\Dto\PresencePeriodDto;
use Malio\EdnotifBundle\Shared\Dto\AnomalyDto;
use Malio\EdnotifBundle\Shared\Dto\StandardResponseDto;
use Throwable;
final class AnimalFileMapper
{
public function map(object $soapResponse): AnimalFileDto
{
$standardResponse = $this->mapStandardResponse($soapResponse->ReponseStandard ?? null);
$specificResponseNode = $soapResponse->ReponseSpecifique ?? null;
$bovinNode = is_object($specificResponseNode) ? ($specificResponseNode->Bovin ?? null) : null;
$identification = null;
$presencePeriods = [];
if (is_object($bovinNode)) {
$identificationNode = $bovinNode->IdentiteBovin ?? null;
if (is_object($identificationNode)) {
$identification = $this->mapIdentification($identificationNode);
}
$presencePeriodsNode = $bovinNode->PeriodesPresences->PeriodePresence ?? null;
foreach ($this->normalizeToList($presencePeriodsNode) as $presencePeriodNode) {
if (!is_object($presencePeriodNode)) {
continue;
}
$presencePeriods[] = $this->mapPresencePeriod($presencePeriodNode);
}
}
return new AnimalFileDto(
standardResponse: $standardResponse,
identification: $identification,
presencePeriods: $presencePeriods,
rawSoapResponse: $soapResponse
);
}
private function mapStandardResponse(mixed $standardResponseNode): StandardResponseDto
{
$result = (bool) ($standardResponseNode->Resultat ?? false);
$anomalyNode = $standardResponseNode->Anomalie ?? null;
$anomaly = null;
if (is_object($anomalyNode)) {
$anomaly = new AnomalyDto(
code: $this->toNullableString($anomalyNode->Code ?? null),
severity: $this->toNullableInt($anomalyNode->Severite ?? null),
message: $this->toNullableString($anomalyNode->Message ?? null),
);
}
return new StandardResponseDto($result, $anomaly);
}
private function mapIdentification(object $identificationNode): BovinIdentificationDto
{
$bovinRef = $this->mapBovinRef($identificationNode->Bovin ?? null);
$birthDate = null;
$birthDateNode = $identificationNode->DateNaissance ?? null;
if (is_object($birthDateNode)) {
$birthDate = new DateValueDto(
date: $this->toNullableDate($birthDateNode->Date ?? null),
completenessFlag: $this->toNullableString($birthDateNode->TemoinCompletude ?? null),
);
}
$motherCarrier = $this->mapParentInfo($identificationNode->MerePorteuse ?? null);
$fatherIpg = $this->mapParentInfo($identificationNode->PereIPG ?? null);
$birthExploitation = $this->mapExploitationRef($identificationNode->ExploitationNaissance ?? null);
return new BovinIdentificationDto(
bovin: $bovinRef,
sex: $this->toNullableString($identificationNode->Sexe ?? null),
breedType: $this->toNullableString($identificationNode->TypeRacial ?? null),
birthDate: $birthDate,
workNumber: $this->toNullableString($identificationNode->NumeroTravail ?? null),
isFilie: $this->toNullableBool($identificationNode->StatutFilie ?? null),
motherCarrier: $motherCarrier,
fatherIpg: $fatherIpg,
birthExploitation: $birthExploitation,
);
}
private function mapPresencePeriod(object $presencePeriodNode): PresencePeriodDto
{
$entryNode = $presencePeriodNode->Entree ?? null;
$exitNode = $presencePeriodNode->Sortie ?? null;
$entryMovement = is_object($entryNode) ? $this->mapMovement($entryNode, 'entry') : null;
$exitMovement = is_object($exitNode) ? $this->mapMovement($exitNode, 'exit') : null;
return new PresencePeriodDto(
entry: $entryMovement,
exit: $exitMovement,
);
}
private function mapMovement(object $movementNode, string $direction): MovementDto
{
$dateValue = null;
$causeValue = null;
if ('entry' === $direction) {
// SOAP: DateEntree / CauseEntree
$dateValue = $movementNode->DateEntree ?? ($movementNode->Date ?? ($movementNode->DateMouvement ?? null));
$causeValue = $movementNode->CauseEntree ?? null;
} else {
// SOAP (souvent): DateSortie / CauseSortie
$dateValue = $movementNode->DateSortie ?? ($movementNode->Date ?? ($movementNode->DateMouvement ?? null));
$causeValue = $movementNode->CauseSortie ?? null;
}
$exploitationRef = $this->mapExploitationRef($movementNode->Exploitation ?? null);
return new MovementDto(
date: $this->toNullableDate($dateValue),
cause: $this->toNullableString($causeValue),
exploitation: $exploitationRef,
);
}
private function mapParentInfo(mixed $parentNode): ?ParentInfoDto
{
if (!is_object($parentNode)) {
return null;
}
$bovinRef = $this->mapBovinRef($parentNode->Bovin ?? null);
return new ParentInfoDto(
bovin: $bovinRef,
breedType: $this->toNullableString($parentNode->TypeRacial ?? null),
);
}
private function mapBovinRef(mixed $bovinNode): ?BovinRef
{
if (!is_object($bovinNode)) {
return null;
}
return new BovinRef(
countryCode: $this->toNullableString($bovinNode->CodePays ?? null),
nationalNumber: $this->toNullableString($bovinNode->NumeroNational ?? null),
);
}
private function mapExploitationRef(mixed $exploitationNode): ?ExploitationRef
{
if (!is_object($exploitationNode)) {
return null;
}
return new ExploitationRef(
countryCode: $this->toNullableString($exploitationNode->CodePays ?? null),
exploitationNumber: $this->toNullableString($exploitationNode->NumeroExploitation ?? null),
);
}
/** @return list<mixed> */
private function normalizeToList(mixed $value): array
{
if (null === $value) {
return [];
}
return is_array($value) ? $value : [$value];
}
private function toNullableString(mixed $value): ?string
{
if (null === $value) {
return null;
}
$stringValue = trim((string) $value);
return '' === $stringValue ? null : $stringValue;
}
private function toNullableInt(mixed $value): ?int
{
if (null === $value) {
return null;
}
if (is_int($value)) {
return $value;
}
if (is_numeric($value)) {
return (int) $value;
}
return null;
}
private function toNullableBool(mixed $value): ?bool
{
if (null === $value) {
return null;
}
return (bool) $value;
}
private function toNullableDate(mixed $value): ?DateTimeImmutable
{
if (!is_string($value) || '' === trim($value)) {
return null;
}
try {
return new DateTimeImmutable($value);
} catch (Throwable) {
return null;
}
}
}