Completed
Pull Request — test (#1)
by Temitope
02:52
created

DatabaseConnection::getDatabaseDriver()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 21
rs 9.0534
cc 4
eloc 16
nc 4
nop 0
1
<?php
2
3
/**
4
 * @author   Temitope Olotin <[email protected]>
5
 * @license  <https://opensource.org/license/MIT> MIT
6
 */
7
namespace Laztopaz\PotatoORM;
8
9
use Dotenv\Dotenv;
10
use PDO;
11
use PDOException;
12
13
class DatabaseConnection extends PDO
14
{
15
    private $databaseName;
16
    private $databaseHost;
17
    private $databaseDriver;
18
    private $databaseUsername;
19
    private $databasePassword;
20
21
    public function __construct()
22
    {
23
        $this->loadEnv(); // load the environment variables
24
25
        $this->databaseName = getenv('databaseName');
26
        $this->databaseHost = getenv('databaseHost');
27
        $this->databaseDriver = getenv('databaseDriver');
28
        $this->databaseUsername = getenv('databaseUsername');
29
        $this->databasePassword = getenv('databasePassword');
30
31
        try {
32
            $options = [
33
                PDO::ATTR_PERSISTENT => true,
34
                PDO::ATTR_ERRMODE    => PDO::ERRMODE_EXCEPTION,
35
            ];
36
37
            parent::__construct($this->getDatabaseDriver(), $this->databaseUsername, $this->databasePassword, $options);
38
        } catch (PDOException $e) {
39
            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...
40
        }
41
    }
42
43
     /**
44
      * This method determines the driver to be used for appropriate database server.
45
      *
46
      * @params void
47
      *
48
      * @return string dsn
49
      */
50
     public function getDatabaseDriver()
51
     {
52
         $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...
53
54
         switch ($this->databaseDriver) {
55
            case 'mysql':
56
                $dsn = 'mysql:host='.$this->databaseHost.';dbname='.$this->databaseName; // Set DSN
57
                break;
58
            case 'sqlite':
59
                $dsn = 'sqlite:host='.$this->databaseHost.';dbname='.$this->databaseName;
60
                break;
61
            case 'pgsql':
62
                $dsn = 'pgsqlsql:host='.$this->databaseHost.';dbname='.$this->databaseName;
63
                break;
64
            default:
65
                $dsn = 'mysql:host='.$this->databaseHost.';dbname='.$this->databaseName;
66
                break;
67
        }
68
69
         return $dsn;
70
     }
71
72
     /**
73
      * Load Dotenv to grant getenv() access to environment variables in .env file.
74
      */
75
     public function loadEnv()
76
     {
77
         if (!getenv('APP_ENV')) {
78
             $dotenv = new Dotenv(__DIR__.'/../../');
79
             $dotenv->load();
80
         }
81
     }
82
}
83