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