Files
hydra-sql/src/Service/SQLLoginService.php

64 lines
1.8 KiB
PHP
Raw Normal View History

<?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;
class SQLLoginService extends AbstractController
{
public const MAX_RETRY = 3;
public function __construct(
private SQLLoginRequest $sqlLoginRequest,
private LoggerInterface $loggerInterface
) {
$this->sqlLoginRequest = $sqlLoginRequest;
$this->loggerInterface = $loggerInterface;
}
public function fetchPasswordAndDatas(string $login): array
{
2024-10-11 15:06:32 +02:00
$dataRequest = $this->sqlLoginRequest->getDatasRequest();
$datas = $this->executeRequestWithLogin($dataRequest, $login);
2024-09-24 11:47:52 +02:00
return $datas;
}
private function executeRequestWithLogin(string $request, string $login): array
{
$attempt = 0;
while ($attempt < self::MAX_RETRY) {
2024-10-11 13:25:21 +02:00
$pdo = $this->getConnection();
$query = $pdo->prepare($request);
$query->execute([$this->sqlLoginRequest->getLoginColumnName() => $login]);
$datas = $query->fetch(PDO::FETCH_ASSOC);
$query->closeCursor();
if (false === $datas) {
2024-10-10 16:32:46 +02:00
usleep(3000);
++$attempt;
} else {
break;
}
}
if (self::MAX_RETRY === $attempt) {
2024-10-10 16:32:46 +02:00
throw new EmptyResultException();
}
return $datas;
}
private function getConnection(): PDO
{
// Appel du singleton
2024-10-11 13:25:21 +02:00
$sqlLogin = SQLLoginConnect::getInstance();
$pdo = $sqlLogin->connect($this->sqlLoginRequest->getDatabaseDsn(), $this->sqlLoginRequest->getDbUser(), $this->sqlLoginRequest->getDbPassword());
2024-09-24 11:47:52 +02:00
return $pdo;
}
}