fix login sql : ajout d'un retry sur le login, suppression des options
This commit is contained in:
parent
8e56433216
commit
d6d9e81df6
9
src/SQLLogin/Exception/EmptyResultException.php
Normal file
9
src/SQLLogin/Exception/EmptyResultException.php
Normal file
@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace App\SQLLogin\Exception;
|
||||
|
||||
use Exception;
|
||||
|
||||
class EmptyResultException extends Exception
|
||||
{
|
||||
}
|
@ -24,12 +24,6 @@ class SQLLoginConnect extends AbstractController
|
||||
|
||||
public function connect($urlDatabase, $dbUser, $dbPassword): PDO
|
||||
{
|
||||
$options = [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_TIMEOUT => 5,
|
||||
PDO::ATTR_PERSISTENT => false,
|
||||
];
|
||||
|
||||
return new PDO($urlDatabase, $dbUser, $dbPassword, $options);
|
||||
return new PDO($urlDatabase, $dbUser, $dbPassword);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user