2022-12-09 17:31:07 +01:00
|
|
|
<?php
|
|
|
|
|
2022-12-14 16:38:46 +01:00
|
|
|
namespace App\Service;
|
2022-12-09 17:31:07 +01:00
|
|
|
|
2024-10-10 12:01:15 +02:00
|
|
|
use App\SQLLogin\Exception\EmptyResultException;
|
2022-12-14 16:38:46 +01:00
|
|
|
use App\SQLLogin\SQLLoginConnect;
|
|
|
|
use App\SQLLogin\SQLLoginRequest;
|
2022-12-09 17:31:07 +01:00
|
|
|
use PDO;
|
2023-06-19 14:22:05 +02:00
|
|
|
use Psr\Log\LoggerInterface;
|
2022-12-09 17:31:07 +01:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
|
|
|
2022-12-14 16:38:46 +01:00
|
|
|
class SQLLoginService extends AbstractController
|
2022-12-09 17:31:07 +01:00
|
|
|
{
|
2024-10-10 12:01:15 +02:00
|
|
|
public const MAX_RETRY = 3;
|
2022-12-09 17:31:07 +01:00
|
|
|
|
2024-10-08 14:36:12 +02:00
|
|
|
public function __construct(
|
|
|
|
private SQLLoginRequest $sqlLoginRequest,
|
|
|
|
private LoggerInterface $loggerInterface
|
|
|
|
) {
|
2022-12-14 16:38:46 +01:00
|
|
|
$this->sqlLoginRequest = $sqlLoginRequest;
|
2023-06-19 14:22:05 +02:00
|
|
|
$this->loggerInterface = $loggerInterface;
|
2022-12-09 17:31:07 +01:00
|
|
|
}
|
|
|
|
|
2024-10-08 14:36:12 +02:00
|
|
|
public function fetchPasswordAndDatas(string $login): array
|
2022-12-09 17:31:07 +01:00
|
|
|
{
|
2024-10-11 15:06:32 +02:00
|
|
|
$dataRequest = $this->sqlLoginRequest->getDatasRequest();
|
2025-03-07 14:01:40 +01:00
|
|
|
$login = \strtolower($login);
|
2024-10-11 15:06:32 +02:00
|
|
|
$datas = $this->executeRequestWithLogin($dataRequest, $login);
|
2024-09-24 11:47:52 +02:00
|
|
|
|
2022-12-09 17:31:07 +01:00
|
|
|
return $datas;
|
|
|
|
}
|
|
|
|
|
2024-10-08 14:36:12 +02:00
|
|
|
private function executeRequestWithLogin(string $request, string $login): array
|
2022-12-09 17:31:07 +01:00
|
|
|
{
|
2024-10-10 12:01:15 +02:00
|
|
|
$attempt = 0;
|
|
|
|
while ($attempt < self::MAX_RETRY) {
|
2024-10-11 13:25:21 +02:00
|
|
|
$pdo = $this->getConnection();
|
|
|
|
$query = $pdo->prepare($request);
|
2024-10-10 12:01:15 +02:00
|
|
|
$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);
|
2024-10-10 12:01:15 +02:00
|
|
|
++$attempt;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (self::MAX_RETRY === $attempt) {
|
2024-10-10 16:32:46 +02:00
|
|
|
throw new EmptyResultException();
|
2024-10-10 12:01:15 +02:00
|
|
|
}
|
2023-06-19 15:56:55 +02:00
|
|
|
|
2024-10-08 14:36:12 +02:00
|
|
|
return $datas;
|
|
|
|
}
|
2023-06-19 15:56:55 +02:00
|
|
|
|
2024-10-08 14:36:12 +02:00
|
|
|
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
|
|
|
|
2024-10-08 14:36:12 +02:00
|
|
|
return $pdo;
|
2022-12-09 17:31:07 +01:00
|
|
|
}
|
|
|
|
}
|