Completed
Push — develop ( e32bfe...213a3e )
by Mathieu
01:40
created

Database::connect()   B

Complexity

Conditions 8
Paths 23

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 9.4924

Importance

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