Passed
Push — master ( 87e63f...d6a1bd )
by Darío
02:40
created

AbstractDriver::getDbhost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * DronePHP (http://www.dronephp.com)
4
 *
5
 * @link      http://github.com/Pleets/DronePHP
6
 * @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org)
7
 * @license   http://www.dronephp.com/license
8
 * @author    Darío Rivera <[email protected]>
9
 */
10
11
namespace Drone\Db\Driver;
12
13
use Drone\Error\Errno;
14
15
/**
16
 * AbstractDriver Class
17
 *
18
 * This class defines standard behavior for database drivers
19
 */
20
abstract class AbstractDriver
21
{
22
    use \Drone\Error\ErrorTrait;
23
24
    /**
25
     * Database host
26
     *
27
     * @var string
28
     */
29
    protected $dbhost;
30
31
    /**
32
     * Database user
33
     *
34
     * @var string
35
     */
36
    protected $dbuser;
37
38
    /**
39
     * Database password
40
     *
41
     * @var string
42
     */
43
    protected $dbpass;
44
45
    /**
46
     * Database name
47
     *
48
     * @var string
49
     */
50
    protected $dbname;
51
52
    /**
53
     * Database charset
54
     *
55
     * @var string
56
     */
57
    protected $dbchar;
58
59
    /**
60
     * Database port
61
     *
62
     * @var integer
63
     */
64
    protected $dbport;
65
66
    /**
67
     * Driver name
68
     *
69
     * @var string
70
     */
71
    protected $driverName;
72
73
    /**
74
     * Connection identifier
75
     *
76
     * @var object|resource
77
     */
78
    protected $dbconn;
79
80
    /**
81
     * Rows returned on execute() method
82
     *
83
     * @var integer
84
     */
85
    protected $numRows;
86
87
    /**
88
     * Fields returned on execute() method
89
     *
90
     * @var integer
91
     */
92
    protected $numFields;
93
94
    /**
95
     * Rows affected returned on execute() method
96
     *
97
     * @var integer
98
     */
99
    protected $rowsAffected;
100
101
    /**
102
     * Statement handle
103
     *
104
     * @var resource|mixed
105
     */
106
    protected $result;
107
108
    /**
109
     * Data stored in select statements
110
     *
111
     * @var array
112
     */
113
    protected $arrayResult;
114
115
    /**
116
     * Defines if consecutive querys are part of a transaction
117
     *
118
     * @var boolean
119
     */
120
    protected $transac_mode = false;
121
122
    /**
123
     * Latest result of a transaction process
124
     *
125
     * The first time would be a result of a query(), execute() or parse() methods
126
     * On subsequent times it's a boolean resulting of the boolean operation AND with the latest result.
127
     *
128
     * @var boolean
129
     */
130
    protected $transac_result = null;
131
132
    /**
133
     * Autocommit behavior
134
     *
135
     * @var boolean
136
     */
137
    protected $autocommit = true;
138
139
    /**
140
     * Returns the dbhost attribute
141
     *
142
     * @return string
143
     */
144 1
    public function getDbhost()
145
    {
146 1
        return $this->dbhost;
147
    }
148
149
    /**
150
     * Returns the dbuser attribute
151
     *
152
     * @return string
153
     */
154 1
    public function getDbuser()
155
    {
156 1
        return $this->dbuser;
157
    }
158
159
    /**
160
     * Returns the dbname attribute
161
     *
162
     * @return string
163
     */
164 1
    public function getDbname()
165
    {
166 1
        return $this->dbname;
167
    }
168
169
    /**
170
     * Returns the dbchar attribute
171
     *
172
     * @return string
173
     */
174
    public function getDbchar()
175
    {
176
        return $this->dbchar;
177
    }
178
179
    /**
180
     * Returns the dbport attribute
181
     *
182
     * @return string
183
     */
184 1
    public function getDbport()
185
    {
186 1
        return $this->dbport;
187
    }
188
189
    /**
190
     * Returns the driverName attribute
191
     *
192
     * @return string
193
     */
194 8
    public function getDriverName()
195
    {
196 8
        return $this->driverName;
197
    }
198
199
    /**
200
     * Returns the numRows attribute
201
     *
202
     * @return integer
203
     */
204 20
    public function getNumRows()
205
    {
206 20
        return $this->numRows;
207
    }
208
209
    /**
210
     * Returns the numFields attribute
211
     *
212
     * @return integer
213
     */
214 20
    public function getNumFields()
215
    {
216 20
        return $this->numFields;
217
    }
218
219
    /**
220
     * Returns the rowsAffected attribute
221
     *
222
     * @return integer
223
     */
224 20
    public function getRowsAffected()
225
    {
226 20
        return $this->rowsAffected;
227
    }
228
229
    /**
230
     * Returns an array with all results of the last execute statement
231
     *
232
     * @return array
233
     */
234 10
    public function getArrayResult()
235
    {
236 10
        if (!empty($this->arrayResult))
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
237
            return $this->arrayResult;
238
239 10
        return $this->toArray();
240
    }
241
242
    /**
243
     * Returns the autocommit attribute
244
     *
245
     * @return boolean
246
     */
247
    public function getAutoCommit()
248
    {
249
        return $this->autocommit;
250
    }
251
252
    /**
253
     * Sets dbhost attribute
254
     *
255
     * @param string $value
256
     *
257
     * @return null
258
     */
259 21
    public function setDbhost($value)
260
    {
261 21
        $this->dbhost = $value;
262 21
    }
263
264
    /**
265
     * Sets dbuser attribute
266
     *
267
     * @param string $value
268
     *
269
     * @return null
270
     */
271 21
    public function setDbuser($value)
272
    {
273 21
        $this->dbuser = $value;
274 21
    }
275
276
    /**
277
     * Sets dbpass attribute
278
     *
279
     * @param string $value
280
     *
281
     * @return null
282
     */
283 21
    public function setDbpass($value)
284
    {
285 21
        $this->dbpass = $value;
286 21
    }
287
288
    /**
289
     * Sets dbname attribute
290
     *
291
     * @param string $value
292
     *
293
     * @return null
294
     */
295 21
    public function setDbname($value)
296
    {
297 21
        $this->dbname = $value;
298 21
    }
299
300
    /**
301
     * Sets dbchar attribute
302
     *
303
     * @param string $value
304
     *
305
     * @return null
306
     */
307 21
    public function setDbchar($value)
308
    {
309 21
        $this->dbchar = $value;
310 21
    }
311
312
    /**
313
     * Sets dbport attribute
314
     *
315
     * @param integer $value
316
     *
317
     * @return null
318
     */
319 21
    public function setDbport($value)
320
    {
321 21
        $this->dbport = $value;
322 21
    }
323
324
    /**
325
     * Sets autocommit behavior
326
     *
327
     * @param boolean $value
328
     *
329
     * @return null
330
     */
331 4
    public function autocommit($value)
332
    {
333 4
        if ($this->transac_mode)
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
334
            throw new \LogicException("You cannot change autocommit behavior during a transaction");
335
336 4
        $this->autocommit = $value;
337 4
    }
338
339
    /**
340
     * Driver Constructor
341
     *
342
     * All modifiable attributes (i.e. with setter method) can be passed as key
343
     *
344
     * @param array $options
345
     */
346 21
    public function __construct($options)
347
    {
348 21
        foreach ($options as $option => $value)
349
        {
350 21
            if (property_exists(__CLASS__, strtolower($option)) && method_exists($this, 'set'.$option))
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
351 21
                $this->{'set'.$option}($value);
352
        }
353 21
    }
354
355
    /**
356
     * Returns true if there is a stablished connection
357
     *
358
     * @return boolean
359
     */
360 13
    public function isConnected()
361
    {
362 13
        return (is_resource($this->dbconn) || is_object($this->dbconn));
363
    }
364
365
    /**
366
     * Abstract connect
367
     *
368
     * @throws RuntimeException
369
     *
370
     * @return resource|object
371
     */
372
    public abstract function connect();
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
373
374
    /**
375
     * Abstract execute
376
     *
377
     * @param string $sql
378
     * @param array $params to bind
379
     *
380
     * @throws RuntimeException
381
     *
382
     * @return resource|object
383
     */
384
    public abstract function execute($sql, Array $params = []);
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected array, but found Array.
Loading history...
385
386
    /**
387
     * Reconnects to the database
388
     *
389
     * @throws LogicException
390
     *
391
     * @return resource|object
392
     */
393 2
    public function reconnect()
394
    {
395 2
        if (!$this->isConnected())
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
396 1
            throw new \LogicException("Connection was not established");
397
398 1
        $this->disconnect();
399 1
        return $this->connect();
400
    }
401
402
    /**
403
     * Commit definition
404
     *
405
     * @return boolean
406
     */
407
    public abstract function commit();
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
408
409
    /**
410
     * Rollback definition
411
     *
412
     * @return boolean
413
     */
414
    public abstract function rollback();
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
415
416
    /**
417
     * Closes the connection
418
     *
419
     * @throws LogicException
420
     *
421
     * @return boolean
422
     */
423 3
    public function disconnect()
424
    {
425 3
        if (!$this->isConnected())
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
426 1
            throw new \LogicException("Connection was not established");
427 2
    }
428
429
    /**
430
     * Defines the start point of a transaction
431
     *
432
     * @throws LogicException if transaction was already started
433
     *
434
     * @return null
435
     */
436 2
    public function beginTransaction()
437
    {
438 2
        if (!$this->isConnected())
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
439
            $this->connect();
440
441 2
        if ($this->transac_mode)
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
442
            throw new \LogicException($this->standardErrors[Errno::DB_TRANSACTION_STARTED]);
443
444 2
        $this->transac_mode = true;
445 2
    }
446
447
    /**
448
     * Defines the end point of a transaction
449
     *
450
     * @throws LogicException if transaction has not been started or it's empty
451
     *
452
     * @return null
453
     */
454 2
    public function endTransaction()
455
    {
456 2
        if (!$this->transac_mode)
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
457
            throw new \LogicException($this->standardErrors[Errno::DB_TRANSACTION_NOT_STARTED]);
458
459 2
        if (is_null($this->transac_result))
0 ignored issues
show
introduced by
The condition is_null($this->transac_result) is always false.
Loading history...
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
460
            throw new \LogicException($this->standardErrors[Errno::DB_TRANSACTION_EMPTY]);
461
462 2
        if ($this->transac_result)
0 ignored issues
show
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
463 2
            $this->commit();
464
        else
0 ignored issues
show
Coding Style introduced by
Expected 1 space after ELSE keyword; newline found
Loading history...
Coding Style Best Practice introduced by
It is generally a best practice to always use braces with control structures.

Adding braces to control structures avoids accidental mistakes as your code changes:

// Without braces (not recommended)
if (true)
    doSomething();

// Recommended
if (true) {
    doSomething();
}
Loading history...
465
            $this->rollback();
466
467 2
        $this->result = $this->transac_result;
468
469 2
        $this->transac_result = null;
470 2
        $this->transac_mode = false;
471 2
    }
472
473
    /**
474
     * Abstract result set
475
     *
476
     * By default all Drivers must be implement toArray() function.
477
     * The toArray() method must take the latest result from an execute statement
478
     * and convert it to an array. To get this array getArrayResult() has been implemented.
479
     *
480
     * @throws LogicException if execute() was not executed before this.
481
     *
482
     * @return array
483
     */
484
    protected abstract function toArray();
0 ignored issues
show
Coding Style introduced by
The abstract declaration must precede the visibility declaration
Loading history...
485
486
    /**
487
     * Excecutes multiple statements as transaction
488
     *
489
     * @param array $querys
490
     *
491
     * @return null
492
     */
493
    public function transaction(Array $querys)
0 ignored issues
show
Coding Style introduced by
As per coding-style, PHP keywords should be in lowercase; expected array, but found Array.
Loading history...
494
    {
495
        $this->beginTransaction();
496
497
        foreach ($querys as $sql)
498
        {
499
            $this->execute($sql);
500
        }
501
502
        $this->endTransaction();
503
    }
504
}