Completed
Push — master ( d2e043...09ed12 )
by Changwan
12:25
created

Configuration::createPdo()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 14
nc 1
nop 0
dl 0
loc 18
ccs 10
cts 10
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Wandu\Database;
3
4
use PDO;
5
6
class Configuration
7
{
8
    const DRIVER_MYSQL = 'mysql';
9
    
10
    /** @var string */
11
    protected $driver = 'mysql';
12
    
13
    /** @var string */
14
    protected $host = 'localhost';
15
16
    /** @var int */
17
    protected $port = 4403;
18
19
    /** @var string */
20
    protected $username = 'root';
21
22
    /** @var string */
23
    protected $password = '';
24
25
    /** @var string */
26
    protected $database = null;
27
28
    /** @var string */
29
    protected $charset;
30
31
    /** @var string */
32
    protected $collation;
33
34
    /** @var string */
35
    protected $prefix = '';
36
37
    /** @var string */
38
    protected $timezone;
39
40
    /** @var array */
41
    protected $options = [
42
        PDO::ATTR_CASE => PDO::CASE_NATURAL,
43
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
44
        PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
45
        PDO::ATTR_STRINGIFY_FETCHES => false,
46
        PDO::ATTR_EMULATE_PREPARES => false,
47
    ];
48
49
    /**
50
     * @param array $settings
51
     */
52 17
    public function __construct(array $settings = [])
53
    {
54 17
        foreach ($settings as $name => $setting) {
55 17
            $this->{$name} = $setting;
56
        }
57 17
    }
58
59
    /**
60
     * @return string
61
     */
62 17
    public function getDriver()
63
    {
64 17
        return $this->driver;
65
    }
66
67
    /**
68
     * @return string
69
     */
70
    public function getHost()
71
    {
72
        return $this->host;
73
    }
74
75
    /**
76
     * @return int
77
     */
78
    public function getPort()
79
    {
80
        return $this->port;
81
    }
82
    
83
    /**
84
     * @return \PDO
85
     */
86 16
    public function createPdo()
87
    {
88 16
        $pdo = new PDO(
89 16
            "{$this->driver}:host={$this->host};port={$this->port};dbname={$this->database}",
90 16
            $this->username,
91 16
            $this->password,
92 16
            $this->options + [
93 16
                PDO::ATTR_CASE => PDO::CASE_NATURAL,
94
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
95
                PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL,
96
                PDO::ATTR_STRINGIFY_FETCHES => false,
97
                PDO::ATTR_EMULATE_PREPARES => false,
98
            ]
99
        );
100 16
        $this->applyCharset($pdo);
101 16
        $this->applyTimezone($pdo);
102 16
        return $pdo;
103
    }
104
105
    /**
106
     * @param \PDO $pdo
107
     */
108 16
    protected function applyCharset(PDO $pdo)
109
    {
110 16
        if ($this->charset) {
111 16
            $names = "SET NAMES '{$this->charset}'";
112 16
            if ($this->collation) {
113 16
                $names .= " COLLATE '{$this->collation}'";
114
            }
115 16
            $pdo->prepare($names)->execute();
116
        }
117 16
    }
118
119
    /**
120
     * @param \PDO $pdo
121
     */
122 16
    protected function applyTimezone(PDO $pdo)
123
    {
124 16
        if ($this->timezone) {
125 1
            $pdo->prepare("SET time_zone='{$this->timezone}'")->execute();
126
        }
127 16
    }
128
}
129