Completed
Push — test ( 2ec062...3a08db )
by Temitope
02:38
created

DatabaseConnection   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 8
Bugs 0 Features 1
Metric Value
wmc 8
c 8
b 0
f 1
lcom 1
cbo 1
dl 0
loc 76
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 23 2
B getDatabaseDriver() 0 25 4
A loadEnv() 0 8 2
1
<?php
2
3
/**
4
 * @package  Laztopaz\potato-ORM
5
 * @author   Temitope Olotin <[email protected]>
6
 * @license  <https://opensource.org/license/MIT> MIT
7
 */
8
9
namespace Laztopaz\potatoORM;
10
11
use PDO;
12
use Dotenv\Dotenv;
13
use PDOException;
14
15
class DatabaseConnection extends \PDO
16
{
17
	private $databaseName;
18
    private $databaseHost;
19
	private $databaseDriver;
20
    private $databaseUsername;
21
	private $databasePassword;
22
23
	public  function  __construct()
24
	{
25
		$this->loadEnv(); // load the environment variables
26
27
		$this->databaseName     =  getenv('databaseName');
28
		$this->databaseHost     =  getenv('databaseHost');
29
		$this->databaseDriver   =  getenv('databaseDriver');
30
		$this->databaseUsername =  getenv('databaseUsername');
31
		$this->databasePassword =  getenv('databasePassword');
32
33
		try {
34
			$options = [
35
				PDO::ATTR_PERSISTENT    => true,
36
37
				PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION
38
			];
39
			parent::__construct($this->getDatabaseDriver(), $this->databaseUsername, $this->databasePassword, $options);
40
			} catch(PDOException $e) {
41
42
			return $e->getMessage();
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
43
		}
44
45
	}
46
47
	/**
48
	 * This method determines the driver to be used for appropriate database server
49
	 * @params void
50
	 * @return string dsn
51
	 */
52
	public function getDatabaseDriver()
53
	{
54
		$dsn = "";
0 ignored issues
show
Unused Code introduced by
$dsn is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
55
56
		switch ($this->databaseDriver)
57
		{
58
			case 'mysql':
59
				// Set DSN
60
				$dsn = 'mysql:host='.$this->databaseHost.';dbname='. $this->databaseName;
61
				break;
62
			case 'sqlite':
63
				// Set DSN
64
				$dsn = 'sqlite:host='.$this->databaseHost.';dbname='. $this->databaseName;
65
				break;
66
			case 'pgsql':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
67
68
				// Set DSN
69
				$dsn = 'pgsqlsql:host='.$this->databaseHost.';dbname='. $this->databaseName;
70
				break;
71
			default:
72
				// Set DSN
73
				$dsn = 'mysql:host='.$this->databaseHost.';dbname='. $this->databaseName;
74
		}
75
		return $dsn;
76
	}
77
78
	/**
79
	 * Load Dotenv to grant getenv() access to environment variables in .env file
80
	 */
81
	public function loadEnv()
82
	{
83
		if (!getenv("APP_ENV")) {
84
			$dotenv = new Dotenv(__DIR__.'/../../');
85
		    $dotenv->load();
86
		}
87
88
	}
89
90
}