Failed Conditions
Pull Request — master (#3260)
by Michael
61:30
created

MysqliConnection::setDriverOptions()   A

Complexity

Conditions 6
Paths 10

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 18.6628

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 32
ccs 5
cts 17
cp 0.294
rs 9.0444
c 0
b 0
f 0
cc 6
nc 10
nop 1
crap 18.6628
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Driver\Mysqli;
6
7
use Doctrine\DBAL\Driver\Connection;
8
use Doctrine\DBAL\Driver\PingableConnection;
9
use Doctrine\DBAL\Driver\ResultStatement;
10
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
11
use Doctrine\DBAL\Driver\Statement as DriverStatement;
12
use mysqli;
13
use const MYSQLI_INIT_COMMAND;
14
use const MYSQLI_OPT_CONNECT_TIMEOUT;
15
use const MYSQLI_OPT_LOCAL_INFILE;
16
use const MYSQLI_READ_DEFAULT_FILE;
17
use const MYSQLI_READ_DEFAULT_GROUP;
18
use const MYSQLI_SERVER_PUBLIC_KEY;
19
use function defined;
20
use function floor;
21
use function in_array;
22
use function ini_get;
23
use function mysqli_init;
24
use function mysqli_options;
25
use function restore_error_handler;
26
use function set_error_handler;
27
use function sprintf;
28
use function stripos;
29
30
class MysqliConnection implements Connection, PingableConnection, ServerInfoAwareConnection
31
{
32
    /**
33
     * Name of the option to set connection flags
34
     */
35
    public const OPTION_FLAGS = 'flags';
36
37
    /** @var mysqli */
38
    private $conn;
39
40
    /**
41
     * @param mixed[] $params
42
     * @param string  $username
43
     * @param string  $password
44
     * @param mixed[] $driverOptions
45
     *
46
     * @throws MysqliException
47
     */
48 2278
    public function __construct(array $params, $username, $password, array $driverOptions = [])
49
    {
50 2278
        $port = $params['port'] ?? (int) ini_get('mysqli.default_port');
51
52
        // Fallback to default MySQL port if not given.
53 2278
        if (! $port) {
54
            $port = 3306;
55
        }
56
57 2278
        $socket = $params['unix_socket'] ?? ini_get('mysqli.default_socket');
58 2278
        $dbname = $params['dbname'] ?? '';
59
60 2278
        $flags = $driverOptions[static::OPTION_FLAGS] ?? 0;
61
62 2278
        $this->conn = mysqli_init();
63
64 2278
        $this->setSecureConnection($params);
65 2278
        $this->setDriverOptions($driverOptions);
66
67
        set_error_handler(static function () {
68 2278
        });
69
        try {
70 2278
            if (! $this->conn->real_connect($params['host'], $username, $password, $dbname, $port, $socket, $flags)) {
71 2270
                throw MysqliException::fromConnectionError($this->conn);
72
            }
73 2269
        } finally {
74 2278
            restore_error_handler();
75
        }
76
77 2269
        if (! isset($params['charset'])) {
78 2269
            return;
79
        }
80
81 1405
        $this->conn->set_charset($params['charset']);
82 1405
    }
83
84
    /**
85
     * Retrieves mysqli native resource handle.
86
     *
87
     * Could be used if part of your application is not using DBAL.
88
     *
89
     * @return mysqli
90
     */
91
    public function getWrappedResourceHandle()
92
    {
93
        return $this->conn;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     *
99
     * The server version detection includes a special case for MariaDB
100
     * to support '5.5.5-' prefixed versions introduced in Maria 10+
101
     *
102
     * @link https://jira.mariadb.org/browse/MDEV-4088
103
     */
104 2269
    public function getServerVersion()
105
    {
106 2269
        $serverInfos = $this->conn->get_server_info();
107 2269
        if (stripos($serverInfos, 'mariadb') !== false) {
108 1131
            return $serverInfos;
109
        }
110
111 1138
        $majorVersion = floor($this->conn->server_version / 10000);
112 1138
        $minorVersion = floor(($this->conn->server_version - $majorVersion * 10000) / 100);
113 1138
        $patchVersion = floor($this->conn->server_version - $majorVersion * 10000 - $minorVersion * 100);
114
115 1138
        return $majorVersion . '.' . $minorVersion . '.' . $patchVersion;
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 2287
    public function requiresQueryForServerVersion()
122
    {
123 2287
        return false;
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129 2253
    public function prepare(string $sql) : DriverStatement
130
    {
131 2253
        return new MysqliStatement($this->conn, $sql);
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137 2245
    public function query(string $sql) : ResultStatement
138
    {
139 2245
        $stmt = $this->prepare($sql);
140 2245
        $stmt->execute();
141 2245
142 2245
        return $stmt;
143
    }
144 2245
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function quote(string $input) : string
149
    {
150 2117
        return "'" . $this->conn->escape_string($input) . "'";
151
    }
152 2117
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function exec(string $statement) : int
157
    {
158 2269
        if ($this->conn->query($statement) === false) {
159
            throw MysqliException::fromConnectionError($this->conn);
160 2269
        }
161 2253
162
        return $this->conn->affected_rows;
163
    }
164 2269
165
    /**
166
     * {@inheritdoc}
167
     */
168
    public function lastInsertId($name = null)
169
    {
170 56
        return $this->conn->insert_id;
171
    }
172 56
173
    /**
174
     * {@inheritdoc}
175
     */
176
    public function beginTransaction() : void
177
    {
178 2197
        $this->conn->query('START TRANSACTION');
179
    }
180 2197
181
    /**
182 2197
     * {@inheritdoc}
183
     */
184
    public function commit() : void
185
    {
186
        if (! $this->conn->commit()) {
187
            throw MysqliException::fromConnectionError($this->conn);
188 2181
        }
189
    }
190 2181
191
    /**
192
     * {@inheritdoc}
193
     */
194
    public function rollBack() : void
195
    {
196 2197
        if (! $this->conn->rollback()) {
197
            throw MysqliException::fromConnectionError($this->conn);
198 2197
        }
199
    }
200
201
    /**
202
     * Apply the driver options to the connection.
203
     *
204
     * @param mixed[] $driverOptions
205
     *
206
     * @throws MysqliException When one of of the options is not supported.
207
     * @throws MysqliException When applying doesn't work - e.g. due to incorrect value.
208
     */
209
    private function setDriverOptions(array $driverOptions = [])
210
    {
211
        $supportedDriverOptions = [
212
            MYSQLI_OPT_CONNECT_TIMEOUT,
213
            MYSQLI_OPT_LOCAL_INFILE,
214
            MYSQLI_INIT_COMMAND,
215
            MYSQLI_READ_DEFAULT_FILE,
216
            MYSQLI_READ_DEFAULT_GROUP,
217
        ];
218
219
        if (defined('MYSQLI_SERVER_PUBLIC_KEY')) {
220
            $supportedDriverOptions[] = MYSQLI_SERVER_PUBLIC_KEY;
221
        }
222
223
        $exceptionMsg = "%s option '%s' with value '%s'";
224
225 2278
        foreach ($driverOptions as $option => $value) {
226
            if ($option === static::OPTION_FLAGS) {
227
                continue;
228 2278
            }
229
230
            if (! in_array($option, $supportedDriverOptions, true)) {
231
                throw new MysqliException(
232
                    sprintf($exceptionMsg, 'Unsupported', $option, $value)
233
                );
234
            }
235 2278
236 2278
            if (@mysqli_options($this->conn, $option, $value)) {
237
                continue;
238
            }
239 2278
240
            throw MysqliException::fromConnectionError($this->conn);
241 2278
        }
242 1589
    }
243
244
    /**
245
     * Pings the server and re-connects when `mysqli.reconnect = 1`
246 1589
     *
247 1581
     * @return bool
248 1581
     */
249
    public function ping()
250
    {
251
        return $this->conn->ping();
252 1589
    }
253 1589
254
    /**
255
     * Establish a secure connection
256
     *
257
     * @param mixed[] $params
258
     *
259
     * @throws MysqliException
260
     */
261
    private function setSecureConnection(array $params)
262
    {
263
        if (! isset($params['ssl_key']) &&
264
            ! isset($params['ssl_cert']) &&
265 2278
            ! isset($params['ssl_ca']) &&
266
            ! isset($params['ssl_capath']) &&
267
            ! isset($params['ssl_cipher'])
268
        ) {
269
            return;
270
        }
271
272 2109
        $this->conn->ssl_set(
273
            $params['ssl_key']    ?? null,
274 2109
            $params['ssl_cert']   ?? null,
275
            $params['ssl_ca']     ?? null,
276
            $params['ssl_capath'] ?? null,
277
            $params['ssl_cipher'] ?? null
278
        );
279
    }
280
}
281