2022-12-09 17:31:07 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use PDO;
|
|
|
|
use PDOException;
|
2022-12-13 15:46:24 +01:00
|
|
|
use App\Pdo\PdoConnect;
|
|
|
|
use App\Pdo\PdoRequest;
|
|
|
|
use Sentry\captureException;
|
|
|
|
use App\Pdo\Exception\InvalidPasswordException;
|
2022-12-09 17:31:07 +01:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
|
|
|
|
|
|
|
class PdoService extends AbstractController
|
|
|
|
{
|
|
|
|
private $params;
|
|
|
|
public PdoRequest $pdoRequest;
|
|
|
|
|
|
|
|
public function __construct(ParameterBagInterface $params, PdoRequest $pdoRequest)
|
|
|
|
{
|
|
|
|
$this->params = $params;
|
|
|
|
$this->pdoRequest = $pdoRequest;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fetchDatas($login)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$dbh = $this->getConnection();
|
|
|
|
|
|
|
|
// forge de la requête
|
|
|
|
$request = $this->pdoRequest->getRequestScope();
|
|
|
|
|
|
|
|
// Préparation de la requête
|
|
|
|
$query = $dbh->prepare($request);
|
|
|
|
$query->execute([$this->pdoRequest->getNameLogin() => $login]);
|
|
|
|
$datas = $query->fetch(PDO::FETCH_ASSOC);
|
|
|
|
} catch (PDOException $e) {
|
2022-12-13 15:46:24 +01:00
|
|
|
\Sentry\captureException($e);
|
|
|
|
|
|
|
|
throw new PDOException();
|
2022-12-09 17:31:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $datas;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $login
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function fetchPassword($login)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$dbh = $this->getConnection();
|
|
|
|
$request = $this->pdoRequest->getRequestLogin();
|
|
|
|
$query = $dbh->prepare($request);
|
|
|
|
$query->execute([$this->pdoRequest->getNameLogin() => $login]);
|
|
|
|
$password = $query->fetch(PDO::FETCH_ASSOC);
|
|
|
|
} catch (PDOException $e) {
|
2022-12-13 15:46:24 +01:00
|
|
|
\Sentry\captureException($e);
|
|
|
|
dd($e);
|
|
|
|
throw new PDOException();
|
|
|
|
}
|
|
|
|
if ($password) {
|
|
|
|
return $password[$this->pdoRequest->getNamePassword()];
|
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
|
|
|
}
|
|
|
|
|
|
|
|
public function getConnection()
|
|
|
|
{
|
|
|
|
// Appel du singleton
|
|
|
|
$pdo = PdoConnect::getInstance();
|
|
|
|
// Connection bdd
|
|
|
|
return $pdo->connect($this->pdoRequest->getDatabaseDsn(), $this->pdoRequest->getDbUser(), $this->pdoRequest->getDbPassword());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function verifyPassword($password, $hashedPassword)
|
|
|
|
{
|
|
|
|
$hashAlgo = $this->params->get('hashAlgo') ?? 'sha256';
|
|
|
|
|
|
|
|
if ($hashedPassword === hash($hashAlgo, $password)) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
throw new InvalidPasswordException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|