Passed
Push — develop ( 213a3e...25acdc )
by Mathieu
01:45
created

Database::beginTransaction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 1
cp 0
crap 2
rs 10
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 10
    public function __construct()
29
    {
30 10
        parent::__construct();
31
32 10
        $this->configs = [];
33 10
        $this->handler = false;
34 10
    }
35
36 8
    public function configure($parameters = [])
37
    {
38 8
        $dbConfs = [];
39 8
        foreach ($parameters as $name => $value) {
40 8
            if (is_array($value)) {
41
                $dbConfs[$name] = $value;
42
            } else {
43 8
                $dbConfs['default'][$name] = $value;
44
            }
45
        }
46 8
        $parameters = ['configs' => $dbConfs];
47 8
        parent::configure($parameters);
48 8
    }
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 8
    private function connect()
96
    {
97 8
        if ($this->handler !== false) {
98 1
            return;
99
        }
100
101 8
        if ($this->config !== null && isset($this->configs[$this->config])) {
102
            $params = $this->configs[$this->config];
103
        } else {
104 8
            $confs  = array_values($this->configs);
105 8
            $params = array_shift($confs);
106
        }
107
108 8
        $pdoAttributes = [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION];
109 8
        switch ($params['type']) {
110 8
            case 'mysql':
111
                $this->configurePDOMySQL($params, $pdoDsn, $pdoUsername, $pdoPassword, $pdoAttributes);
112
                break;
113 8
            case 'sqlite':
114 7
                $this->configurePDOSQLite($params, $pdoDsn, $pdoUsername, $pdoPassword);
115 7
                break;
116
            default:
117 1
                throw new \Exception('Unsupported PDO DB handler');
118
        }
119
120
        try {
121 7
            $this->handler = new \PDO($pdoDsn, $pdoUsername, $pdoPassword);
122 7
            foreach ($pdoAttributes as $attributeKey => $attributeValue) {
123 7
                $this->handler->setAttribute($attributeKey, $attributeValue);
124
            }
125
        } catch (\Exception $e) {
126
            throw new \Exception("Cannot connect to database");
127
        }
128 7
    }
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 8
    public function query($sql, $parameters = array())
138
    {
139 8
        $this->connect();
140
141 7
        $this->statement = $this->handler->prepare($sql);
142 7
        $this->statement->execute($parameters);
143
144 7
        return $this;
145
    }
146
147 1
    public function fetchAll($mode = \PDO::FETCH_ASSOC)
148
    {
149 1
        return $this->statement->fetchAll($mode);
150
    }
151
152 1
    public function fetch($mode = \PDO::FETCH_ASSOC)
153
    {
154 1
        return $this->statement->fetch($mode);
155
    }
156
157 1
    public function fetchColumn($colNb = 0)
158
    {
159 1
        return $this->statement->fetchColumn($colNb);
160
    }
161
162 1
    public function fetchObject()
163
    {
164 1
        return $this->statement->fetch(\PDO::FETCH_OBJ);
165
    }
166
167
    /**
168
     * Return the last inserted id
169
     *
170
     * @return string
171
     */
172 1
    public function lastInsertId(): string
173
    {
174 1
        return $this->handler->lastInsertId();
175
    }
176
177
    public function beginTransaction()
178
    {
179
    }
180
181
    public function commit()
182
    {
183
    }
184
185
    public function rollback()
186
    {
187
    }
188
189
    public function inTransaction()
190
    {
191
    }
192
193 1
    public function getColumnCount()
194
    {
195 1
        return $this->statement->columnCount();
196
    }
197
}
198