Completed
Push — master ( 98a82f...7bd861 )
by Márcio Lucas R.
11s
created

PhiberQueryWriter::update()   C

Complexity

Conditions 8
Paths 47

Size

Total Lines 38
Code Lines 23

Duplication

Lines 5
Ratio 13.16 %

Importance

Changes 0
Metric Value
dl 5
loc 38
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 23
nc 47
nop 1
1
<?php
2
3
/**
4
 * Copyright (c) 2017. Este código foi feito por @marciioluucas, sob licença MIT
5
 */
6
namespace Phiber\ORM\Queries;
7
8
use Phiber\ORM\Exceptions\PhiberException;
9
use Phiber\ORM\Interfaces\IPhiberQueryBuilder;
10
use Phiber\Util\Internationalization;
11
use ReflectionMethod;
12
13
/**
14
 * Classe responsável por escrever os SQLs
15
 * 
16
 * @package bin
17
 */
18
class PhiberQueryWriter implements IPhiberQueryBuilder
19
{
20
    /**
21
     * @var string
22
     */
23
    private $sql;
24
25
    /**
26
     * PhiberQueryWriter constructor.
27
     * @param $method
28
     * @param $infos
29
     */
30
    public function __construct($method, $infos)
31
    {
32
        $reflectionMethod = new ReflectionMethod(get_class($this), $method);
33
        $reflectionMethod->invoke($this, $infos);
34
    }
35
36
    /**
37
     * @return mixed
38
     */
39
    function __toString()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
40
    {
41
        return $this->sql;
42
    }
43
44
    /**
45
     * Responsável por criar a quesry string.
46
     * 
47
     * @param $infos
48
     * @return string
49
     * @throws PhiberException
50
     * @internal param $object
51
     */
52
    public function create($infos)
53
    {
54
        $tabela = $infos['table'];
55
        $campos = $infos['fields'];
56
        $camposV = $infos['values'];
57
        try {
58
            $camposNome = [];
59
60 View Code Duplication
            for ($i = 0; $i < count($campos); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
Duplication introduced by
This code seems to be duplicated across 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...
61
                if ($camposV[$i] != null) {
62
                    $camposNome[$i] = $campos[$i];
63
                }
64
            }
65
66
            $camposNome = array_values($camposNome);
67
            $this->sql = "INSERT INTO $tabela (" . implode(", ", $camposNome) . ") VALUES (";
68
69
            for ($j = 0; $j < count($camposNome); $j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
70
                if ($j != count($camposNome) - 1) {
71
                    $this->sql .= ":" . $camposNome[$j] . ", ";
72
                } else if ($j == count($camposNome) - 1) {
73
                    $this->sql .= ":" . $camposNome[$j] . ")";
74
                }
75
            }
76
            return $this->sql;
77
78
        } catch (PhiberException $e) {
79
            throw new PhiberException(new Internationalization("query_processor_error"));
80
        }
81
    }
82
83
    /**
84
     * Faz a query de update de um registro no banco com os dados.
85
     * 
86
     * @param $infos
87
     * @return mixed
88
     * @throws PhiberException
89
     * @internal param $object
90
     * @internal param $id
91
     */
92
    public function update($infos)
93
    {
94
        $tabela        = $infos['table'];
95
        $campos        = $infos['fields'];
96
        $camposV       = $infos['values'];
97
        $whereCriteria = $infos['where'];
98
99
        try {
100
            $camposNome = [];
101 View Code Duplication
            for ($i = 0; $i < count($campos); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
Duplication introduced by
This code seems to be duplicated across 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...
102
                if ($camposV[$i] != null) {
103
                    $camposNome[$i] = $campos[$i];
104
                }
105
            }
106
107
            $camposNome = array_values($camposNome);
108
            $this->sql = "UPDATE $tabela SET ";
109
110
            for ($i = 0; $i < count($camposNome); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
111
                if (!empty($camposNome[$i])) {
112
                    if ($i != count($camposNome) - 1) {
113
                        $this->sql .= $camposNome[$i] . " = :" . $camposNome[$i] . ", ";
114
                    } else {
115
                        $this->sql .= $camposNome[$i] . " = :" . $camposNome[$i];
116
                    }
117
                }
118
            }
119
120
            if (!empty($whereCriteria)) {
121
                $this->sql .= " WHERE " . $whereCriteria . " ";
122
            }
123
124
            return $this->sql . ";";
125
126
        } catch (PhiberException $e) {
127
            throw new PhiberException(new Internationalization("query_processor_error"));
128
        }
129
    }
130
131
    /**
132
     * Faz a query de delete de um registro no banco com os dados.
133
     * 
134
     * @param    array $infos
135
     * @return   bool|string
136
     * @throws   PhiberException
137
     * @internal param $object
138
     * @internal param array $conditions
139
     * @internal param array $conjunctions
140
     */
141
    public function delete(array $infos)
142
    {
143
        $tabela        = $infos['table'];
144
        $whereCriteria = $infos['where'];
145
146
        try {
147
            $this->sql = "DELETE FROM $tabela ";
148
            if (!empty($whereCriteria)) {
149
                $this->sql .= " WHERE " . $whereCriteria . " ";
150
            }
151
            
152
            return $this->sql . ";";
153
154
        } catch (PhiberException $e) {
0 ignored issues
show
Unused Code introduced by
catch (\Phiber\ORM\Excep...y_processor_error')); } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
155
            throw new PhiberException(new Internationalization("query_processor_error"));
156
        }
157
    }
158
159
    /**
160
     * Faz a query de select de um registro no banco com os dados.
161
     * 
162
     * @param  array $infos
163
     * @return string
164
     * @throws PhiberException
165
     */
166
    public function select(array $infos)
167
    {
168
        try {
169
170
            $tabela = $infos['table'];
171
            $campos = $infos['fields'];
172
173
            $whereCriteria   = $infos['where'];
174
            $joins           = $infos['join'];
175
            $limitCriteria   = $infos['limit'];
176
            $offsetCriteria  = $infos['offset'];
177
            $orderByCriteria = $infos['orderby'];
178
179
            $campos = gettype($campos) == "array" ? implode(", ", $campos) : $campos;
180
181
            $this->sql = "SELECT " . $campos . " FROM $tabela";
182
183
            // responsável por montar JOIN
184
            if (!empty($joins)) {
185
                for ($i = 0; $i < count($joins); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
186
                    $this->sql .= " " . $joins[$i] . " ";
187
                }
188
            }
189
190
            // responsável por montar o WHERE.
191
            if (!empty($whereCriteria)) {
192
                $this->sql .= " WHERE " . $whereCriteria;
193
            }
194
195
            // responsável por montar o order by.
196
            if (!is_null($orderByCriteria)) {
197
                $orderBy = gettype($orderByCriteria) == "array" ? implode(", ", $orderByCriteria) : $orderByCriteria;
198
199
                $this->sql .= " ORDER BY " . $orderBy;
200
            }
201
202
            // responsável por montar o limit.
203
            if (!empty($limitCriteria)) {
204
                $this->sql .= " LIMIT " . $limitCriteria;
205
            }
206
207
            // responsável por montar OFFSET
208
            if (!empty($offsetCriteria)) {
209
210
                $this->sql .= " OFFSET " . $offsetCriteria;
211
            }
212
213
            $this->sql .= ";";
214
            
215
            return $this->sql;
216
217
        } catch (PhiberException $phiberException) {
218
            throw new PhiberException(new Internationalization("query_processor_error"));
219
        }
220
    }
221
}
222