Completed
Push — master ( 80cb01...ea14c7 )
by Christopher
101:42 queued 86:29
created

Connection::connectSQL()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 11
Ratio 91.67 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 11
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
            default:
44
                self::$conn = $this->connectSQL($config);
45
        }
46
    }
47
48
    /**
49
     * Get an instance of the db connection
50
     * @return SQLite3 connection
51
     */
52
    public static function db()
53
    {
54
        return self::$conn;
55
    }
56
     public static function close()
57
     {
58
        self::$conn = null;
59
     }
60
61
    public function checkParam($config) {
62
        if (!isset($config['host']) || empty($config['host'])) {
63
            throw new PotatoException("You must provide a host to connect to database");
64
        }
65
66
        if (!isset($config['user']) || empty($config['user'])) {
67
            throw new PotatoException("You must provide a user for database");
68
        }
69
70
        if (!isset($config['password']) || empty($config['password'])) {
71
            throw new PotatoException("You must provide a password for  database");
72
        }
73
    }
74
75
    public function connectMySQL($config)
76
    {
77
        $this->checkParam($config);
78
79
        try {
80
            $db = new PDO("mysql:host=".$config['host'].";dbname=".$config['database'], $config['user'], $config['password']);
81
        } 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...
82
            throw new PotatoException($e->getMessage());
83
        }
84
85
        return $db;
86
    }
87
88
    public function connectSQLite($config)
89
    {
90
        try {
91
            $db =  new PDO("sqlite:".$config['database']);
92
        } 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...
93
            throw new PotatoException($e->getMessage());
94
        }
95
96
        return $db;
97
    }
98
99
    public function connectPostrges()
100
    {
101
102
    }
103
104 View Code Duplication
    public function connectMSSQL($config)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106
        $this->checkParam($config);
107
108
        try {
109
            $db = new PDO("mssql:host=".$config['host'].";dbname=".$config['database'].", ".$config['user'].", ".$config['password']);
110
        } 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...
111
            throw new PotatoException($e->getMessage());
112
        }
113
114
        return $db;
115
    }
116
117 View Code Duplication
    public function connectSQL($config)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
118
    {
119
        $this->checkParam($config);
120
121
        try {
122
            $db = new PDO($config['type'].":host=".$config['host'].";dbname=".$config['database'].", ".$config['user'].", ".$config['password']);
123
        } 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...
124
            throw new PotatoException($e->getMessage());
125
        }
126
127
        return $db;
128
    }
129
}
130