Passed
Push — master ( a7f2b9...90017e )
by huang
03:13
created

Database::has()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
cc 3
eloc 6
c 0
b 0
f 0
nc 2
nop 3
dl 0
loc 12
ccs 5
cts 6
cp 0.8333
crap 3.0416
rs 9.4285
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 44
    public function __construct(array $options = null)
37
    {
38 44
        $this->config = $options;
39
40 44
        parent::__construct($this->config);
41
42 44
        $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
43 44
        $this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
44 44
        $this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
45 44
        $this->pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false);
46 44
    }
47
48
    /**
49
     * reconnect database.
50
     */
51
    public function reconnect()
52
    {
53
        $this->__construct($this->config);
54
    }
55
56
    /**
57
     * @param $query
58
     *
59
     * @return bool|\PDOStatement
60
     */
61 3 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...
62
    {
63
        try {
64 3
            return parent::query($query);
65
        } catch (PDOException $e) {
66
            if ('HY000' !== $e->getCode()) {
67
                throw $e;
68
            }
69
            $this->reconnect();
70
71
            return parent::query($query);
72
        }
73
    }
74
75
    /**
76
     * @param $query
77
     *
78
     * @return bool|\PDOStatement
79
     */
80 1 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...
81
    {
82
        try {
83 1
            return parent::exec($query);
84
        } catch (PDOException $e) {
85
            if ('HY000' !== $e->getCode()) {
86
                throw $e;
87
            }
88
            $this->reconnect();
89
90
            return parent::exec($query);
91
        }
92
    }
93
94 1
    public function has($table, $join, $where = null)
95
    {
96 1
        $column = null;
97
98 1
        $query = $this->query('SELECT EXISTS('.$this->selectContext($table, $join, $column, $where, 1).')');
99
100 1
        if ($query && intval($query->fetchColumn()) === 1) {
101 1
            return true;
102
        }
103
104
        return false;
105
    }
106
}
107