Table   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 16
eloc 54
c 4
b 0
f 2
dl 0
loc 173
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A all() 0 3 1
A __construct() 0 7 2
A find() 0 6 1
A countRows() 0 5 2
A query() 0 6 2
A extract() 0 8 2
A delete() 0 6 1
A offset() 0 9 1
A update() 0 17 2
A create() 0 16 2
1
<?php
2
3
namespace Core\Table;
4
5
use Core\Services\Database\MysqlDatabase;
6
use PDOException;
7
8
class Table
9
{
10
    public $table;
11
    protected MysqlDatabase $db;
12
13
    public function __construct(MysqlDatabase $db)
14
    {
15
        $this->db = $db;
16
        if (is_null($this->table)) {
0 ignored issues
show
introduced by
The condition is_null($this->table) is always false.
Loading history...
17
            $parts = explode('\\', \get_class($this));
18
            $class_name = \end($parts);
19
            $this->table = \strtolower(\str_replace('Table', '', $class_name));
20
        }
21
    }
22
23
    /**
24
     * Simplifies SQL query preparation
25
     * @param string $statement
26
     * @param null|array $attributes
27
     * @param null|bool $one
28
     * @return mixed
29
     * @throws PDOException
30
     */
31
    public function query(string $statement, ?array $attributes = \null, ?bool $one = \false)
32
    {
33
        if ($attributes) {
34
            return $this->db->prepare($statement, $attributes, \str_replace('Table', 'Entity', \get_called_class()), $one);
35
        }
36
        return $this->db->query($statement, \str_replace('Table', 'Entity', \get_called_class()), $one);
37
    }
38
39
    /**
40
     * Fetches all the elements of a Table
41
     * @return mixed
42
     * @throws PDOException
43
     */
44
    public function all()
45
    {
46
        return $this->query("SELECT * FROM {$this->table}");
47
    }
48
49
    /**
50
     * Counts the number of rows in a table
51
     * @param null|string $statement
52
     * @param null|array $attributes
53
     * @return int
54
     * @throws PDOException
55
     */
56
    public function countRows(?string $statement = \null, ?array $attributes = \null): int
57
    {
58
        $statement = empty($statement) ? "SELECT COUNT(id) FROM {$this->table}" : \preg_replace('/SELECT.*FROM/im', "SELECT COUNT({$this->table[0]}.id) FROM", $statement);
59
60
        return $this->query($statement, $attributes);
61
    }
62
63
    /**
64
     * Fetch one element from a Table
65
     * @param int $id
66
     * @return mixed
67
     * @throws PDOException
68
     */
69
    public function find(int $id)
70
    {
71
        return $this->query(
72
            "SELECT * FROM {$this->table} WHERE id = ?",
73
            [$id],
74
            \true
75
        );
76
    }
77
78
    /**
79
     * Get the last X rows
80
     * @param string $statement
81
     * @param null|array $attributes
82
     * @param int $limit $limit number of post to return
83
     * @param int $page $page current page number
84
     * @return mixed
85
     * @throws PDOException
86
     */
87
    public function offset(string $statement, ?array $attributes = [], int $limit, int $page)
88
    {
89
        $offset = strval(($page - 1) * $limit);
90
        $attributes[] = \intval($offset);
91
        $attributes[] = \intval($limit);
92
93
        return $this->query(
94
            $statement . " LIMIT ?, ?",
95
            $attributes
96
        );
97
    }
98
99
    /**
100
     * Add an element to the Table
101
     * @param array $fields
102
     * @return mixed
103
     * @throws PDOException
104
     */
105
    public function create(array $fields)
106
    {
107
        $sql_parts = [];
108
        $attributes = [];
109
110
        foreach ($fields as $k => $v) {
111
            $sql_parts[] = "$k = ?";
112
            $attributes[] = $v;
113
        }
114
115
        $sql_part = implode(', ', $sql_parts);
116
117
        return $this->query(
118
            "INSERT INTO {$this->table} SET {$sql_part}",
119
            $attributes,
120
            \true
121
        );
122
    }
123
124
    /**
125
     * Edit an element to the Table
126
     * @param int $id
127
     * @param array $fields
128
     * @return mixed
129
     * @throws PDOException
130
     */
131
    public function update(int $id, array  $fields)
132
    {
133
        $sql_parts = [];
134
        $attributes = [];
135
136
        foreach ($fields as $k => $v) {
137
            $sql_parts[] = "$k = ?";
138
            $attributes[] = $v;
139
        }
140
        $attributes[] = $id;
141
142
        $sql_part = implode(', ', $sql_parts);
143
144
        return $this->query(
145
            "UPDATE {$this->table} SET {$sql_part} WHERE id = ?",
146
            $attributes,
147
            \true
148
        );
149
    }
150
151
    /**
152
     * Delet an element to the Table
153
     * @param int $id
154
     * @return mixed
155
     * @throws PDOException
156
     */
157
    public function delete(int $id)
158
    {
159
        return $this->query(
160
            "DELETE FROM {$this->table} WHERE id = ?",
161
            [$id],
162
            \true
163
        );
164
    }
165
166
    /**
167
     * Extracting properties from an Object
168
     * @param string $key
169
     * @param string $value
170
     * @return array
171
     * @throws PDOException
172
     */
173
    public function extract(string $key, string  $value)
174
    {
175
        $records = $this->all();
176
        $return = [];
177
        foreach ($records as $v) {
178
            $return[$v->$key] = $v->$value;
179
        }
180
        return $return;
181
    }
182
}
183