179 lines
6.7 KiB
PHP
179 lines
6.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Malio\EdnotifBundle\Bovin\Api;
|
|
|
|
use DateTimeInterface;
|
|
use Malio\EdnotifBundle\Auth\TokenProvider;
|
|
use Malio\EdnotifBundle\Bovin\Dto\AnimalFileDto;
|
|
use Malio\EdnotifBundle\Bovin\Dto\InventoryDto;
|
|
use Malio\EdnotifBundle\Bovin\Dto\PresumedExitsDto;
|
|
use Malio\EdnotifBundle\Bovin\Dto\ReturnedDossiersDto;
|
|
use Malio\EdnotifBundle\Bovin\Mapper\AnimalFileMapper;
|
|
use Malio\EdnotifBundle\Bovin\Mapper\InventoryMapper;
|
|
use Malio\EdnotifBundle\Bovin\Mapper\PresumedExitsMapper;
|
|
use Malio\EdnotifBundle\Bovin\Mapper\ReturnedDossiersMapper;
|
|
use Malio\EdnotifBundle\Shared\Exception\EdnotifException;
|
|
use Malio\EdnotifBundle\Shared\Soap\ZipMessageDecoder;
|
|
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 InventoryMapper $inventoryMapper,
|
|
private ReturnedDossiersMapper $returnedDossiersMapper,
|
|
private PresumedExitsMapper $presumedExitsMapper,
|
|
private ZipMessageDecoder $zipMessageDecoder,
|
|
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);
|
|
}
|
|
|
|
$this->assertSuccessfulResponse($soapResponse, 'IpBGetDossierAnimal');
|
|
|
|
return $this->bovinDossierMapper->map($soapResponse);
|
|
}
|
|
|
|
public function getInventory(
|
|
DateTimeInterface $startDate,
|
|
?DateTimeInterface $endDate = null,
|
|
bool $includeEarTagStock = false,
|
|
): InventoryDto {
|
|
$token = $this->tokenProvider->getToken();
|
|
|
|
$payload = [
|
|
'JetonAuthentification' => $token,
|
|
'Exploitation' => [
|
|
'CodePays' => $this->exploitationCountryCode,
|
|
'NumeroExploitation' => $this->exploitationNumber,
|
|
],
|
|
'DateDebut' => $startDate->format('Y-m-d'),
|
|
'StockBoucles' => $includeEarTagStock,
|
|
];
|
|
if (null !== $endDate) {
|
|
$payload['DateFin'] = $endDate->format('Y-m-d');
|
|
}
|
|
|
|
try {
|
|
/** @var object $soapResponse */
|
|
$soapResponse = $this->businessClient->__soapCall('IpBGetInventaire', [$payload]);
|
|
} catch (SoapFault $soapFault) {
|
|
throw new RuntimeException('SOAP Fault on IpBGetInventaire: '.$soapFault->getMessage(), 0, $soapFault);
|
|
}
|
|
|
|
$this->assertSuccessfulResponse($soapResponse, 'IpBGetInventaire');
|
|
|
|
$messageZip = $soapResponse->ReponseSpecifique->MessageZip ?? null;
|
|
$unzippedMessage = is_string($messageZip) && '' !== $messageZip
|
|
? $this->zipMessageDecoder->decode($messageZip)
|
|
: null;
|
|
|
|
return $this->inventoryMapper->map($soapResponse, $unzippedMessage);
|
|
}
|
|
|
|
public function getReturnedDossiers(DateTimeInterface $startDate): ReturnedDossiersDto
|
|
{
|
|
$token = $this->tokenProvider->getToken();
|
|
|
|
$payload = [
|
|
'JetonAuthentification' => $token,
|
|
'Exploitation' => [
|
|
'CodePays' => $this->exploitationCountryCode,
|
|
'NumeroExploitation' => $this->exploitationNumber,
|
|
],
|
|
'DateDebut' => $startDate->format('Y-m-d'),
|
|
];
|
|
|
|
try {
|
|
/** @var object $soapResponse */
|
|
$soapResponse = $this->businessClient->__soapCall('IpBGetRetourDossiers', [$payload]);
|
|
} catch (SoapFault $soapFault) {
|
|
throw new RuntimeException('SOAP Fault on IpBGetRetourDossiers: '.$soapFault->getMessage(), 0, $soapFault);
|
|
}
|
|
|
|
$this->assertSuccessfulResponse($soapResponse, 'IpBGetRetourDossiers');
|
|
|
|
$messageZip = $soapResponse->ReponseSpecifique->MessageZip ?? null;
|
|
$unzippedMessage = is_string($messageZip) && '' !== $messageZip
|
|
? $this->zipMessageDecoder->decode($messageZip)
|
|
: null;
|
|
|
|
return $this->returnedDossiersMapper->map($soapResponse, $unzippedMessage);
|
|
}
|
|
|
|
public function getPresumedExits(): PresumedExitsDto
|
|
{
|
|
$token = $this->tokenProvider->getToken();
|
|
|
|
$payload = [
|
|
'JetonAuthentification' => $token,
|
|
'Exploitation' => [
|
|
'CodePays' => $this->exploitationCountryCode,
|
|
'NumeroExploitation' => $this->exploitationNumber,
|
|
],
|
|
];
|
|
|
|
try {
|
|
/** @var object $soapResponse */
|
|
$soapResponse = $this->businessClient->__soapCall('IpBGetSortiesPresumees', [$payload]);
|
|
} catch (SoapFault $soapFault) {
|
|
throw new RuntimeException('SOAP Fault on IpBGetSortiesPresumees: '.$soapFault->getMessage(), 0, $soapFault);
|
|
}
|
|
|
|
$this->assertSuccessfulResponse($soapResponse, 'IpBGetSortiesPresumees');
|
|
|
|
$messageZip = $soapResponse->ReponseSpecifique->MessageZip ?? null;
|
|
$unzippedMessage = is_string($messageZip) && '' !== $messageZip
|
|
? $this->zipMessageDecoder->decode($messageZip)
|
|
: null;
|
|
|
|
return $this->presumedExitsMapper->map($soapResponse, $unzippedMessage);
|
|
}
|
|
|
|
private function assertSuccessfulResponse(object $soapResponse, string $operation): void
|
|
{
|
|
$standardResponseNode = $soapResponse->ReponseStandard ?? null;
|
|
$isOk = is_object($standardResponseNode) && (($standardResponseNode->Resultat ?? false) === true);
|
|
|
|
if ($isOk) {
|
|
return;
|
|
}
|
|
|
|
$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 ?? $operation.' : EDNOTIF error')
|
|
);
|
|
}
|
|
}
|