Passed
Push — master ( c85748...2fd0ab )
by Petr
08:11
created

MySQL   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 9.52%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
c 1
b 0
f 0
dl 0
loc 37
ccs 2
cts 21
cp 0.0952
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A languageDialect() 0 3 1
A connectToServer() 0 28 3
1
<?php
2
3
namespace kalanis\kw_mapper\Storage\Database\PDO;
4
5
6
use kalanis\kw_mapper\Storage\Database\Dialects;
7
use PDO;
8
9
10
/**
11
 * Class MySQL
12
 * @package kalanis\kw_mapper\Storage\Database\PDO
13
 * Can be also used for Sphinx search engine
14
 */
15
class MySQL extends APDO
16
{
17
    protected $extension = 'pdo_mysql';
18
19 3
    public function languageDialect(): string
20
    {
21 3
        return Dialects\MySQL::class;
22
    }
23
24
    protected function connectToServer(): PDO
25
    {
26
        ini_set('mysql.connect_timeout', strval($this->config->getTimeout()));
27
        ini_set('default_socket_timeout', strval($this->config->getTimeout()));
28
29
        $connection = new PDO(
30
            sprintf('mysql:host=%s;port=%d;dbname=%s',
31
                $this->config->getLocation(),
32
                $this->config->getPort(),
33
                $this->config->getDatabase()
34
            ),
35
            $this->config->getUser(),
36
            $this->config->getPassword()
37
        );
38
39
        $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
40
41
        if ($this->config->isPersistent()) {
42
            $connection->setAttribute(PDO::ATTR_PERSISTENT, true);
43
        }
44
45
        foreach ($this->attributes as $key => $value){
46
            $connection->setAttribute($key, $value);
47
        }
48
49
        $connection->query('SET NAMES utf8;');
50
51
        return $connection;
52
    }
53
}
54