Connection::close()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Ganga\Potato;
4
5
use PDO;
6
7
/**
8
 * Class to define connection to sqlite database;
9
 */
10
class Connection
11
{
12
    protected static $conn;
13
    public static $dbType;
14
    public static $dbName;
15
16
    /**
17
     * Constructor
18
     * Declare an instance of SQLite class
19
     */
20
    public function __construct($config)
21
    {
22
        if (! is_array($config)) {
23
            throw new PotatoException("Database Config must be an array");
24
        }
25
26
        if (! array_key_exists('type', $config) || empty($config['type'])) {
27
            throw new PotatoException("You must specify a database type in the config");
28
        }
29
30
        if (! array_key_exists('database', $config) || empty($config['database'])) {
31
            throw new PotatoException("You must provide a database in the config");
32
        }
33
34
        self::$dbName = $config['database'];
35
        self::$dbType = strtolower($config['type']);
36
37
        switch (self::$dbType) {
38
            case 'sqlite':
39
                self::$conn = $this->connectSQLite($config);
40
                break;
41
            case 'mssql':
42
                self::$conn = $this->connectMSSQL($config);
43
                break;
44
            default:
45
                self::$conn = $this->connectSQL($config);
46
        }
47
    }
48
49
    /**
50
     * Get an instance of the db connection
51
     * @return SQLite3 connection
52
     */
53
    public static function db()
54
    {
55
        self::$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
56
        return self::$conn;
57
    }
58
59
    public static function close()
60
    {
61
        self::$conn = null;
62
    }
63
64
    private function checkParam($config)
65
    {
66
        if (! array_key_exists('host', $config) || empty($config['host'])) {
67
            throw new PotatoException("You must provide a host to connect to database");
68
        }
69
70
        if (! array_key_exists('user', $config) || empty($config['user'])) {
71
            throw new PotatoException("You must provide a user for database");
72
        }
73
74
        if (! array_key_exists('password', $config)) {
75
            throw new PotatoException("You must provide a password for  database");
76
        }
77
    }
78
79
    public function connectSQLite($config)
80
    {
81
        try {
82
            $db =  new PDO("sqlite:".$config['database']);
83
        } 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...
84
            throw new PotatoException($e->getMessage());
85
        }
86
87
        return $db;
88
    }
89
90
    public function connectMSSQL($config)
91
    {
92
        $this->checkParam($config);
93
94
        try {
95
            $db = new PDO("mssql:host=".$config['host'].";dbname=".$config['database'].", ".$config['user'].", ".$config['password']);
96
        } 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...
97
            throw new PotatoException($e->getMessage());
98
        }
99
100
        return $db;
101
    }
102
103
    public function connectSQL($config)
104
    {
105
        $this->checkParam($config);
106
107
        try {
108
            $db = new PDO($config['type'].":host=".$config['host'].$this->checkPort($config).";dbname=".$config['database'], $config['user'], $config['password']);
109
        } 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...
110
            throw new PotatoException($e->getMessage());
111
        }
112
113
        return $db;
114
    }
115
116
    /**
117
     * Check if PORT is provided in the database config
118
     * @param  array $config database configuration array
119
     * @return void
120
     */
121
    private function checkPort($config)
122
    {
123
        return array_key_exists('port', $config) ? ";port=".$config['port'] : '';
124
    }
125
}
126