ajoutUtilisateurs
This commit is contained in:
45
src/Infrastructure/Database.php
Normal file
45
src/Infrastructure/Database.php
Normal file
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace App\Infrastructure;
|
||||
|
||||
/**
|
||||
* Permet de se connecter à la base de données et de faire le pont
|
||||
* entre la classe PDO et nos actions.
|
||||
*/
|
||||
final class Database {
|
||||
|
||||
/**
|
||||
* Contient les informations de connexion à notre BDD.
|
||||
* @var array
|
||||
*/
|
||||
private array $databaseInfos;
|
||||
|
||||
/**
|
||||
* Instance PDO.
|
||||
* @var \PDO
|
||||
*/
|
||||
public private(set) \PDO $pdo;
|
||||
|
||||
/**
|
||||
* Constructeur, lance la connexion.
|
||||
* @param array $loginInfos ['name','host','port','user','pass']
|
||||
*/
|
||||
public function __construct( array $loginInfos ){
|
||||
$this->databaseInfos = $loginInfos;
|
||||
$this->tryLogin();
|
||||
}
|
||||
|
||||
/**
|
||||
* Permet d'essayer un login à la base de données.
|
||||
* @return void - never si la connexion échoue.
|
||||
*/
|
||||
private function tryLogin(): void {
|
||||
try {
|
||||
$dsn = 'mysql:dbname=' . $this->databaseInfos['name'] . ';host=' . $this->databaseInfos['host'] . ';port=' . $this->databaseInfos['port'];
|
||||
$this->pdo = new \PDO( $dsn, $this->databaseInfos['user'], $this->databaseInfos['pass'] );
|
||||
} catch ( \PDOException $e ) {
|
||||
die( $e->getMessage() );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user