MysqlDatabase   A
last analyzed

Complexity

Total Complexity 19

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 19
eloc 49
c 0
b 0
f 0
dl 0
loc 115
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B query() 0 21 7
B prepare() 0 37 9
A getPDO() 0 8 2
A __construct() 0 6 1
1
<?php
2
3
namespace Core\Services\Database;
4
5
use PDO;
6
use PDOException;
7
8
class MysqlDatabase
9
{
10
    private $db_name;
11
    private $db_user;
12
    private $db_pass;
13
    private $db_host;
14
    private $pdo;
15
16
17
    /**
18
     * Construct - set the environment variables
19
     * @param string $db_name
20
     * @param string $db_user
21
     * @param string $db_pass
22
     * @param string $db_host
23
     * @return void
24
     */
25
    public function __construct(string $db_name, string $db_user = "root", string $db_pass = "", string $db_host = "localhost")
26
    {
27
        $this->db_name = $db_name;
28
        $this->db_user = $db_user;
29
        $this->db_pass = $db_pass;
30
        $this->db_host = $db_host;
31
    }
32
33
    /**
34
     * Get PDO - connect to the server
35
     * @return PDO
36
     */
37
    private function getPDO(): PDO
38
    {
39
        if ($this->pdo === \null) {
40
            $pdo = new PDO("mysql:dbname={$this->db_name};host={$this->db_host}", $this->db_user, $this->db_pass);
41
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
42
            $this->pdo = $pdo;
43
        }
44
        return $this->pdo;
45
    }
46
47
    /**
48
     * Query - auto manages the fetch mode & type
49
     * @param string $statement
50
     * @param null|string $class_name
51
     * @param null|bool $one
52
     * @return mixed
53
     */
54
    public function query(string $statement, ?string $class_name = \null, ?bool $one = \false)
55
    {
56
        $req = $this->getPDO()->query($statement);
57
58
        if (
59
            \strpos($statement, "UPDATE") === 0 ||
60
            \strpos($statement, "INSERT") === 0 ||
61
            \strpos($statement, "DELETE") === 0
62
        ) {
63
            return $req;
64
        }
65
66
        if (\strpos($statement, 'COUNT')) {
67
            return $req->fetchColumn();
68
        }
69
70
        $class_name === \null ? $req->setFetchMode(PDO::FETCH_OBJ) : $req->setFetchMode(PDO::FETCH_CLASS, $class_name);
71
72
        $datas = ($one === \true) ? $req->fetch() : $req->fetchAll();
73
74
        return $datas;
75
    }
76
77
    /**
78
     * Prepare - auto manages the fetch mode & type
79
     * @param string $statement
80
     * @param array $attributes
81
     * @param null|string $class_name
82
     * @param null|bool $one
83
     * @return mixed
84
     * @throws PDOException
85
     */
86
    public function prepare(string $statement, array $attributes, ?string $class_name = \null, ?bool $one = \false)
87
    {
88
        $req = $this->getPDO()->prepare($statement);
89
        foreach ($attributes as $key => $value) {
90
            if (is_int($value)) {
91
                $req->bindValue($key + 1, $value, PDO::PARAM_INT);
92
            } else {
93
                $req->bindValue($key + 1, $value, PDO::PARAM_STR);
94
            }
95
        }
96
        $res = $req->execute();
97
98
99
        if (
100
            \strpos($statement, "UPDATE") === 0 ||
101
            \strpos($statement, "INSERT") === 0 ||
102
            \strpos($statement, "DELETE") === 0
103
        ) {
104
            return $res;
105
        }
106
107
        if (\strpos($statement, 'COUNT')) {
108
            return $req->fetchColumn();
109
        }
110
111
        if ($class_name === \null) {
112
            $req->setFetchMode(PDO::FETCH_OBJ);
113
        } else {
114
            $req->setFetchMode(PDO::FETCH_CLASS, $class_name);
0 ignored issues
show
Bug introduced by
$class_name of type string is incompatible with the type integer expected by parameter $columnNumber of PDOStatement::setFetchMode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

114
            $req->setFetchMode(PDO::FETCH_CLASS, /** @scrutinizer ignore-type */ $class_name);
Loading history...
115
        }
116
117
        if ($one) {
118
            $datas = $req->fetch();
119
        } else {
120
            $datas = $req->fetchAll();
121
        }
122
        return $datas;
123
    }
124
}
125