Conditions | 5 |
Paths | 8 |
Total Lines | 74 |
Code Lines | 44 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
156 | function emitir_boleto($venda_id) { |
||
157 | $AsaasController = new AsaasController; |
||
158 | |||
159 | $this->loadModel('Venda'); |
||
160 | |||
161 | $venda = $this->Venda->find('first', array( |
||
162 | 'conditions' => array( |
||
163 | 'Venda.id' => $venda_id |
||
164 | ) |
||
165 | ) |
||
166 | ); |
||
167 | |||
168 | $this->loadModel('Cliente'); |
||
169 | |||
170 | $cliente = $this->Cliente->find('first', array( |
||
171 | 'conditions' => array( |
||
172 | 'Cliente.id' => $venda['Venda']['cliente_id'] |
||
173 | ) |
||
174 | ) |
||
175 | ); |
||
176 | |||
177 | if (empty($cliente['Cliente']['asaas_id'])) { |
||
178 | $cliente_data = array( |
||
179 | "name" => $cliente['Cliente']['nome1'] . ' ' . $cliente['Cliente']['nome2'], |
||
180 | "email" => $cliente['Cliente']['email'], |
||
181 | "cpfCnpj" => $cliente['Cliente']['documento1'], |
||
182 | "externalReference" => $cliente['Cliente']['id'] |
||
183 | ); |
||
184 | |||
185 | $response = $AsaasController->criar_cliente($cliente_data); |
||
186 | |||
187 | $asaas_id = $response->id; |
||
188 | |||
189 | $this->Cliente->id = $cliente['Cliente']['id']; |
||
190 | $this->Cliente->save(['asaas_id' => $asaas_id]); |
||
191 | } else { |
||
192 | $asaas_id = $cliente['Cliente']['asaas_id']; |
||
193 | } |
||
194 | |||
195 | $venda = array( |
||
196 | "customer" => $asaas_id, |
||
197 | "billingType" => "BOLETO", |
||
198 | "dueDate" => date('Y-m-d'), |
||
199 | "value" => $venda['Venda']['valor'], |
||
200 | "description" => "Boleto referente a venda no sistema winners REFº " . $venda['Venda']['id'], |
||
201 | "externalReference" => $venda['Venda']['id'] |
||
202 | ); |
||
203 | |||
204 | $response = $AsaasController->criar_cobranca($venda); |
||
205 | |||
206 | if (isset($response->errors)) { |
||
207 | foreach ($response->errors as $i => $error) { |
||
208 | $this->Session->setFlash($error->description); |
||
209 | } |
||
210 | |||
211 | return $this->redirect('/cliente/listar_pedidos/' . $cliente['Cliente']['id']); |
||
212 | } |
||
213 | |||
214 | $this->Venda->id = $venda_id; |
||
215 | |||
216 | $data = [ |
||
217 | 'asaas_boleto' => $response->bankSlipUrl, |
||
218 | 'asaas_status' => $response->status, |
||
219 | 'asaas_transaction_id' => $response->id |
||
220 | ]; |
||
221 | |||
222 | if (!$this->Venda->save($data)) { |
||
223 | $this->Session->setFlash('Erro ao emitir cobrança!'); |
||
224 | return $this->redirect('/cliente/listar_pedidos/' . $cliente['Cliente']['id']); |
||
225 | } |
||
226 | |||
227 | $this->Session->setFlash('Cobrança emitida com sucesso!'); |
||
228 | return $this->redirect('/cliente/listar_pedidos/' . $cliente['Cliente']['id']); |
||
229 | } |
||
230 | |||
231 | } |