Completed
Push — master ( 3deaa5...4c5cb4 )
by Changwan
06:31
created

MysqlConnector   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 102
ccs 36
cts 36
cp 1
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A createPdo() 0 15 1
A connect() 0 7 1
A applyCharset() 0 10 3
A applyTimezone() 0 6 2
1
<?php
2
namespace Wandu\Database\Connector;
3
4
use ArrayAccess;
5
use PDO;
6
use Wandu\Database\Connection\MysqlConnection;
7
use Wandu\Database\Contracts\ConnectorInterface;
8
9
class MysqlConnector implements ConnectorInterface
10
{
11
    /** @var string */
12
    protected $host = 'localhost';
13
    
14
    /** @var int */
15
    protected $port = 4403;
16
    
17
    /** @var string */
18
    protected $username = 'root';
19
    
20
    /** @var string */
21
    protected $password = '';
22
    
23
    /** @var string */
24
    protected $database;
25
    
26
    /** @var string */
27
    protected $charset;
28
    
29
    /** @var string */
30
    protected $collation;
31
    
32
    /** @var string */
33
    protected $prefix = '';
34
    
35
    /** @var string */
36
    protected $timezone;
37
    
38
    /** @var array */
39
    protected $options = [
40
        PDO::ATTR_CASE => PDO::CASE_NATURAL,
41
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
42
        PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
43
        PDO::ATTR_STRINGIFY_FETCHES => false,
44
        PDO::ATTR_EMULATE_PREPARES => false,
45
    ];
46
47
    /**
48
     * @param array $settings
49
     */
50 16
    public function __construct(array $settings = [])
51
    {
52 16
        foreach ($settings as $name => $setting) {
53 16
            $this->{$name} = $setting;
54 16
        }
55 16
    }
56
57
    /**
58
     * {@inheritdoc} 
59
     */
60 16
    public function connect(ArrayAccess $container = null)
61
    {
62 16
        $connection = $this->createPdo();
63 16
        $this->applyCharset($connection);
64 16
        $this->applyTimezone($connection);
65 16
        return new MysqlConnection($connection, $container, $this->prefix);
66
    }
67
68
    /**
69
     * @param \PDO $connection
70
     */
71 16
    protected function applyCharset(PDO $connection)
72
    {
73 16
        if ($this->charset) {
74 16
            $names = "SET NAMES '{$this->charset}'";
75 16
            if ($this->collation) {
76 16
                $names .= " COLLATE '{$this->collation}'";
77 16
            }
78 16
            $connection->prepare($names)->execute();
79 16
        }
80 16
    }
81
82
    /**
83
     * @param \PDO $connection
84
     */
85 16
    protected function applyTimezone(PDO $connection)
86
    {
87 16
        if ($this->timezone) {
88 1
            $connection->prepare("SET time_zone='{$this->timezone}'")->execute();
89 1
        }
90 16
    }
91
92
    /**
93
     * @return \PDO
94
     */
95 16
    protected function createPdo()
96
    {
97 16
        return new PDO(
98 16
            "mysql:host={$this->host};port={$this->port};dbname={$this->database}",
99 16
            $this->username,
100 16
            $this->password,
101 16
            $this->options + [
102 16
                PDO::ATTR_CASE => PDO::CASE_NATURAL,
103 16
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
104 16
                PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
105 16
                PDO::ATTR_STRINGIFY_FETCHES => false,
106 16
                PDO::ATTR_EMULATE_PREPARES => false,
107
            ]
108 16
        );
109
    }
110
}
111