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

MysqlConnector::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
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