Completed
Push — master ( 37f54e...a8ce98 )
by Hong
02:32
created

Driver::realSetAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 10
loc 10
rs 9.4285
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
        // real connect
110
        $link->real_connect(
111
            isset($parameters['host']) ? $parameters['host'] : 'localhost',
112
            isset($parameters['username']) ? $parameters['username'] : 'root',
113
            isset($parameters['password']) ? $parameters['password'] : null,
114
            isset($parameters['db']) ? $parameters['db'] : null,
115
            isset($parameters['port']) ? (int) $parameters['port'] : null,
116
            isset($parameters['socket']) ? $parameters['socket'] : null
117
        );
118
119
        // check failure
120
        $this->isConenctFailed($link);
121
122
        // set charset
123
        $this->setCharset($link, $parameters);
124
125
        return $link;
126
    }
127
128
    /**
129
     * Disconnect the \PDO link
130
     *
131
     * {@inheritDoc}
132
     */
133
    protected function realDisconnect()
134
    {
135
        $this->link->close();
136
    }
137
138
    /**
139
     * {@inheritDoc}
140
     */
141
    protected function realPing()/*# : bool */
142
    {
143
        return $this->link->ping();
144
    }
145
146
    /**
147
     * {@inheritDoc}
148
     */
149 View Code Duplication
    protected function realSetAttribute(/*# string */ $attribute, $value)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
150
    {
151
        if (is_string($attribute)) {
152
            $this->checkAttribute($attribute);
153
            $this->link->options(constant($attribute), $value);
154
        } else {
155
            $this->link->options($attribute, $value);
156
        }
157
        return $this;
158
    }
159
160
    /**
161
     * {@inheritDoc}
162
     */
163
    protected function realGetAttribute(/*# string */ $attribute)
164
    {
165
        if (is_string($attribute)) {
166
            $this->checkAttribute($attribute);
167
        }
168
        return null;
169
    }
170
171
    /**
172
     * {@inheritDoc}
173
     */
174
    protected function realBegin()
175
    {
176
        $this->link->autocommit(false);
177
        return $this;
178
    }
179
180
    /**
181
     * {@inheritDoc}
182
     */
183
    protected function realCommit()
184
    {
185
        $this->link->commit();
186
        $this->link->autocommit(true);
187
        return $this;
188
    }
189
190
    /**
191
     * {@inheritDoc}
192
     */
193
    protected function realRollback()
194
    {
195
        $this->link->rollback();
196
        $this->link->autocommit(true);
197
        return $this;
198
    }
199
200
    /**
201
     *
202
     * @param  \mysqli $link
203
     * @throws LogicException if failed
204
     * @access protected
205
     */
206
    protected function isConenctFailed(\mysqli $link)
207
    {
208
        if ($link->connect_error) {
209
            throw new LogicException(
210
                Message::get(
211
                    Message::DB_CONNECT_FAIL,
212
                    $link->connect_errno,
213
                    $link->connect_error
214
                ),
215
                Message::DB_CONNECT_FAIL
216
            );
217
        }
218
    }
219
220
    /**
221
     * Set charset
222
     *
223
     * @param  \mysqli $link
224
     * @param  array $params
225
     * @access protected
226
     */
227
    protected function setCharset(\mysqli $link, array $params)
228
    {
229
        if (!empty($params['charset'])) {
230
            $link->set_charset($params['charset']);
231
        }
232
    }
233
234
    /**
235
     * Is attribute defined ?
236
     *
237
     * @param  string $attribute
238
     * @throws LogicException
239
     * @access protected
240
     */
241
    protected function checkAttribute(/*# string */ $attribute)
242
    {
243
        if (!defined($attribute)) {
244
            throw new LogicException(
245
                Message::get(Message::DB_ATTRIBUTE_UNKNOWN, $attribute),
246
                Message::DB_ATTRIBUTE_UNKNOWN
247
            );
248
        }
249
    }
250
}
251