Passed
Push — master ( 3829ec...650305 )
by Aleksandar
03:24 queued 15s
created

MySqlDriver::createPdoInstance()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.4746

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 4
nop 0
dl 0
loc 14
ccs 5
cts 8
cp 0.625
crap 3.4746
rs 10
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
class MySqlDriver extends PdoDriver
23
{
24
    public $emulatePrepare;
25
26
    public $charset;
27
28 2
    protected function createPdoInstance(): \PDO
29
    {
30 2
        $instance = new \PDO($this->dsn, $this->username, $this->password, $this->options);
31
32 2
        if ($this->emulatePrepare !== null) {
33
            $instance->setAttribute(\PDO::ATTR_EMULATE_PREPARES, $this->emulatePrepare);
34
        }
35
36 2
        if ($this->charset) {
37
            $pdo = $this->getPdo();
38
            $pdo->exec('SET NAMES ' . $pdo->quote($this->charset));
39
        }
40
41 2
        return $instance;
42
    }
43
44 2
    protected function extendConfigure(array $config)
45
    {
46 2
        $this->emulatePrepare = $config['emulatePrepare'] ?? $this->emulatePrepare;
47 2
        $this->charset = $config['charset'] ?? $this->charset;
48
    }
49
}