Driver::realSetAttribute()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 2
1
<?php
2
/**
3
 * Phossa Project
4
 *
5
 * PHP version 5.4
6
 *
7
 * @category  Library
8
 * @package   Phossa2\Db
9
 * @copyright Copyright (c) 2016 phossa.com
10
 * @license   http://mit-license.org/ MIT License
11
 * @link      http://www.phossa.com/
12
 */
13
/*# declare(strict_types=1); */
14
15
namespace Phossa2\Db\Driver\Mysqli;
16
17
use Phossa2\Db\Message\Message;
18
use Phossa2\Db\Driver\DriverAbstract;
19
use Phossa2\Db\Exception\LogicException;
20
use Phossa2\Db\Interfaces\StatementInterface;
21
22
/**
23
 * Driver
24
 *
25
 * Mysqli driver
26
 *
27
 * @package Phossa2\Db
28
 * @author  Hong Zhang <[email protected]>
29
 * @see     DriverAbstract
30
 * @version 2.0.0
31
 * @since   2.0.0 added
32
 */
33
class Driver extends DriverAbstract
34
{
35
    /**
36
     * the connection link
37
     *
38
     * @var    \mysqli
39
     * @access protected
40
     */
41
    protected $link;
42
43
    /**
44
     * Default mysqli attributes
45
     *
46
     * @var    array
47
     * @access protected
48
     */
49
    protected $attributes = [
50
        'MYSQLI_OPT_CONNECT_TIMEOUT' => 300,
51
        'MYSQLI_OPT_LOCAL_INFILE' => true,
52
        'MYSQLI_INIT_COMMAND' => '',
53
    ];
54
55
    /**
56
     * Driver constructor
57
     *
58
     * @param  array $connectInfo
59
     * @param  StatementInterface $statementPrototype
60
     * @throws InvalidArgumentException if link type not right
61
     * @throws LogicException driver specific extension not loaded
62
     * @access public
63
     */
64
    public function __construct(
65
        $connectInfo,
66
        StatementInterface $statementPrototype = null
67
    ) {
68
        parent::__construct($connectInfo);
69
70
        // set prototypes
71
        $this->statement_prototype = $statementPrototype ?: new Statement();
72
    }
73
74
    /**
75
     * {@inheritDoc}
76
     */
77
    protected function extensionLoaded()/*# : bool */
78
    {
79
        return extension_loaded('mysqli');
80
    }
81
82
    /**
83
     * {@inheritDoc}
84
     */
85
    protected function realLastId($name)
86
    {
87
        return $this->link->insert_id;
88
    }
89
90
    /**
91
     * {@inheritDoc}
92
     */
93
    protected function realQuote(
94
        $string,
95
        /*# int */ $type
96
    )/*# : string */ {
97
        return '\'' . $this->link->real_escape_string($string) . '\'';
98
    }
99
100
    /**
101
     * {@inheritDoc}
102
     */
103
    protected function realConnect(array $parameters)
104
    {
105
        // init
106
        $link = new \mysqli();
107
        $link->init();
108
109
        // params with defaults
110
        $p = $this->fixParams($parameters);
111
112
        // real connect
113
        $link->real_connect(
114
            $p['host'],
115
            $p['username'],
116
            $p['password'],
117
            $p['db'],
118
            $p['port'],
119
            $p['socket']
120
        );
121
122
        $this->isConenctFailed($link);
123
        $this->setCharset($link, $parameters);
124
        return $link;
125
    }
126
127
    /**
128
     * Disconnect the \PDO link
129
     *
130
     * {@inheritDoc}
131
     */
132
    protected function realDisconnect()
133
    {
134
        $this->link->close();
135
    }
136
137
    /**
138
     * {@inheritDoc}
139
     */
140
    protected function realPing()/*# : bool */
141
    {
142
        return $this->link->ping();
143
    }
144
145
    /**
146
     * {@inheritDoc}
147
     */
148 View Code Duplication
    protected function realSetAttribute(/*# string */ $attribute, $value)
149
    {
150
        if (is_string($attribute)) {
151
            $this->checkAttribute($attribute);
152
            $this->link->options(constant($attribute), $value);
153
        } else {
154
            $this->link->options($attribute, $value);
155
        }
156
        return $this;
157
    }
158
159
    /**
160
     * {@inheritDoc}
161
     */
162
    protected function realGetAttribute(/*# string */ $attribute)
163
    {
164
        if (is_string($attribute)) {
165
            $this->checkAttribute($attribute);
166
        }
167
        return null;
168
    }
169
170
    /**
171
     * {@inheritDoc}
172
     */
173
    protected function realBegin()
174
    {
175
        $this->link->autocommit(false);
176
        return $this;
177
    }
178
179
    /**
180
     * {@inheritDoc}
181
     */
182
    protected function realCommit()
183
    {
184
        $this->link->commit();
185
        $this->link->autocommit(true);
186
        return $this;
187
    }
188
189
    /**
190
     * {@inheritDoc}
191
     */
192
    protected function realRollback()
193
    {
194
        $this->link->rollback();
195
        $this->link->autocommit(true);
196
        return $this;
197
    }
198
199
    /**
200
     * Fill connect params with defaults
201
     *
202
     * @param  array $params
203
     * @return array
204
     * @access protected
205
     */
206
    protected function fixParams(array $params)/*# : array */
207
    {
208
        return array_replace([
209
            'host' => 'localhost',
210
            'username' => 'root',
211
            'password' => null,
212
            'db' => null,
213
            'port' => null,
214
            'socket' => null
215
        ], $params);
216
    }
217
218
    /**
219
     *
220
     * @param  \mysqli $link
221
     * @throws LogicException if failed
222
     * @access protected
223
     */
224
    protected function isConenctFailed(\mysqli $link)
225
    {
226
        if ($link->connect_error) {
227
            throw new LogicException(
228
                Message::get(
229
                    Message::DB_CONNECT_FAIL,
230
                    $link->connect_errno,
231
                    $link->connect_error
232
                ),
233
                Message::DB_CONNECT_FAIL
234
            );
235
        }
236
    }
237
238
    /**
239
     * Set charset
240
     *
241
     * @param  \mysqli $link
242
     * @param  array $params
243
     * @access protected
244
     */
245
    protected function setCharset(\mysqli $link, array $params)
246
    {
247
        if (!empty($params['charset'])) {
248
            $link->set_charset($params['charset']);
249
        }
250
    }
251
252
    /**
253
     * Is attribute defined ?
254
     *
255
     * @param  string $attribute
256
     * @throws LogicException
257
     * @access protected
258
     */
259
    protected function checkAttribute(/*# string */ $attribute)
260
    {
261
        if (!defined($attribute)) {
262
            throw new LogicException(
263
                Message::get(Message::DB_ATTRIBUTE_UNKNOWN, $attribute),
264
                Message::DB_ATTRIBUTE_UNKNOWN
265
            );
266
        }
267
    }
268
}
269