feat : ajout de l'auth et du guichet pour la récupération d'info bovin

This commit is contained in:
2026-01-23 10:03:14 +01:00
commit b279f1ac47
25 changed files with 4565 additions and 0 deletions

View File

@@ -0,0 +1,98 @@
<?php
declare(strict_types=1);
namespace Malio\EdnotifBundle\Auth;
use Malio\EdnotifBundle\Exception\EdnotifException;
use Psr\Cache\CacheItemPoolInterface;
use SoapClient;
use SoapFault;
final class TokenProvider
{
public function __construct(
private SoapClient $guichetClient,
private string $entreprise,
private ?string $zone,
private ?string $application,
private string $login,
private string $password,
private int $tokenTtlSeconds,
private CacheItemPoolInterface $cachePool,
) {
}
public function getToken(): string
{
$cacheKey = $this->getCacheKey();
$item = $this->cachePool->getItem($cacheKey);
if ($item->isHit()) {
$token = $item->get();
if (is_string($token) && $token !== '') {
return $token;
}
}
$token = $this->createToken();
$item->set($token);
$item->expiresAfter($this->tokenTtlSeconds);
$this->cachePool->save($item);
return $token;
}
public function invalidateToken(): void
{
$this->cachePool->deleteItem($this->getCacheKey());
}
private function createToken(): string
{
$profil = array_filter([
'Entreprise' => $this->entreprise,
'Zone' => $this->zone,
'Application' => $this->application,
], static fn ($v) => $v !== null && $v !== '');
$payload = [
'Identification' => [
'UserId' => $this->login,
'Password' => $this->password,
'Profil' => $profil,
],
];
try {
/** @var object $response */
$response = $this->guichetClient->__soapCall('tkCreateIdentification', [$payload]);
} catch (SoapFault $e) {
throw new \RuntimeException('SOAP Fault lors de tkCreateIdentification: ' . $e->getMessage(), 0, $e);
}
$rs = $response->ReponseStandard ?? null;
$ok = is_object($rs) && (($rs->Resultat ?? false) === true);
if (!$ok) {
$anom = $rs->Anomalie ?? null;
$code = (string)($anom->Code ?? 'UNKNOWN');
$sev = (int)($anom->Severite ?? 1);
$msg = (string)($anom->Message ?? 'Authentification refusée');
throw new EdnotifException($code, $sev, $msg);
}
$token = $response->Jeton ?? null;
if (!is_string($token) || $token === '') {
throw new \RuntimeException('Guichet: réponse OK mais Jeton absent.');
}
return $token;
}
private function getCacheKey(): string
{
return 'ednotif.token.' . hash('sha256', $this->entreprise . '|' . $this->login);
}
}