MySqlDriver   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 53
ccs 10
cts 10
cp 1
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A createPdoInstance() 0 13 3
A extendConfigure() 0 4 1
1
<?php
2
/**
3
 * Copyright 2021 Aleksandar Panic
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *   http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
namespace ArekX\PQL\Drivers\Pdo\MySql;
19
20
use ArekX\PQL\Drivers\Pdo\PdoDriver;
21
22
/**
23
 * Driver for handling connections for
24
 * a MySQL driver.
25
 */
26
class MySqlDriver extends PdoDriver
27
{
28
    /**
29
     * Whether to emulate prepare statement.
30
     *
31
     * @var bool
32
     */
33
    public bool $emulatePrepare = false;
34
35
    /**
36
     * What character set to use during the connection.
37
     *
38
     * @var string|null
39
     */
40
    public ?string $charset = null;
41
42
    /**
43
     * @inheritDoc
44
     */
45 18
    protected function createPdoInstance(): \PDO
46
    {
47 18
        $instance = new \PDO($this->dsn, $this->username, $this->password, $this->options);
48
49 18
        if ($this->emulatePrepare) {
50 1
            $instance->setAttribute(\PDO::ATTR_EMULATE_PREPARES, $this->emulatePrepare);
51
        }
52
53 18
        if ($this->charset) {
54 1
            $instance->exec('SET NAMES ' . $instance->quote($this->charset));
55
        }
56
57 18
        return $instance;
58
    }
59
60
61
    /**
62
     * Extends with additional config
63
     *
64
     * Format is:
65
     * ```php
66
     * [
67
     *    'emulatePrepare' => false,
68
     *    'charset' => 'utf8mb4'
69
     * ]
70
     * ```
71
     *
72
     * @param array $config
73
     * @return void
74
     */
75 18
    protected function extendConfigure(array $config): void
76
    {
77 18
        $this->emulatePrepare = $config['emulatePrepare'] ?? $this->emulatePrepare;
78 18
        $this->charset = $config['charset'] ?? $this->charset;
79
    }
80
}
81