MySqlConnector::setCustomModes()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
namespace Childish\connection;
3
4
use PDO;
5
6
/**
7
 * MySqlConnector
8
 *
9
 * @author    Pu ShaoWei <[email protected]>
10
 * @date      2017/12/7
11
 * @package   Childish\connection
12
 * @version   1.0
13
 */
14
15
class MySqlConnector extends Connector
16
{
17
    /**
18
     * Establish a database connection.
19
     *
20
     * @param  array  $config
21
     * @return \PDO
22
     */
23
    public function connect(array $config)
24
    {
25
        $dsn = $this->getDsn($config);
26
27
        $options = $this->getOptions($config);
28
29
        // We need to grab the PDO options that should be used while making the brand
30
        // new connection instance. The PDO options control various aspects of the
31
        // connection's behavior, and some might be specified by the developers.
32
        $connection = $this->createConnection($dsn, $config, $options);
33
34
        if (! empty($config['database'])) {
35
            $connection->exec("use `{$config['database']}`;");
36
        }
37
38
        $this->configureEncoding($connection, $config);
39
40
        // Next, we will check to see if a timezone has been specified in this config
41
        // and if it has we will issue a statement to modify the timezone with the
42
        // database. Setting this DB timezone is an optional configuration item.
43
        $this->configureTimezone($connection, $config);
44
45
        $this->setModes($connection, $config);
46
47
        return $connection;
48
    }
49
50
    /**
51
     * Set the connection character set and collation.
52
     *
53
     * @param  \PDO  $connection
54
     * @param  array  $config
55
     * @return void
56
     */
57
    protected function configureEncoding($connection, array $config)
58
    {
59
        if (! isset($config['charset'])) {
60
            return $connection;
61
        }
62
63
        $connection->prepare(
64
            "set names '{$config['charset']}'".$this->getCollation($config)
65
        )->execute();
66
    }
67
68
    /**
69
     * Get the collation for the configuration.
70
     *
71
     * @param  array  $config
72
     * @return string
73
     */
74
    protected function getCollation(array $config)
75
    {
76
        return isset($config['collation']) ? " collate '{$config['collation']}'" : '';
77
    }
78
79
    /**
80
     * Set the timezone on the connection.
81
     *
82
     * @param  \PDO  $connection
83
     * @param  array  $config
84
     * @return void
85
     */
86
    protected function configureTimezone($connection, array $config)
87
    {
88
        if (isset($config['timezone'])) {
89
            $connection->prepare('set time_zone="'.$config['timezone'].'"')->execute();
90
        }
91
    }
92
93
    /**
94
     * Create a DSN string from a configuration.
95
     *
96
     * Chooses socket or host/port based on the 'unix_socket' config value.
97
     *
98
     * @param  array   $config
99
     * @return string
100
     */
101
    protected function getDsn(array $config)
102
    {
103
        return $this->hasSocket($config)
104
            ? $this->getSocketDsn($config)
105
            : $this->getHostDsn($config);
106
    }
107
108
    /**
109
     * Determine if the given configuration array has a UNIX socket value.
110
     *
111
     * @param  array  $config
112
     * @return bool
113
     */
114
    protected function hasSocket(array $config)
115
    {
116
        return isset($config['unix_socket']) && ! empty($config['unix_socket']);
117
    }
118
119
    /**
120
     * Get the DSN string for a socket configuration.
121
     *
122
     * @param  array  $config
123
     * @return string
124
     */
125
    protected function getSocketDsn(array $config)
126
    {
127
        return "mysql:unix_socket={$config['unix_socket']};dbname={$config['database']}";
128
    }
129
130
    /**
131
     * Get the DSN string for a host / port configuration.
132
     *
133
     * @param  array  $config
134
     * @return string
135
     */
136
    protected function getHostDsn(array $config)
137
    {
138
        extract($config, EXTR_SKIP);
139
140
        return isset($port)
141
            ? "mysql:host={$host};port={$port};dbname={$database}"
142
            : "mysql:host={$host};dbname={$database}";
143
    }
144
145
    /**
146
     * Set the modes for the connection.
147
     *
148
     * @param  \PDO  $connection
149
     * @param  array  $config
150
     * @return void
151
     */
152
    protected function setModes(PDO $connection, array $config)
153
    {
154
        if (isset($config['modes'])) {
155
            $this->setCustomModes($connection, $config);
156
        } elseif (isset($config['strict'])) {
157
            if ($config['strict']) {
158
                $connection->prepare($this->strictMode())->execute();
159
            } else {
160
                $connection->prepare("set session sql_mode='NO_ENGINE_SUBSTITUTION'")->execute();
161
            }
162
        }
163
    }
164
165
    /**
166
     * Set the custom modes on the connection.
167
     *
168
     * @param  \PDO  $connection
169
     * @param  array  $config
170
     * @return void
171
     */
172
    protected function setCustomModes(PDO $connection, array $config)
173
    {
174
        $modes = implode(',', $config['modes']);
175
176
        $connection->prepare("set session sql_mode='{$modes}'")->execute();
177
    }
178
179
    /**
180
     * Get the query to enable strict mode.
181
     *
182
     * @return string
183
     */
184
    protected function strictMode()
185
    {
186
        return "set session sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'";
187
    }
188
}