Passed
Push — develop ( a0fec2...51b6fa )
by Mathieu
01:55
created

Database::configurePDOMySQL()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 5
dl 0
loc 17
ccs 0
cts 8
cp 0
crap 6
rs 9.8666
c 0
b 0
f 0
1
<?php
2
namespace Suricate;
3
4
/**
5
 * Database extension for Suricate
6
 *
7
 * @package Suricate
8
 * @author  Mathieu LESNIAK <[email protected]>
9
 *
10
 * @property array $configs array of predefined DB configurations
11
 */
12
13
14
class Database extends Service
15
{
16
    use Traits\DatabaseMySQL;
17
    use Traits\DatabaseSQLite;
18
19
    protected $parametersList = [
20
        'configs'
21
    ];
22
23
    private $config;
24
    private $handler;
25
    private $statement;
26
27
    public function __construct()
28
    {
29
        parent::__construct();
30
31
        $this->configs = [];
32
        $this->handler = false;
33
    }
34
35
    public function configure($parameters = [])
36
    {
37
        $dbConfs = [];
38
        foreach ($parameters as $name => $value) {
39
            if (is_array($value)) {
40
                $dbConfs[$name] = $value;
41
            } else {
42
                $dbConfs['default'][$name] = $value;
43
            }
44
        }
45
        $parameters = ['configs' => $dbConfs];
46
        parent::configure($parameters);
47
    }
48
49
    public function setConfigs($configs)
50
    {
51
        $this->configs = $configs;
52
53
        return $this;
54
    }
55
56
    public function getConfigs()
57
    {
58
        return $this->configs;
59
    }
60
61
    public function setConfig($config)
62
    {
63
        $this->config = $config;
64
65
        return $this;
66
    }
67
68
    public function getConfig()
69
    {
70
        return $this->config;
71
    }
72
73
    private function connect()
74
    {
75
        if ($this->handler !== false) {
76
            return;
77
        }
78
79
        if ($this->config !== null && isset($this->configs[$this->config])) {
80
            $params = $this->configs[$this->config];
81
        } else {
82
            $confs  = array_values($this->configs);
83
            $params = array_shift($confs);
84
        }
85
86
        $pdoAttributes = array(\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION);
87
        switch ($params['type']) {
88
            case 'mysql':
89
                $this->configurePDOMySQL($params, $pdoDsn, $pdoUsername, $pdoPassword, $pdoAttributes);
90
                break;
91
            case 'sqlite':
92
                $this->configurePDOSQLite($params, $pdoDsn, $pdoUsername, $pdoPassword);
93
                break;
94
            default:
95
                throw new \Exception('Unsupported PDO DB handler');
96
        }
97
98
        try {
99
            $this->handler = new \PDO($pdoDsn, $pdoUsername, $pdoPassword);
100
            foreach ($pdoAttributes as $attributeKey => $attributeValue) {
101
                $this->handler->setAttribute($attributeKey, $attributeValue);
102
            }
103
        } catch (\Exception $e) {
104
            throw new \Exception("Cannot connect to database");
105
        }
106
    }
107
108
    /**
109
     * Execute a query against database. Create a connection if not already alvailable
110
     * @param  string $sql        Query
111
     * @param  array  $parameters Parameters used in query
112
     * @return Database
113
     */
114
    public function query($sql, $parameters = array())
115
    {
116
        $this->connect();
117
118
        $this->statement = $this->handler->prepare($sql);
119
        $this->statement->execute($parameters);
120
121
        return $this;
122
    }
123
124
    public function fetchAll($mode = \PDO::FETCH_ASSOC)
125
    {
126
        return $this->statement->fetchAll($mode);
127
    }
128
129
    public function fetch($mode = \PDO::FETCH_ASSOC)
130
    {
131
        return $this->statement->fetch($mode);
132
    }
133
134
    public function fetchColumn($colNb = 0)
135
    {
136
        return $this->statement->fetchColumn($colNb);
137
    }
138
139
    public function fetchObject()
140
    {
141
        return $this->statement->fetch(\PDO::FETCH_OBJ);
142
    }
143
144
    public function lastInsertId()
145
    {
146
        return $this->handler->lastInsertId();
147
    }
148
149
    public function beginTransaction()
150
    {
151
    }
152
153
    public function commit()
154
    {
155
    }
156
157
    public function rollback()
158
    {
159
    }
160
161
    public function inTransaction()
162
    {
163
    }
164
165
    public function getColumnCount()
166
    {
167
        return $this->statement->columnCount();
168
    }
169
170
    
171
172
    
173
}
174