Skip to content

Commit

Permalink
Removed hardcoded sensitive values and used a .env file
Browse files Browse the repository at this point in the history
The database settings are now set using environment variables that
users can set themselves in the .env file.
  • Loading branch information
andela-aonyango committed Feb 25, 2016
1 parent 3a68fc9 commit 680dd97
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
8 changes: 8 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
DATABASE_TYPE=mysql
DATABASE_NAME=test

HOST=localhost
PORT=

USERNAME=potato
PASSWORD=potatopass
24 changes: 13 additions & 11 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,13 @@
namespace PotatoORM;

use PDO;
use Dotenv\Dotenv;

/**
* Class that assists in data persistence and retrieval
*/
class Database
{
private $host = "localhost";
private $username = "potato";
private $password = "potatopass";
private $database_name = "test";
private $database_type = "mysql";

private $dsn;
private $database_handler;
private $statement;
Expand All @@ -35,17 +30,24 @@ public function __construct()
//create a new pdo instance
$this->database_handler = new PDO(
$this->dsn,
$this->username,
$this->password,
getenv("USERNAME"),
getenv("PASSWORD"),
$options
);
}

/**
* Sets the DSN as set in the environment variable file
*/
private function setDSN()
{
$this->dsn = $this->database_type
. ":host=" . $this->host
. ";dbname=" . $this->database_name;
$env = new Dotenv(__DIR__ . "/../");
$env->load();

$this->dsn = getenv("DATABASE_TYPE")
. ":host=" . getenv("HOST")
. ";dbname=" . getenv("DATABASE_NAME")
. ";port=" . getenv("PORT");
}

/**
Expand Down

0 comments on commit 680dd97

Please sign in to comment.