chore (login) #43 : remaniement connexion sql, passage à 1 requête au lieu de 2 pour performances
This commit is contained in:
@ -8,7 +8,6 @@ use App\SQLLogin\Exception\LoginElementsConfigurationException;
|
||||
use App\SQLLogin\Exception\NullDataToFetchException;
|
||||
use App\SQLLogin\SQLLoginConnect;
|
||||
use App\SQLLogin\SQLLoginRequest;
|
||||
use Exception;
|
||||
use PDO;
|
||||
use PDOException;
|
||||
use Psr\Log\LoggerInterface;
|
||||
@ -16,86 +15,54 @@ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
|
||||
class SQLLoginService extends AbstractController
|
||||
{
|
||||
private SQLLoginRequest $sqlLoginRequest;
|
||||
private PDO $pdo;
|
||||
|
||||
public function __construct(SQLLoginRequest $sqlLoginRequest, private LoggerInterface $loggerInterface)
|
||||
{
|
||||
public function __construct(
|
||||
private SQLLoginRequest $sqlLoginRequest,
|
||||
private LoggerInterface $loggerInterface
|
||||
) {
|
||||
$this->sqlLoginRequest = $sqlLoginRequest;
|
||||
$this->loggerInterface = $loggerInterface;
|
||||
$this->pdo = $this->getConnection();
|
||||
}
|
||||
|
||||
public function fetchDatas(string $login): array
|
||||
public function fetchPasswordAndDatas(string $login): array
|
||||
{
|
||||
if (empty($login)) {
|
||||
throw new Exception('Connexion échouée, le login ne peut pas être vide');
|
||||
}
|
||||
try {
|
||||
$dbh = $this->getConnection();
|
||||
} catch (PDOException $e) {
|
||||
$this->loggerInterface->critical($e->getMessage());
|
||||
throw new DatabaseConnectionException($e->getMessage());
|
||||
}
|
||||
try {
|
||||
// forge de la requête
|
||||
$request = $this->sqlLoginRequest->getRequestScope();
|
||||
$dataRequest = $this->sqlLoginRequest->getDatasRequest();
|
||||
$datas = $this->executeRequestWithLogin($dataRequest, $login);
|
||||
} catch (NullDataToFetchException $e) {
|
||||
throw new DataToFetchConfigurationException($e->getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
// Préparation de la requête
|
||||
$query = $dbh->prepare($request);
|
||||
$query->execute([$this->sqlLoginRequest->getLoginColumnName() => $login]);
|
||||
$datas = $query->fetch(PDO::FETCH_ASSOC);
|
||||
$query->closeCursor();
|
||||
} catch (PDOException $e) {
|
||||
$this->loggerInterface->critical($e->getMessage());
|
||||
throw new DataToFetchConfigurationException($e->getMessage());
|
||||
}
|
||||
|
||||
if (false === $datas) {
|
||||
throw new Exception(sprintf('La requête sql "%s" a renvoyé un résultat incorrect.', $request));
|
||||
throw new LoginElementsConfigurationException($e->getMessage());
|
||||
}
|
||||
|
||||
return $datas;
|
||||
}
|
||||
|
||||
public function fetchPassword(string $login): array
|
||||
private function executeRequestWithLogin(string $request, string $login): array
|
||||
{
|
||||
try {
|
||||
$dbh = $this->getConnection();
|
||||
} catch (PDOException $e) {
|
||||
$this->loggerInterface->critical($e->getMessage());
|
||||
throw new DatabaseConnectionException($e->getMessage());
|
||||
}
|
||||
$query = $this->pdo->prepare($request);
|
||||
$query->bindParam($this->sqlLoginRequest->getLoginColumnName(), $login, PDO::PARAM_STR);
|
||||
$query->execute();
|
||||
$datas = $query->fetch(PDO::FETCH_ASSOC);
|
||||
$query->closeCursor();
|
||||
|
||||
// forge de la requête
|
||||
$request = $this->sqlLoginRequest->getRequestPassword();
|
||||
|
||||
try {
|
||||
$query = $dbh->prepare($request);
|
||||
$query->execute([$this->sqlLoginRequest->getLoginColumnName() => $login]);
|
||||
$password = $query->fetch(PDO::FETCH_ASSOC);
|
||||
$query->closeCursor();
|
||||
} catch (PDOException $e) {
|
||||
$this->loggerInterface->critical($e->getMessage());
|
||||
throw new LoginElementsConfigurationException($e->getMessage());
|
||||
}
|
||||
if (!$password) {
|
||||
throw new Exception('Une erreur est survenue lors de la récupération des données');
|
||||
}
|
||||
|
||||
return [
|
||||
$password[$this->sqlLoginRequest->getPasswordColumnName()],
|
||||
isset($password[$this->sqlLoginRequest->getSaltColumnName()]) ? $password[$this->sqlLoginRequest->getSaltColumnName()] : null,
|
||||
];
|
||||
return $datas;
|
||||
}
|
||||
|
||||
private function getConnection(): PDO
|
||||
{
|
||||
// Appel du singleton
|
||||
$sqlLogin = SQLLoginConnect::getInstance();
|
||||
// Connection bdd
|
||||
return $sqlLogin->connect($this->sqlLoginRequest->getDatabaseDsn(), $this->sqlLoginRequest->getDbUser(), $this->sqlLoginRequest->getDbPassword());
|
||||
try {
|
||||
$sqlLogin = SQLLoginConnect::getInstance();
|
||||
$pdo = $sqlLogin->connect($this->sqlLoginRequest->getDatabaseDsn(), $this->sqlLoginRequest->getDbUser(), $this->sqlLoginRequest->getDbPassword());
|
||||
} catch (PDOException $e) {
|
||||
$this->loggerInterface->critical($e->getMessage());
|
||||
throw new DatabaseConnectionException($e->getMessage());
|
||||
}
|
||||
|
||||
return $pdo;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user