Passed
Push — develop ( e50761...7081aa )
by Mathieu
02:34
created

Database::fetchColumn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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