Test Failed
Push — master ( ce96ba...918ead )
by Joao
01:53
created

Updatable   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 169
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
eloc 62
dl 0
loc 169
ccs 66
cts 72
cp 0.9167
rs 10
c 0
b 0
f 0
wmc 21

9 Methods

Rating   Name   Duplication   Size   Complexity  
A where() 0 4 1
A getInstance() 0 3 1
A getFields() 0 7 2
A fields() 0 5 1
A getWhere() 0 15 3
A table() 0 5 1
A buildUpdate() 0 32 6
A buildInsert() 0 23 4
A buildDelete() 0 13 2
1
<?php
2
3
namespace ByJG\MicroOrm;
4
5
use ByJG\AnyDataset\Db\DbFunctionsInterface;
6
use ByJG\MicroOrm\Exception\InvalidArgumentException;
7
use ByJG\MicroOrm\Exception\OrmInvalidFieldsException;
8
9
class Updatable
10
{
11
    protected $fields = [];
12
    protected $table = "";
13 11
    protected $where = [];
14
15 11
    public static function getInstance()
16
    {
17
        return new Updatable();
18
    }
19
20
    /**
21
     * Example:
22
     *   $query->fields(['name', 'price']);
23
     *
24
     * @param array $fields
25 11
     * @return $this
26
     */
27 11
    public function fields(array $fields)
28
    {
29 11
        $this->fields = array_merge($this->fields, (array)$fields);
30
        
31
        return $this;
32
    }
33
34
    /**
35
     * Example
36
     *    $query->table('product');
37
     *
38
     * @param string $table
39 16
     * @return $this
40
     */
41 16
    public function table($table)
42
    {
43 16
        $this->table = $table;
44
45
        return $this;
46
    }
47
48
    /**
49
     * Example:
50
     *    $query->filter('price > [[amount]]', [ 'amount' => 1000] );
51
     *
52
     * @param string $filter
53
     * @param array $params
54 8
     * @return $this
55
     */
56 8
    public function where($filter, array $params = [])
57 8
    {
58
        $this->where[] = [ 'filter' => $filter, 'params' => $params  ];
59
        return $this;
60
    }
61
62
    protected function getFields()
63
    {
64
        if (empty($this->fields)) {
65
            return ' * ';
66
        }
67
68
        return ' ' . implode(', ', $this->fields) . ' ';
69 10
    }
70
    
71 10
    protected function getWhere()
72 10
    {
73
        $whereStr = [];
74 10
        $params = [];
75 8
76 8
        foreach ($this->where as $item) {
77 10
            $whereStr[] = $item['filter'];
78
            $params = array_merge($params, $item['params']);
79 10
        }
80 2
        
81
        if (empty($whereStr)) {
82
            return null;
83 8
        }
84
        
85
        return [ implode(' AND ', $whereStr), $params ];
86
    }
87
88
89
    /**
90
     * @param $params
91
     * @param \ByJG\AnyDataset\Db\DbFunctionsInterface|null $dbHelper
92 6
     * @return null|string|string[]
93
     * @throws \ByJG\MicroOrm\Exception\OrmInvalidFieldsException
94 6
     */
95
    public function buildInsert(&$params, DbFunctionsInterface $dbHelper = null)
96
    {
97
        if (empty($this->fields)) {
98 6
            throw new OrmInvalidFieldsException('You must specifiy the fields for insert');
99 6
        }
100 6
101 6
        $fieldsStr = $this->fields;
102
        if (!is_null($dbHelper)) {
103 6
            $fieldsStr = $dbHelper->delimiterField($fieldsStr);
104 6
        }
105 6
106 6
        $tableStr = $this->table;
107
        if (!is_null($dbHelper)) {
108
            $tableStr = $dbHelper->delimiterTable($tableStr);
109
        }
110 6
111 6
        $sql = 'INSERT INTO '
112 6
            . $tableStr
113
            . '( ' . implode(', ', $fieldsStr) . ' ) '
114 6
            . ' values '
115
            . '( [[' . implode(']], [[', $this->fields) . ']] ) ';
116 6
117
        return ORMHelper::processLiteral($sql, $params);
118
    }
119
120
    /**
121
     * @param $params
122
     * @param \ByJG\AnyDataset\Db\DbFunctionsInterface|null $dbHelper
123
     * @return array
124
     * @throws \ByJG\MicroOrm\Exception\InvalidArgumentException
125 5
     */
126
    public function buildUpdate(&$params, DbFunctionsInterface $dbHelper = null)
127 5
    {
128
        if (empty($this->fields)) {
129
            throw new InvalidArgumentException('You must specifiy the fields for insert');
130
        }
131 5
        
132 5
        $fieldsStr = [];
133 5
        foreach ($this->fields as $field) {
134 5
            $fieldName = $field;
135 4
            if (!is_null($dbHelper)) {
136 4
                $fieldName = $dbHelper->delimiterField($fieldName);
137 5
            }
138 5
            $fieldsStr[] = "$fieldName = [[$field]] ";
139
        }
140 5
        
141 5
        $whereStr = $this->getWhere();
142 1
        if (is_null($whereStr)) {
143
            throw new InvalidArgumentException('You must specifiy a where clause');
144
        }
145 4
146 4
        $tableName = $this->table;
147 4
        if (!is_null($dbHelper)) {
148 4
            $tableName = $dbHelper->delimiterTable($tableName);
149
        }
150 4
151 4
        $sql = 'UPDATE ' . $tableName . ' SET '
152 4
            . implode(', ', $fieldsStr)
153
            . ' WHERE ' . $whereStr[0];
154 4
155
        $params = array_merge($params, $whereStr[1]);
156 4
157
        return ORMHelper::processLiteral($sql, $params);
0 ignored issues
show
Bug Best Practice introduced by
The expression return ByJG\MicroOrm\ORM...sLiteral($sql, $params) returns the type string which is incompatible with the documented return type array.
Loading history...
158 4
    }
159
160
    /**
161
     * @param $params
162
     * @return array
163
     * @throws \ByJG\MicroOrm\Exception\InvalidArgumentException
164
     */
165
    public function buildDelete(&$params)
166 5
    {
167
        $whereStr = $this->getWhere();
168 5
        if (is_null($whereStr)) {
169 5
            throw new InvalidArgumentException('You must specifiy a where clause');
170 1
        }
171
172
        $sql = 'DELETE FROM ' . $this->table
173 4
            . ' WHERE ' . $whereStr[0];
174 4
175
        $params = array_merge($params, $whereStr[1]);
176 4
177
        return ORMHelper::processLiteral($sql, $params);
0 ignored issues
show
Bug Best Practice introduced by
The expression return ByJG\MicroOrm\ORM...sLiteral($sql, $params) returns the type string which is incompatible with the documented return type array.
Loading history...
178 4
    }
179
}
180