Completed
Push — test ( 48685e...bfbcaf )
by Temitope
02:34
created

DatabaseConnection   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 33 2
B getDatabaseDriver() 0 27 4
A loadEnv() 0 10 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
	private $databaseHandle;
0 ignored issues
show
Unused Code introduced by
The property $databaseHandle is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
23
24
	public  function  __construct()
25
	{
26
		self::loadEnv(); // load the environment variables
27
28
		//$this->databaseHandle   = $this->connect(); // database connection handle
0 ignored issues
show
Unused Code Comprehensibility introduced by
54% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
29
30
		$this->databaseName     =  getenv('databaseName');
31
		$this->databaseHost     =  getenv('databaseHost');
32
		$this->databaseDriver   =  getenv('databaseDriver');
33
		$this->databaseUsername =  getenv('databaseUsername');
34
		$this->databasePassword =  getenv('databasePassword');
35
36
		try {
37
38
			$options = [
39
40
				PDO::ATTR_PERSISTENT    => true,
41
42
				PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION
43
			];
44
45
			parent::__construct($this->getDatabaseDriver(), $this->databaseUsername, $this->databasePassword, $options);
46
//			$databaseHandle = new PDO($this->getDatabaseDriver(), $this->databaseUsername, $this->databasePassword, $options);
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
47
48
		} catch(PDOException $e) {
49
50
			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...
51
52
		}
53
54
55
56
	}
57
58
	/**
59
	 * This method connects the specified database chosen by the user
60
	 * @params void
61
	 * @return boolean true or false
62
	 */
63
//	public function connect()
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
64
//	{
65
//		try {
66
//
67
//			$options = [
68
//
69
//				PDO::ATTR_PERSISTENT    => true,
70
//
71
//				PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION
72
//			];
73
//
74
//			$databaseHandle = new PDO($this->getDatabaseDriver(), $this->databaseUsername, $this->databasePassword, $options);
75
//
76
//		} catch(PDOException $e) {
77
//
78
//			return $e->getMessage();
79
//
80
//		}
81
//
82
//		return $databaseHandle;
83
//	}
84
85
86
	/**
87
	 * This method determines the driver to be used for appropriate database server
88
	 * @params void
89
	 * @return string dsn
90
	 */
91
	public function getDatabaseDriver()
92
	{
93
		$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...
94
95
		switch ($this->databaseDriver)
96
		{
97
			case 'mysql':
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...
98
99
				// Set DSN
100
				$dsn = 'mysql:host='.    $this->databaseHost. ';dbname='. $this->databaseName;
101
				break;
102
			case 'sqlite':
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...
103
104
				// Set DSN
105
				$dsn = 'sqlite:host='.   $this->databaseHost. ';dbname='. $this->databaseName;
106
				break;
107
			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...
108
109
				// Set DSN
110
				$dsn = 'pgsqlsql:host='. $this->databaseHost. ';dbname='. $this->databaseName;
111
				break;
112
			default:
113
				// Set DSN
114
				$dsn = 'mysql:host='.    $this->databaseHost. ';dbname='. $this->databaseName;
115
		}
116
		return $dsn;
117
	}
118
119
	/**
120
	 * Load Dotenv to grant getenv() access to environment variables in .env file
121
	 */
122
	public function loadEnv()
123
	{
124
		if (!getenv("APP_ENV")) {
125
126
			//$dotenv = new Dotenv($_SERVER['DOCUMENT_ROOT']);
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
127
			$dotenv = new Dotenv(__DIR__.'/../../');
128
		    $dotenv->load();
129
		}
130
131
	}
132
133
}