From d6d9e81df682728973f2d40de01b72d45fdb4740 Mon Sep 17 00:00:00 2001 From: rudy Date: Thu, 10 Oct 2024 12:01:15 +0200 Subject: [PATCH] fix login sql : ajout d'un retry sur le login, suppression des options --- .../Exception/EmptyResultException.php | 9 ++++++++ src/SQLLogin/SQLLoginConnect.php | 8 +------ src/Service/SQLLoginService.php | 23 +++++++++++++++---- 3 files changed, 28 insertions(+), 12 deletions(-) create mode 100644 src/SQLLogin/Exception/EmptyResultException.php diff --git a/src/SQLLogin/Exception/EmptyResultException.php b/src/SQLLogin/Exception/EmptyResultException.php new file mode 100644 index 0000000..4ec746d --- /dev/null +++ b/src/SQLLogin/Exception/EmptyResultException.php @@ -0,0 +1,9 @@ + PDO::ERRMODE_EXCEPTION, - PDO::ATTR_TIMEOUT => 5, - PDO::ATTR_PERSISTENT => false, - ]; - - return new PDO($urlDatabase, $dbUser, $dbPassword, $options); + return new PDO($urlDatabase, $dbUser, $dbPassword); } } diff --git a/src/Service/SQLLoginService.php b/src/Service/SQLLoginService.php index b411823..493f5c2 100644 --- a/src/Service/SQLLoginService.php +++ b/src/Service/SQLLoginService.php @@ -4,6 +4,7 @@ namespace App\Service; use App\SQLLogin\Exception\DatabaseConnectionException; use App\SQLLogin\Exception\DataToFetchConfigurationException; +use App\SQLLogin\Exception\EmptyResultException; use App\SQLLogin\Exception\LoginElementsConfigurationException; use App\SQLLogin\Exception\NullDataToFetchException; use App\SQLLogin\SQLLoginConnect; @@ -15,6 +16,7 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; class SQLLoginService extends AbstractController { + public const MAX_RETRY = 3; private PDO $pdo; public function __construct( @@ -43,11 +45,22 @@ class SQLLoginService extends AbstractController private function executeRequestWithLogin(string $request, string $login): array { - $query = $this->pdo->prepare($request); - $query->bindParam($this->sqlLoginRequest->getLoginColumnName(), $login, PDO::PARAM_STR); - $query->execute(); - $datas = $query->fetch(PDO::FETCH_ASSOC); - $query->closeCursor(); + $attempt = 0; + while ($attempt < self::MAX_RETRY) { + $query = $this->pdo->prepare($request); + $query->execute([$this->sqlLoginRequest->getLoginColumnName() => $login]); + $datas = $query->fetch(PDO::FETCH_ASSOC); + $query->closeCursor(); + if (false === $datas) { + usleep(1000); + ++$attempt; + } else { + break; + } + } + if (self::MAX_RETRY === $attempt) { + throw new EmptyResultException('Résultat vide après 3 tentatives'); + } return $datas; }