feat : restructuration des dossiers pour implementer plus facilement la suite des WS
This commit is contained in:
64
src/Bovin/Api/BovinApi.php
Normal file
64
src/Bovin/Api/BovinApi.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Malio\EdnotifBundle\Bovin\Api;
|
||||
|
||||
use Malio\EdnotifBundle\Auth\TokenProvider;
|
||||
use Malio\EdnotifBundle\Bovin\Dto\AnimalFileDto;
|
||||
use Malio\EdnotifBundle\Bovin\Mapper\AnimalFileMapper;
|
||||
use Malio\EdnotifBundle\Shared\Exception\EdnotifException;
|
||||
use RuntimeException;
|
||||
use SoapClient;
|
||||
use SoapFault;
|
||||
|
||||
final readonly class BovinApi implements BovinApiInterface
|
||||
{
|
||||
public function __construct(
|
||||
private TokenProvider $tokenProvider,
|
||||
private SoapClient $businessClient,
|
||||
private AnimalFileMapper $bovinDossierMapper,
|
||||
private string $exploitationCountryCode,
|
||||
private string $exploitationNumber,
|
||||
) {}
|
||||
|
||||
public function getAnimalFile(string $nationalNumber, string $countryCode = 'FR'): AnimalFileDto
|
||||
{
|
||||
$token = $this->tokenProvider->getToken();
|
||||
|
||||
$requestPayload = [[
|
||||
'JetonAuthentification' => $token,
|
||||
'Exploitation' => [
|
||||
'CodePays' => $this->exploitationCountryCode,
|
||||
'NumeroExploitation' => $this->exploitationNumber,
|
||||
],
|
||||
'Bovin' => [
|
||||
'CodePays' => $countryCode,
|
||||
'NumeroNational' => $nationalNumber,
|
||||
],
|
||||
]];
|
||||
|
||||
try {
|
||||
/** @var object $soapResponse */
|
||||
$soapResponse = $this->businessClient->__soapCall('IpBGetDossierAnimal', $requestPayload);
|
||||
} catch (SoapFault $soapFault) {
|
||||
throw new RuntimeException('SOAP Fault on IpBGetDossierAnimal: '.$soapFault->getMessage(), 0, $soapFault);
|
||||
}
|
||||
|
||||
// Throw uniquement si Resultat=false (erreur métier)
|
||||
$standardResponseNode = $soapResponse->ReponseStandard ?? null;
|
||||
$isOk = is_object($standardResponseNode) && (($standardResponseNode->Resultat ?? false) === true);
|
||||
|
||||
if (!$isOk) {
|
||||
$anomalyNode = is_object($standardResponseNode) ? ($standardResponseNode->Anomalie ?? null) : null;
|
||||
|
||||
throw new EdnotifException(
|
||||
codeAnomalie: (string) ($anomalyNode->Code ?? 'UNKNOWN'),
|
||||
severite: (int) ($anomalyNode->Severite ?? 1),
|
||||
message: (string) ($anomalyNode->Message ?? 'EDNOTIF error')
|
||||
);
|
||||
}
|
||||
|
||||
return $this->bovinDossierMapper->map($soapResponse);
|
||||
}
|
||||
}
|
||||
12
src/Bovin/Api/BovinApiInterface.php
Normal file
12
src/Bovin/Api/BovinApiInterface.php
Normal file
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Malio\EdnotifBundle\Bovin\Api;
|
||||
|
||||
use Malio\EdnotifBundle\Bovin\Dto\AnimalFileDto;
|
||||
|
||||
interface BovinApiInterface
|
||||
{
|
||||
public function getAnimalFile(string $nationalNumber, string $countryCode = 'FR'): AnimalFileDto;
|
||||
}
|
||||
20
src/Bovin/Dto/AnimalFileDto.php
Normal file
20
src/Bovin/Dto/AnimalFileDto.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Malio\EdnotifBundle\Bovin\Dto;
|
||||
|
||||
use Malio\EdnotifBundle\Shared\Dto\StandardResponseDto;
|
||||
|
||||
final readonly class AnimalFileDto
|
||||
{
|
||||
/**
|
||||
* @param list<PresencePeriodDto> $presencePeriods
|
||||
*/
|
||||
public function __construct(
|
||||
public StandardResponseDto $standardResponse,
|
||||
public ?BovinIdentificationDto $identification,
|
||||
public array $presencePeriods,
|
||||
public ?object $rawSoapResponse, // pour garder 100% des data
|
||||
) {}
|
||||
}
|
||||
20
src/Bovin/Dto/BovinIdentificationDto.php
Normal file
20
src/Bovin/Dto/BovinIdentificationDto.php
Normal file
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Malio\EdnotifBundle\Bovin\Dto;
|
||||
|
||||
final readonly class BovinIdentificationDto
|
||||
{
|
||||
public function __construct(
|
||||
public ?BovinRef $bovin,
|
||||
public ?string $sex,
|
||||
public ?string $breedType,
|
||||
public ?DateValueDto $birthDate,
|
||||
public ?string $workNumber,
|
||||
public ?bool $isFilie,
|
||||
public ?ParentInfoDto $motherCarrier,
|
||||
public ?ParentInfoDto $fatherIpg,
|
||||
public ?ExploitationRef $birthExploitation,
|
||||
) {}
|
||||
}
|
||||
13
src/Bovin/Dto/BovinRef.php
Normal file
13
src/Bovin/Dto/BovinRef.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Malio\EdnotifBundle\Bovin\Dto;
|
||||
|
||||
final readonly class BovinRef
|
||||
{
|
||||
public function __construct(
|
||||
public ?string $countryCode,
|
||||
public ?string $nationalNumber,
|
||||
) {}
|
||||
}
|
||||
15
src/Bovin/Dto/DateValueDto.php
Normal file
15
src/Bovin/Dto/DateValueDto.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Malio\EdnotifBundle\Bovin\Dto;
|
||||
|
||||
use DateTimeImmutable;
|
||||
|
||||
final readonly class DateValueDto
|
||||
{
|
||||
public function __construct(
|
||||
public ?DateTimeImmutable $date,
|
||||
public ?string $completenessFlag,
|
||||
) {}
|
||||
}
|
||||
19
src/Bovin/Dto/DossierAnimalDto.php
Normal file
19
src/Bovin/Dto/DossierAnimalDto.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Malio\EdnotifBundle\Bovin\Dto;
|
||||
|
||||
final readonly class DossierAnimalDto
|
||||
{
|
||||
/**
|
||||
* @param array<string,mixed> $identiteBovin
|
||||
* @param list<array{entree: array<string,mixed>, sortie?: array<string,mixed>}> $periodesPresence
|
||||
*/
|
||||
public function __construct(
|
||||
public string $numeroNational,
|
||||
public array $identiteBovin,
|
||||
public array $periodesPresence,
|
||||
public object $rawResponse,
|
||||
) {}
|
||||
}
|
||||
13
src/Bovin/Dto/ExploitationRef.php
Normal file
13
src/Bovin/Dto/ExploitationRef.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Malio\EdnotifBundle\Bovin\Dto;
|
||||
|
||||
final readonly class ExploitationRef
|
||||
{
|
||||
public function __construct(
|
||||
public ?string $countryCode,
|
||||
public ?string $exploitationNumber,
|
||||
) {}
|
||||
}
|
||||
16
src/Bovin/Dto/MovementDto.php
Normal file
16
src/Bovin/Dto/MovementDto.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Malio\EdnotifBundle\Bovin\Dto;
|
||||
|
||||
use DateTimeImmutable;
|
||||
|
||||
final readonly class MovementDto
|
||||
{
|
||||
public function __construct(
|
||||
public ?DateTimeImmutable $date,
|
||||
public ?string $cause,
|
||||
public ?ExploitationRef $exploitation,
|
||||
) {}
|
||||
}
|
||||
13
src/Bovin/Dto/ParentInfoDto.php
Normal file
13
src/Bovin/Dto/ParentInfoDto.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Malio\EdnotifBundle\Bovin\Dto;
|
||||
|
||||
final readonly class ParentInfoDto
|
||||
{
|
||||
public function __construct(
|
||||
public ?BovinRef $bovin,
|
||||
public ?string $breedType,
|
||||
) {}
|
||||
}
|
||||
13
src/Bovin/Dto/PresencePeriodDto.php
Normal file
13
src/Bovin/Dto/PresencePeriodDto.php
Normal file
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Malio\EdnotifBundle\Bovin\Dto;
|
||||
|
||||
final readonly class PresencePeriodDto
|
||||
{
|
||||
public function __construct(
|
||||
public ?MovementDto $entry,
|
||||
public ?MovementDto $exit,
|
||||
) {}
|
||||
}
|
||||
235
src/Bovin/Mapper/AnimalFileMapper.php
Normal file
235
src/Bovin/Mapper/AnimalFileMapper.php
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user