DBConfig::loadEnv()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
/**
3
 * This class handles database connection.
4
 *
5
 * @package Ibonly\PotatoORM\DBConfig
6
 * @author  Ibraheem ADENIYI <[email protected]>
7
 * @license MIT <https://opensource.org/licenses/MIT>
8
 */
9
10
namespace Ibonly\PotatoORM;
11
12
use PDO;
13
use PDOException;
14
use Dotenv\Dotenv;
15
use Ibonly\PotatoORM\Inflector;
16
17
class DBConfig extends PDO
18
{
19
    protected $driver;
20
    protected $host;
21
    protected $dbname;
22
    protected $port;
23
    protected $user;
24
    protected $password;
25
    protected $sqlitePath;
26
27
    /**
28
     * Define the database connection
29
     */
30
    public function __construct()
31
    {
32
        $this->loadEnv();
33
        $this->driver     = getenv('DATABASE_DRIVER');
34
        $this->host       = getenv('DATABASE_HOST');
35
        $this->dbname     = getenv('DATABASE_NAME');
36
        $this->port       = getenv('DATABASE_PORT');
37
        $this->user       = getenv('DATABASE_USER');
38
        $this->password   = getenv('DATABASE_PASSWORD');
39
        $this->sqlitePath = getenv('SQLITE_PATH');
40
        try
41
        {
42
            if ($this->driver === 'pgsql' || $this->driver === 'postgres')
43
            {
44
                parent::__construct($this->pgsqlConnectionString());
45
            }
46
            elseif ($this->driver === 'mysql')
47
            {
48
                parent::__construct($this->mysqlConnectionString(), $this->user, $this->password);
49
            }
50
            elseif($this->driver === 'sqlite')
51
            {
52
                parent::__construct($this->sqlitConnectionString());
53
            }
54
        } catch (PDOException $e) {
55
            $e->getMessage();
56
        }
57
    }
58
59
    /**
60
     * pgsqlConnectionString Postgres connection string
61
     *
62
     * @return string
63
     */
64
    protected function pgsqlConnectionString()
65
    {
66
        return $this->driver . ':host=' . $this->host . ';port=' . $this->port . ';dbname=' . $this->dbname . ';user=' . $this->user . ';password=' . $this->password;
67
    }
68
69
    /**
70
     * mysqlConnectionString Mysql connection string
71
     *
72
     * @return string
73
     */
74
    protected function mysqlConnectionString()
75
    {
76
        return $this->driver . ':host=' . $this->host . ';dbname=' . $this->dbname . ';charset=utf8mb4';
77
    }
78
79
    /**
80
     * sqliteConnectionString Sqlite connection string
81
     *
82
     * @return string
83
     */
84
    protected function sqlitConnectionString()
85
    {
86
        return $this->driver . ':' . $this->sqlitePath;
87
    }
88
89
    /**
90
     * Load Dotenv to grant getenv() access to environment variables in .env file
91
     */
92
    protected function loadEnv()
0 ignored issues
show
Coding Style introduced by
loadEnv uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
93
    {
94
        if( ! getenv("APP_ENV"))
95
        {
96
            $dotenv = new Dotenv($_SERVER['DOCUMENT_ROOT']);
97
            $dotenv->load();
98
        }
99
    }
100
}