DatabaseConnection::connect()   B
last analyzed

Complexity

Conditions 3
Paths 7

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 30
rs 8.8571
cc 3
eloc 16
nc 7
nop 0
1
<?php
2
3
namespace Potato\Database;
4
5
use PDO;
6
use PDOException;
7
8
/**
9
 * Class configures connection to the database
10
 *
11
 * Loads database details from .env, configures DSN details and returns a PDO connection
12
 *
13
 */
14
class DatabaseConnection
15
{
16
    /**
17
     * Associative array of Database configuratioon settings
18
     *
19
     * @var array
20
     */
21
    protected static $dbConfig = [];
22
23
    /**
24
     * PDO connection
25
     *
26
     * @var object
27
     */
28
    protected static $connection;
29
30
    /**
31
     * Constructor
32
     *
33
     * Load .env package by https://github.com/vlucas/phpdotenv
34
     *
35
     * Populate $dbConfig class variable with database connection settings
36
     */
37
    public function __construct()
38
    {
39
        $dotenv = new \Dotenv\Dotenv(__DIR__ . "/..");
40
        $dotenv->load();
41
42
        self::$dbConfig = [
43
            "driver" => getenv('DB_DRIVER'),
44
            "username" => getenv('DB_USERNAME'),
45
            "password" => getenv('DB_PASSWORD'),
46
            "database" => getenv('DB_NAME'),
47
            "host" => getenv('DB_HOST'),
48
            "port" => getenv('DB_PORT')
49
        ];
50
51
    }
52
53
    /**
54
     * Fetch database connection details
55
     * and return a PDO connection or throw error
56
     *
57
     * @return object PDO Object
58
     */
59
    public static function connect()
60
    {
61
        new static;
62
        self::configDsn();
63
64
        try {
65
66
            if (self::$dbConfig["driver"] === "sqlite") {
67
                self::$connection = new PDO(
68
                    self::$dbConfig["dsn"]
69
                );
70
            } else {
71
72
                self::$connection = new PDO(
73
                    self::$dbConfig["dsn"],
74
                    self::$dbConfig["username"],
75
                    self::$dbConfig["password"]
76
                );
77
78
            }
79
80
            self::$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
81
82
        } catch (PDOException $e) {
83
84
            echo $e->getMessage();
85
        }
86
87
        return self::$connection;
88
    }
89
90
    /**
91
     * Terminate database connection
92
     *
93
     * @return null close PDO DATABASE connection
94
     */
95
    public static function close()
96
    {
97
        self::$connection = null;
98
        return null;
99
    }
100
101
    /**
102
     * construct PDO dsn string
103
     *
104
     * @return void
105
     */
106
    public static function configDsn()
107
    {
108
        switch (self::$dbConfig["driver"]) {
109
110
            case "mysql":
111
                $dsn = "mysql:host=" . self::$dbConfig['host'];
112
                $dsn .= ";dbname=" . self::$dbConfig['database'];
113
                break;
114
115
            case "pgsql":
116
                $dsn = "pgsql:host=" . self::$dbConfig['host'];
117
                $dsn .= ";port=". self::$dbConfig['port'] .";dbname=" . self::$dbConfig['database'];
118
                break;
119
            case "sqlite":
120
                $dsn = "sqlite:" . self::$dbConfig["database"];
121
                break;
122
            default:
123
                $dsn = "sqlite:" . self::$dbConfig["database"];
124
                break;
125
        }
126
127
        self::$dbConfig["dsn"] = $dsn;
128
    }
129
}
130