Completed
Push — develop ( ab0470...4a7a74 )
by Christopher
14:44
created

Connection::connectMySQL()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 12
rs 9.4286
cc 2
eloc 7
nc 2
nop 1
1
<?php
2
3
namespace Ganga\Potato;
4
5
use PDO;
6
/**
7
 * Class to define connection to sqlite database;
8
 */
9
class Connection
10
{
11
    protected static $conn;
12
    public static $dbType;
13
    public static $dbName;
14
15
    /**
16
     * Constructor
17
     * Declare an instance of SQLite class
18
     */
19
    public function __construct($config)
20
    {
21
        if (!is_array($config)) {
22
            throw new PotatoException("Database Config must be an array");
23
        }
24
25
        if (!isset($config['type']) || empty($config['type'])) {
26
            throw new PotatoException("You must specify a database type in the config");
27
        }
28
29
        if (!isset($config['database']) || empty($config['database'])) {
30
            throw new PotatoException("You must provide a database in the config");
31
        }
32
33
        self::$dbName = $config['database'];
34
        self::$dbType = strtolower($config['type']);
35
36
        switch (self::$dbType) {
37
            case 'sqlite':
38
                self::$conn = $this->connectSQLite($config);
39
                break;
40
            case 'mysql':
41
                self::$conn = $this->connectMySQL($config);
42
                break;
43
            case 'postgres':
44
            case 'pgssql':
45
46
                break;
47
            case 'mssql':
48
                self::$conn = $this->connectMSSQL($config);
49
                break;
50
        }
51
    }
52
53
    /**
54
     * Get an instance of the db connection
55
     * @return SQLite3 connection
56
     */
57
    public static function db()
58
    {
59
        return self::$conn;
60
    }
61
     public static function close()
62
     {
63
        self::$conn = null;
64
     }
65
66
    public function checkParam($config) {
67
        if (!isset($config['host']) || empty($config['host'])) {
68
            throw new PotatoException("You must provide a host to connect to database");
69
        }
70
71
        if (!isset($config['user']) || empty($config['user'])) {
72
            throw new PotatoException("You must provide a user for database");
73
        }
74
75
        if (!isset($config['password']) || empty($config['password'])) {
76
            throw new PotatoException("You must provide a password for  database");
77
        }
78
    }
79
80
    public function connectMySQL($config)
81
    {
82
        $this->checkParam($config);
83
84
        try {
85
            $db = new PDO("mysql:host=".$config['host'].";dbname=".$config['database'], $config['user'], $config['password']);
86
        } catch (PDOException $e) {
0 ignored issues
show
Bug introduced by
The class Ganga\Potato\PDOException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
87
            throw new PotatoException($e->getMessage());
88
        }
89
90
        return $db;
91
    }
92
93
    public function connectSQLite($config)
94
    {
95
        try {
96
            $db =  new PDO("sqlite:".$config['database']);
97
        } catch (PDOException $e) {
0 ignored issues
show
Bug introduced by
The class Ganga\Potato\PDOException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
98
            throw new PotatoException($e->getMessage());
99
        }
100
101
        return $db;
102
    }
103
104
    public function connectPostrges()
105
    {
106
107
    }
108
109
    public function connectMSSQL($config) {
110
        $this->checkParam($config);
111
112
        try {
113
            $db = new PDO("mssql:host=".$config['host'].";dbname=".$config['database'].", ".$config['user'].", ".$config['password']);
114
        } catch (PDOException $e) {
0 ignored issues
show
Bug introduced by
The class Ganga\Potato\PDOException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
115
            throw new PotatoException($e->getMessage());
116
        }
117
118
        return $db;
119
    }
120
}
121