Files
hydra-sql/src/Service/SQLLoginService.php
Gauthier DUPONT 2e5e1e72ae
Some checks are pending
Cadoles/hydra-sql/pipeline/pr-develop Build started...
chore(symfony) #57 : bump symfony to version 6.4 and fix deprecations
2025-07-30 11:37:06 +02:00

72 lines
2.0 KiB
PHP

<?php
namespace App\Service;
use App\SQLLogin\Exception\EmptyResultException;
use App\SQLLogin\SQLLoginConnect;
use App\SQLLogin\SQLLoginRequest;
use PDO;
use Psr\Log\LoggerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Security\Core\User\UserInterface;
class SQLLoginService extends AbstractController
{
public const MAX_RETRY = 3;
public function __construct(
private SQLLoginRequest $sqlLoginRequest,
private LoggerInterface $loggerInterface
) {
$this->sqlLoginRequest = $sqlLoginRequest;
$this->loggerInterface = $loggerInterface;
}
/**
* @return array<string,string>
*/
public function fetchPasswordAndDatas(string $login): array
{
$dataRequest = $this->sqlLoginRequest->getDatasRequest();
$login = \strtolower($login);
$datas = $this->executeRequestWithLogin($dataRequest, $login);
return $datas;
}
/**
* @return array<string,string>
*/
private function executeRequestWithLogin(string $request, string $login): array
{
$attempt = 0;
while ($attempt < self::MAX_RETRY) {
$pdo = $this->getConnection();
$query = $pdo->prepare($request);
$query->execute([$this->sqlLoginRequest->getLoginColumnName() => $login]);
$datas = $query->fetch(PDO::FETCH_ASSOC);
$query->closeCursor();
if (false === $datas) {
usleep(3000);
++$attempt;
} else {
break;
}
}
if (self::MAX_RETRY === $attempt) {
throw new EmptyResultException();
}
return $datas;
}
private function getConnection(): PDO
{
// Appel du singleton
$sqlLogin = SQLLoginConnect::getInstance();
$pdo = $sqlLogin->connect($this->sqlLoginRequest->getDatabaseDsn(), $this->sqlLoginRequest->getDbUser(), $this->sqlLoginRequest->getDbPassword());
return $pdo;
}
}