Completed
Push — master ( f5adf6...a90a41 )
by Michal
02:09
created

MySqlDriver   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 96.77%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 7
dl 0
loc 67
ccs 30
cts 31
cp 0.9677
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A extensions() 0 4 1
A type() 0 4 1
A defaultCredentials() 0 8 1
A getCredentialsForm() 0 4 1
A connect() 0 15 3
A getFormManager() 0 4 1
A getHeaderManager() 0 4 1
A getPermissions() 0 4 1
A getDataManager() 0 4 1
1
<?php
2
3
namespace UniMan\Drivers\MySql;
4
5
use UniMan\Core\Driver\AbstractDriver;
6
use UniMan\Core\Exception\ConnectException;
7
use UniMan\Drivers\MySql\Forms\MySqlCredentialsForm;
8
use PDO;
9
use PDOException;
10
11
class MySqlDriver extends AbstractDriver
12
{
13
    const TYPE_TABLE = 'table';
14
    const TYPE_VIEW = 'view';
15
16
    private $connection;
17
18 2
    public function extensions()
19
    {
20 2
        return ['pdo_mysql'];
21
    }
22
23 2
    public function type()
24
    {
25 2
        return 'mysql';
26
    }
27
28 2
    public function defaultCredentials()
29
    {
30
        return [
31 2
            'server' => 'localhost:3306',
32 1
            'user' => 'root',
33 1
            'password' => '',
34 1
        ];
35
    }
36
37 2
    public function getCredentialsForm()
38
    {
39 2
        return new MySqlCredentialsForm();
40
    }
41
42 2
    public function connect(array $credentials)
43
    {
44 2
        $host = $credentials['server'];
45 2
        $port = '3306';
46 2
        if (strpos($credentials['server'], ':') !== false) {
47 2
            list($host, $port) = explode(':', $credentials['server'], 2);
48 1
        }
49 2
        $dsn = 'mysql:;host=' . $host . ';port=' . $port . ';charset=utf8';
50
        try {
51 2
            $this->connection = new PDO($dsn, $credentials['user'], $credentials['password']);
52 2
            $this->connection->query("SET SESSION sql_mode = ''");
53 1
        } catch (PDOException $e) {
54
            throw new ConnectException($e->getMessage());
55
        }
56 2
    }
57
58 2
    protected function getFormManager()
59
    {
60 2
        return new MySqlFormManager($this->connection, $this->dataManager());
0 ignored issues
show
Compatibility introduced by
$this->dataManager() of type object<UniMan\Core\DataM...r\DataManagerInterface> is not a sub-type of object<UniMan\Drivers\MySql\MySqlDataManager>. It seems like you assume a concrete implementation of the interface UniMan\Core\DataManager\DataManagerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
61
    }
62
63 2
    protected function getHeaderManager()
64
    {
65 2
        return new MySqlHeaderManager($this->dataManager());
0 ignored issues
show
Compatibility introduced by
$this->dataManager() of type object<UniMan\Core\DataM...r\DataManagerInterface> is not a sub-type of object<UniMan\Drivers\MySql\MySqlDataManager>. It seems like you assume a concrete implementation of the interface UniMan\Core\DataManager\DataManagerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
66
    }
67
68 2
    protected function getPermissions()
69
    {
70 2
        return new MySqlPermissions();
71
    }
72
73 2
    protected function getDataManager()
74
    {
75 2
        return new MySqlDataManager($this->connection);
76
    }
77
}
78