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
|
|
|
|
2023-06-19 15:56:55 +02:00
|
|
|
use App\SQLLogin\Exception\DatabaseConnectionException;
|
|
|
|
use App\SQLLogin\Exception\DataToFetchConfigurationException;
|
|
|
|
use App\SQLLogin\Exception\LoginElementsConfigurationException;
|
|
|
|
use App\SQLLogin\Exception\NullDataToFetchException;
|
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;
|
|
|
|
use PDOException;
|
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
|
|
|
{
|
2022-12-14 16:38:46 +01:00
|
|
|
public SQLLoginRequest $sqlLoginRequest;
|
2022-12-09 17:31:07 +01:00
|
|
|
|
2023-06-19 14:22:05 +02:00
|
|
|
public function __construct(SQLLoginRequest $sqlLoginRequest, private LoggerInterface $loggerInterface)
|
2022-12-09 17:31:07 +01:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2023-06-19 14:22:05 +02:00
|
|
|
public function fetchDatas(string $login): array
|
2022-12-09 17:31:07 +01:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
$dbh = $this->getConnection();
|
2023-06-19 15:56:55 +02:00
|
|
|
} catch (PDOException $e) {
|
|
|
|
$this->loggerInterface->critical($e->getMessage());
|
|
|
|
throw new DatabaseConnectionException($e->getMessage());
|
|
|
|
}
|
|
|
|
try {
|
2022-12-09 17:31:07 +01:00
|
|
|
// forge de la requête
|
2022-12-14 16:38:46 +01:00
|
|
|
$request = $this->sqlLoginRequest->getRequestScope();
|
2023-06-19 15:56:55 +02:00
|
|
|
} catch (NullDataToFetchException $e) {
|
|
|
|
throw new DataToFetchConfigurationException($e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-12-09 17:31:07 +01:00
|
|
|
// Préparation de la requête
|
|
|
|
$query = $dbh->prepare($request);
|
2022-12-14 16:38:46 +01:00
|
|
|
$query->execute([$this->sqlLoginRequest->getLoginColumnName() => $login]);
|
2022-12-09 17:31:07 +01:00
|
|
|
$datas = $query->fetch(PDO::FETCH_ASSOC);
|
|
|
|
} catch (PDOException $e) {
|
2023-06-19 14:22:05 +02:00
|
|
|
$this->loggerInterface->critical($e->getMessage());
|
2023-06-19 15:56:55 +02:00
|
|
|
throw new DataToFetchConfigurationException($e->getMessage());
|
2022-12-09 17:31:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $datas;
|
|
|
|
}
|
|
|
|
|
2024-04-29 10:44:38 +02:00
|
|
|
public function fetchPassword($login): array|bool
|
2022-12-09 17:31:07 +01:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
$dbh = $this->getConnection();
|
2023-06-19 15:56:55 +02:00
|
|
|
} catch (PDOException $e) {
|
|
|
|
$this->loggerInterface->critical($e->getMessage());
|
|
|
|
throw new DatabaseConnectionException($e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
// forge de la requête
|
|
|
|
$request = $this->sqlLoginRequest->getRequestPassword();
|
|
|
|
|
|
|
|
try {
|
2022-12-09 17:31:07 +01:00
|
|
|
$query = $dbh->prepare($request);
|
2022-12-14 16:38:46 +01:00
|
|
|
$query->execute([$this->sqlLoginRequest->getLoginColumnName() => $login]);
|
2022-12-09 17:31:07 +01:00
|
|
|
$password = $query->fetch(PDO::FETCH_ASSOC);
|
|
|
|
} catch (PDOException $e) {
|
2023-06-19 14:22:05 +02:00
|
|
|
$this->loggerInterface->critical($e->getMessage());
|
2023-06-19 15:56:55 +02:00
|
|
|
throw new LoginElementsConfigurationException($e->getMessage());
|
2022-12-13 15:46:24 +01:00
|
|
|
}
|
|
|
|
if ($password) {
|
2022-12-14 16:38:46 +01:00
|
|
|
return [
|
2022-12-16 15:00:14 +01:00
|
|
|
$password[$this->sqlLoginRequest->getPasswordColumnName()],
|
2022-12-14 16:38:46 +01:00
|
|
|
isset($password[$this->sqlLoginRequest->getSaltColumnName()]) ? $password[$this->sqlLoginRequest->getSaltColumnName()] : null,
|
2024-04-18 17:13:42 +02:00
|
|
|
];
|
2022-12-09 17:31:07 +01:00
|
|
|
}
|
|
|
|
|
2022-12-13 15:46:24 +01:00
|
|
|
return false;
|
2022-12-09 17:31:07 +01:00
|
|
|
}
|
|
|
|
|
2023-06-19 14:22:05 +02:00
|
|
|
public function getConnection(): PDO
|
2022-12-09 17:31:07 +01:00
|
|
|
{
|
|
|
|
// Appel du singleton
|
2022-12-16 15:00:14 +01:00
|
|
|
$sqlLogin = SQLLoginConnect::getInstance();
|
2022-12-09 17:31:07 +01:00
|
|
|
// Connection bdd
|
2022-12-16 15:00:14 +01:00
|
|
|
return $sqlLogin->connect($this->sqlLoginRequest->getDatabaseDsn(), $this->sqlLoginRequest->getDbUser(), $this->sqlLoginRequest->getDbPassword());
|
2022-12-09 17:31:07 +01:00
|
|
|
}
|
|
|
|
}
|