Passed
Branch 3.1 (41cce1)
by huang
02:56
created

Database::query()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 12
Ratio 92.31 %

Code Coverage

Tests 2
CRAP Score 6.28

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 12
loc 13
rs 9.4285
c 0
b 0
f 0
ccs 2
cts 7
cp 0.2857
crap 6.28
1
<?php
2
/**
3
 * @author    jan huang <[email protected]>
4
 * @copyright 2016
5
 *
6
 * @see      https://www.github.com/janhuang
7
 * @see      http://www.fast-d.cn/
8
 */
9
10
namespace FastD\Model;
11
12
use Medoo\Medoo;
13
use PDO;
14
use PDOException;
15
16
/**
17
 * Class Database.
18
 */
19
class Database extends Medoo
20
{
21
    /**
22
     * @var array|null
23
     */
24
    protected $config = [];
25
26
    /**
27
     * @var PDO
28
     */
29
    public $pdo;
30
31
    /**
32
     * Database constructor.
33
     *
34
     * @param array $options
35
     */
36 37
    public function __construct(array $options = null)
37
    {
38 37
        $this->config = $options;
39
40 37
        parent::__construct($this->config);
41
42 37
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
43 37
        $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
44 37
    }
45
46
    /**
47
     * reconnect database.
48
     */
49
    public function reconnect()
50
    {
51
        $this->__construct($this->config);
52
    }
53
54
    /**
55
     * @param $query
56
     *
57
     * @return bool|\PDOStatement
58
     */
59 2 View Code Duplication
    public function query($query)
0 ignored issues
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...
60
    {
61
        try {
62 2
            return parent::query($query);
63
        } catch (PDOException $e) {
64
            if ('HY000' !== $e->getCode()) {
65
                throw $e;
66
            }
67
            $this->reconnect();
68
69
            return parent::query($query);
70
        }
71
    }
72
73
    /**
74
     * @param $query
75
     *
76
     * @return bool|int
77
     */
78 View Code Duplication
    public function exec($query)
0 ignored issues
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...
79
    {
80
        try {
81
            return parent::exec($query);
82
        } catch (PDOException $e) {
83
            if ('HY000' !== $e->getCode()) {
84
                throw $e;
85
            }
86
            $this->reconnect();
87
88
            return parent::exec($query);
89
        }
90
    }
91
}
92