Merge pull request 'chore (fetch datas) #39 : fermeture connexion sql après login$' (#40) from issue-39 into develop
Cadoles/hydra-sql/pipeline/head This commit was not built Details

Reviewed-on: #40
This commit is contained in:
Matthieu Lamalle 2024-09-24 10:37:15 +02:00
commit 27f957124b
2 changed files with 10 additions and 4 deletions

View File

@ -13,9 +13,8 @@ class SQLLoginConnect extends AbstractController
* Méthode qui crée l'unique instance de la classe * Méthode qui crée l'unique instance de la classe
* si elle n'existe pas encore puis la retourne. * si elle n'existe pas encore puis la retourne.
* *
* @return SQLLoginConnect
*/ */
public static function getInstance() public static function getInstance(): SQLLoginConnect
{ {
if (is_null(self::$_instance)) { if (is_null(self::$_instance)) {
self::$_instance = new SQLLoginConnect(); self::$_instance = new SQLLoginConnect();
@ -24,7 +23,7 @@ class SQLLoginConnect extends AbstractController
return self::$_instance; return self::$_instance;
} }
public function connect($urlDatabase, $dbUser, $dbPassword) public function connect($urlDatabase, $dbUser, $dbPassword): PDO
{ {
return new PDO($urlDatabase, $dbUser, $dbPassword); return new PDO($urlDatabase, $dbUser, $dbPassword);
} }

View File

@ -26,6 +26,9 @@ class SQLLoginService extends AbstractController
public function fetchDatas(string $login): array public function fetchDatas(string $login): array
{ {
if (empty($login)) {
throw new Exception('Connexion échouée, le login ne peut pas être vide');
}
try { try {
$dbh = $this->getConnection(); $dbh = $this->getConnection();
} catch (PDOException $e) { } catch (PDOException $e) {
@ -44,11 +47,15 @@ class SQLLoginService extends AbstractController
$query = $dbh->prepare($request); $query = $dbh->prepare($request);
$query->execute([$this->sqlLoginRequest->getLoginColumnName() => $login]); $query->execute([$this->sqlLoginRequest->getLoginColumnName() => $login]);
$datas = $query->fetch(PDO::FETCH_ASSOC); $datas = $query->fetch(PDO::FETCH_ASSOC);
$query->closeCursor();
} catch (PDOException $e) { } catch (PDOException $e) {
$this->loggerInterface->critical($e->getMessage()); $this->loggerInterface->critical($e->getMessage());
throw new DataToFetchConfigurationException($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));
}
return $datas; return $datas;
} }
@ -68,6 +75,7 @@ class SQLLoginService extends AbstractController
$query = $dbh->prepare($request); $query = $dbh->prepare($request);
$query->execute([$this->sqlLoginRequest->getLoginColumnName() => $login]); $query->execute([$this->sqlLoginRequest->getLoginColumnName() => $login]);
$password = $query->fetch(PDO::FETCH_ASSOC); $password = $query->fetch(PDO::FETCH_ASSOC);
$query->closeCursor();
} catch (PDOException $e) { } catch (PDOException $e) {
$this->loggerInterface->critical($e->getMessage()); $this->loggerInterface->critical($e->getMessage());
throw new LoginElementsConfigurationException($e->getMessage()); throw new LoginElementsConfigurationException($e->getMessage());
@ -75,7 +83,6 @@ class SQLLoginService extends AbstractController
if (!$password) { if (!$password) {
throw new Exception('Une erreur est survenue lors de la récupération des données'); throw new Exception('Une erreur est survenue lors de la récupération des données');
} }
return [ return [
$password[$this->sqlLoginRequest->getPasswordColumnName()], $password[$this->sqlLoginRequest->getPasswordColumnName()],
isset($password[$this->sqlLoginRequest->getSaltColumnName()]) ? $password[$this->sqlLoginRequest->getSaltColumnName()] : null, isset($password[$this->sqlLoginRequest->getSaltColumnName()]) ? $password[$this->sqlLoginRequest->getSaltColumnName()] : null,