Completed
Push — test ( 5bf343...41c779 )
by Temitope
03:44
created

DatabaseConnection::loadEnv()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
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
                PDO::ATTR_ERRMODE    => PDO::ERRMODE_EXCEPTION
37
            ];
38
39
        parent::__construct($this->getDatabaseDriver(), $this->databaseUsername, $this->databasePassword, $options);
40
            
41
        } catch(PDOException $e) {
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
    /**
49
     * This method determines the driver to be used for appropriate database server
50
     * @params void
51
     * @return string dsn
52
     */
53
     public function getDatabaseDriver()
54
     {
55
        $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...
56
57
        switch ($this->databaseDriver) {
58
            case 'mysql':
59
                $dsn = 'mysql:host='.$this->databaseHost.';dbname='.$this->databaseName; // Set DSN
60
                break;
61
            case 'sqlite':
62
                $dsn = 'sqlite:host='.$this->databaseHost.';dbname='.$this->databaseName;
63
                break;
64
            case 'pgsql':
65
                $dsn = 'pgsqlsql:host='.$this->databaseHost.';dbname='.$this->databaseName;
66
                break;
67
            default:
68
                $dsn = 'mysql:host='.$this->databaseHost.';dbname='.$this->databaseName;
69
                break;
70
        }
71
72
        return $dsn;
73
74
     }
75
76
     /**
77
      * Load Dotenv to grant getenv() access to environment variables in .env file
78
      */
79
     public function loadEnv()
80
     {
81
        if (! getenv("APP_ENV")) {
82
            $dotenv = new Dotenv(__DIR__.'/../../');
83
            $dotenv->load();
84
        }
85
        
86
     }
87
88
}
89