65 lines
2.3 KiB
PHP
65 lines
2.3 KiB
PHP
<?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);
|
|
}
|
|
}
|