Passed
Push — master ( ecf49a...4e3a77 )
by Joao
02:50 queued 27s
created

Updatable::buildInsert()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4.0039

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 15
cts 16
cp 0.9375
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 16
nc 5
nop 2
crap 4.0039
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: jg
5
 * Date: 21/06/16
6
 * Time: 12:01
7
 */
8
9
namespace ByJG\MicroOrm;
10
11
12
use ByJG\AnyDataset\DbFunctionsInterface;
13
14
class Updatable
15
{
16
    protected $fields = [];
17
    protected $table = "";
18
    protected $where = [];
19
20 11
    public static function getInstance()
21
    {
22 11
        return new Updatable();
23
    }
24
25
    /**
26
     * Example:
27
     *   $query->fields(['name', 'price']);
28
     * 
29
     * @param array $fields
30
     * @return $this
31
     */
32 11
    public function fields(array $fields)
33
    {
34 11
        $this->fields = array_merge($this->fields, (array)$fields);
35
        
36 11
        return $this;
37
    }
38
39
    /**
40
     * Example
41
     *    $query->table('product');
42
     * 
43
     * @param string $table
44
     * @return $this
45
     */
46 16
    public function table($table) 
47
    {
48 16
        $this->table = $table;
49
50 16
        return $this;
51
    }
52
53
    /**
54
     * Example:
55
     *    $query->filter('price > [[amount]]', [ 'amount' => 1000] );
56
     * 
57
     * @param string $filter
58
     * @param array $params
59
     * @return $this
60
     */
61 8
    public function where($filter, array $params = [])
62
    {
63 8
        $this->where[] = [ 'filter' => $filter, 'params' => $params  ];
64 8
        return $this;
65
    }
66
67 View Code Duplication
    protected function getFields()
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...
68
    {
69
        if (empty($this->fields)) {
70
            return ' * ';
71
        }
72
73
        return ' ' . implode(', ', $this->fields) . ' ';
74
    }
75
    
76 10 View Code Duplication
    protected function getWhere()
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...
77
    {
78 10
        $where = [];
79 10
        $params = [];
80
81 10
        foreach ($this->where as $item) {
82 8
            $where[] = $item['filter'];
83 8
            $params = array_merge($params, $item['params']);
84 10
        }
85
        
86 10
        if (empty($where)) {
87 2
            return null;
88
        }
89
        
90 8
        return [ implode(' AND ', $where), $params ];
91
    }
92
93
    /**
94
     * @param \ByJG\AnyDataset\DbFunctionsInterface|null $dbHelper
95
     * @param $params
96
     * @return string
97
     * @throws \Exception
98
     */
99 6
    public function buildInsert(&$params, DbFunctionsInterface $dbHelper = null)
100
    {
101 6
        if (empty($this->fields)) {
102
            throw new \Exception('You must specifiy the fields for insert');
103
        }
104
105 6
        $fields = $this->fields;
106 6
        if (!is_null($dbHelper)) {
107 6
            $fields = $dbHelper->delimiterField($fields);
108 6
        }
109
110 6
        $table = $this->table;
111 6
        if (!is_null($dbHelper)) {
112 6
            $table = $dbHelper->delimiterTable($table);
113 6
        }
114
115
        $sql = 'INSERT INTO '
116
            . $table
117 6
            . '( ' . implode(', ', $fields) . ' ) '
118 6
            . ' values '
119 6
            . '( [[' . implode(']], [[', $this->fields) . ']] ) ';
120
121 6
        $sql = ORMHelper::processLiteral($sql, $params);
122
123 6
        return $sql;
124
    }
125
126
    /**
127
     * @param \ByJG\AnyDataset\DbFunctionsInterface|null $dbHelper
128
     * @param $params
129
     * @return array
130
     * @throws \Exception
131
     */
132 5
    public function buildUpdate(&$params, DbFunctionsInterface $dbHelper = null)
133
    {
134 5
        if (empty($this->fields)) {
135
            throw new \InvalidArgumentException('You must specifiy the fields for insert');
136
        }
137
        
138 5
        $fields = [];
139 5
        foreach ($this->fields as $field) {
140 5
            $fieldName = $field;
141 5
            if (!is_null($dbHelper)) {
142 4
                $fieldName = $dbHelper->delimiterField($fieldName);
143 4
            }
144 5
            $fields[] = "$fieldName = [[$field]] ";
145 5
        }
146
        
147 5
        $where = $this->getWhere();
148 5
        if (is_null($where)) {
149 1
            throw new \InvalidArgumentException('You must specifiy a where clause');
150
        }
151
152 4
        $tableName = $this->table;
153 4
        if (!is_null($dbHelper)) {
154 4
            $tableName = $dbHelper->delimiterTable($tableName);
155 4
        }
156
157 4
        $sql = 'UPDATE ' . $tableName . ' SET '
158 4
            . implode(', ', $fields)
159 4
            . ' WHERE ' . $where[0];
160
161 4
        $params = array_merge($params, $where[1]);
162
163 4
        $sql = ORMHelper::processLiteral($sql, $params);
164
165 4
        return $sql;
166
    }
167
168
    /**
169
     * @param $params
170
     * @return array
171
     * @throws \Exception
172
     */
173 5
    public function buildDelete(&$params)
174
    {
175 5
        $where = $this->getWhere();
176 5
        if (is_null($where)) {
177 1
            throw new \InvalidArgumentException('You must specifiy a where clause');
178
        }
179
180 4
        $sql = 'DELETE FROM ' . $this->table
181 4
            . ' WHERE ' . $where[0];
182
183 4
        $params = array_merge($params, $where[1]);
184
185 4
        $sql = ORMHelper::processLiteral($sql, $params);
186
187 4
        return $sql;
188
    }
189
}
190