84 lines
2.3 KiB
PHP
84 lines
2.3 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Services;
|
||
|
|
||
|
use App\Pdo\Exception\InvalidPasswordException;
|
||
|
use App\Pdo\PdoConnect;
|
||
|
use App\Pdo\PdoRequest;
|
||
|
use PDO;
|
||
|
use PDOException;
|
||
|
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) {
|
||
|
echo 'Erreur fetch Datas !: '.$e->getMessage().'<br/>';
|
||
|
exit();
|
||
|
}
|
||
|
|
||
|
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) {
|
||
|
echo 'Erreur fetch Password!: '.$e->getMessage().'<br/>';
|
||
|
exit();
|
||
|
}
|
||
|
|
||
|
return $password[$this->pdoRequest->getNamePassword()];
|
||
|
}
|
||
|
|
||
|
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();
|
||
|
}
|
||
|
}
|
||
|
}
|