Table   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 0
loc 73
c 0
b 0
f 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 3
A error() 0 4 1
A insert() 0 4 1
A update() 0 4 1
C save() 0 23 8
A delete() 0 4 1
1
<?php
2
namespace jrdev\MySQL;
3
4
class Table
5
{
6
    private $tableName = '';
7
8
    private $pkName = 'id';
9
10
    private $cMySQL = null;
11
12
    public function __construct(\jrdev\MySQL &$cMySQL, $tableName, $tableArgs = [])
13
    {
14
        $this->cMySQL = $cMySQL;
15
        $this->tableName = $tableName;
16
17
        foreach (['pkName', 'tableName'] as $value) {
18
            if (isset($tableArgs[$value])) {
19
                $this->$value = $tableArgs[$value];
20
            }
21
        }
22
    }
23
24
    public function error()
25
    {
26
        return $this->cMySQL->error();
27
    }
28
29
    public function insert($fields)
30
    {
31
        return $this->cMySQL->insert($this->tableName, $fields);
32
    }
33
34
    public function update($fields, $where, $limit = null)
35
    {
36
        return $this->cMySQL->update($this->tableName, $fields, $where, $limit);
37
    }
38
39
    /**
40
     * save
41
     *
42
     * This insert or update a row in the table. If the `id` is in the fields list,
43
     * it will makes an update, otherwise makes an insert.
44
     *
45
     * @param array $fields The fields
46
     * @return int|false The ID of the row that was inserted or updated. Or False on failure.
47
     */
48
    public function save($fields)
49
    {
50
        $result = false;
51
        $method = isset($fields[$this->pkName])? 'update' : 'insert';
52
53
        if ($method === 'update') {
54
            $updated = $this->update($fields, $fields[$this->pkName], 1);
55
56
            if (is_int($updated) && $updated === 1) {
57
                $result = $fields[$this->pkName];
58
            }
59
        }
60
61
        if ($method === 'insert') {
62
            $inserted = $this->insert($fields);
63
64
            if (is_int($inserted) && $inserted > 0) {
65
                $result = $inserted;
66
            }
67
        }
68
69
        return $result;
70
    }
71
72
    public function delete($where, $limit = null)
73
    {
74
        return $this->cMySQL->delete($this->tableName, $where, $limit);
75
    }
76
}
77