Deprecated : Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 58
Deprecated : Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler
<?php
namespace App\Service\Auctionis ;
use App\Model\Auctionis\ErrorApiResponse ;
use App\Model\Auctionis\SuccessApiResponse ;
use DateTime ;
use Psr\Log\LoggerInterface ;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface ;
use Symfony\Component\HttpFoundation\Cookie ;
use Symfony\Component\HttpFoundation\RequestStack ;
use Symfony\Component\Routing\RouterInterface ;
use Symfony\Component\Serializer\SerializerInterface ;
use Symfony\Contracts\HttpClient\HttpClientInterface ;
use Symfony\Contracts\HttpClient\ResponseInterface ;
class AuctionisService
{
private ?string $jwt ;
public function __construct (
private RequestStack $requestStack ,
private HttpClientInterface $auctionisClient ,
private LoggerInterface $auctionisLogger ,
private RouterInterface $router ,
private SerializerInterface $serializer ,
private ParameterBagInterface $params
)
{
$this -> jwt = $requestStack -> getCurrentRequest ()?-> cookies -> get ( 'auctionisJwt' );
}
public function login (): string
{
$params = [
'POST' , 'login_check' , [
'json' => [
'username' => 'bo.npcj' ,
'password' => 'bo.npcj' ,
]
],
];
$response = $this -> auctionisClient -> request (... $params );
$apiResponse = $this -> handleResponse ( $response , $params );
return $apiResponse -> getData ()[ 'token' ];
}
public function createTokenCookie ( string $token ): Cookie
{
return Cookie :: create ( 'auctionisJwt' , $token );
}
public function tokenIsExpired (): bool
{
if (!$this -> jwt ) {
return true ;
}
return new DateTime () > (new DateTime ())-> setTimestamp ( $this -> tokenDecode ( $this -> jwt )-> exp );
}
public function getToken (): ? string
{
return $this -> jwt ;
}
private function tokenDecode ( $token )
{
list($header , $payload ) = explode ( '.' , $token );
return json_decode ( base64_decode ( $payload ));
}
public function request ( string $method , string $path , array $body = null , array $query = null , string $dto = null , string $errorRedirectRoute = 'app_security_login' , array $errorRedirectRouteParameters = []): mixed
{
if ($this -> tokenIsExpired ()) {
$this -> jwt = null ;
}
if ($path === 'login_buyer_folder' ) {
$this -> jwt = null ;
}
if (!$this -> jwt ) {
$token = $this -> login ();
$this -> jwt = $token ;
}
$params = [
$method , $path , [
'query' => $query ,
'json' => $body ,
'headers' => [
'Content-Type' => $method === 'PATCH' ? 'application/merge-patch+json' : 'application/json' ,
'Authorization' => sprintf ( 'Bearer %s' , $this -> jwt ),
],
],
];
$response = $this -> auctionisClient -> request (... $params );
$apiResponse = $this -> handleResponse ( $response , $params );
if ($apiResponse instanceof ErrorApiResponse ) {
if ($apiResponse -> getStatusCode () == 401 && $apiResponse -> getMessage () == "JWT Token not found" ) {
$this -> requestStack -> getSession ()-> getFlashBag ()-> add ( 'error' , 'Vous devez être connecté pour accéder à cette page' );
} else {
$this -> requestStack -> getSession ()-> getFlashBag ()-> add ( 'error' , $apiResponse -> getMessage ());
}
}
if (empty($apiResponse -> getData ())) {
return null ;
}
$data = $apiResponse -> getData ();
if (!$dto ) {
return $data ?? $apiResponse ;
}
if (str_ends_with ( $dto , '[]' )) {
$data = $data [ 'hydra:member' ];
}
return $this -> serializer -> deserialize ( json_encode ( $data ), $dto , 'json' );
}
private function handleResponse ( ResponseInterface $response , array $params ): ErrorApiResponse | SuccessApiResponse
{
if ($response -> getStatusCode () >= 500 ) {
$headers = $response -> getHeaders ( false );
$debugLink = array_key_exists ( 'x-debug-token-link' , $headers ) ? $headers [ 'x-debug-token-link' ][ 0 ] : '' ;
$this -> auctionisLogger -> error ( sprintf ( 'Auctionis API server error (%s): %s' , $debugLink , $response -> getContent ( false )), [
'status_code' => $response -> getStatusCode (),
'response_headers' => $headers ,
'params' => $params ,
]);
$message = 'error.server_error' ;
if ($this -> params -> get ( 'env' ) === 'dev' ) {
$message .= ' - ' . $debugLink ;
}
return new ErrorApiResponse ( $response -> getStatusCode (), message : $message );
}
if ($response -> getStatusCode () >= 400 ) {
$headers = $response -> getHeaders ( false );
$debugLink = array_key_exists ( 'x-debug-token-link' , $headers ) ? $headers [ 'x-debug-token-link' ][ 0 ] : '' ;
// Do not log 401 errors (JWT Token not found / Invalid credentials)
if ( $response -> getStatusCode () !== 401 ) {
$this -> auctionisLogger -> error ( sprintf ( 'Auctionis API client error (%s): %s' , $debugLink , $response -> getContent ( false )), [
'status_code' => $response -> getStatusCode (),
'response_headers' => $headers ,
'params' => $params ,
]);
}
$data = $response -> toArray ( false );
if (array_key_exists ( 'message' , $data )) {
$message = $data [ 'message' ];
} elseif (array_key_exists ( 'hydra:description' , $data )) {
$message = $data [ 'hydra:description' ];
} else {
$message = 'error.client_error' ;
}
return new ErrorApiResponse ( $response -> getStatusCode (), $message , $data );
}
if ($response -> getStatusCode () >= 300 ) {
return new ErrorApiResponse ( $response -> getStatusCode (), 'error.server_error' );
}
return new SuccessApiResponse ( $response -> getStatusCode (), 'success' , $response -> toArray ( false ));
}
}