Conditions | 14 |
Paths | 126 |
Total Lines | 98 |
Code Lines | 64 |
Lines | 18 |
Ratio | 18.37 % |
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 |
||
183 | private function enviarRTC($email, $empresa, $dte, $token, $retry = null) |
||
184 | { |
||
185 | // definir datos que se usarán en el envío |
||
186 | list($rutCompany, $dvCompany) = explode('-', str_replace('.', '', $empresa)); |
||
187 | if (strpos($dte, '<?xml') === false) { |
||
188 | $dte = '<?xml version="1.0" encoding="ISO-8859-1"?>' . "\n" . $dte; |
||
189 | } |
||
190 | View Code Duplication | do { |
|
191 | $file = sys_get_temp_dir() . '/aec_' . md5(microtime() . $token . $dte) . '.xml'; |
||
192 | } while (file_exists($file)); |
||
193 | file_put_contents($file, $dte); |
||
194 | $data = [ |
||
195 | 'emailNotif' => $email, |
||
196 | 'rutCompany' => $rutCompany, |
||
197 | 'dvCompany' => $dvCompany, |
||
198 | 'archivo' => curl_file_create( |
||
199 | $file, |
||
200 | 'application/xml', |
||
201 | basename($file) |
||
202 | ), |
||
203 | ]; |
||
204 | // crear sesión curl con sus opciones |
||
205 | $curl = curl_init(); |
||
206 | $header = [ |
||
207 | 'User-Agent: Mozilla/4.0 (compatible; PROG 1.0; LibreDTE)', |
||
208 | 'Referer: https://libredte.cl', |
||
209 | 'Cookie: TOKEN='.$token, |
||
210 | ]; |
||
211 | $url = 'https://'.\sasco\LibreDTE\Sii::getServidor().'.sii.cl/cgi_rtc/RTC/RTCAnotEnvio.cgi'; |
||
212 | curl_setopt($curl, CURLOPT_POST, true); |
||
213 | curl_setopt($curl, CURLOPT_POSTFIELDS, $data); |
||
214 | curl_setopt($curl, CURLOPT_HTTPHEADER, $header); |
||
215 | curl_setopt($curl, CURLOPT_URL, $url); |
||
216 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
||
217 | // si no se debe verificar el SSL se asigna opción a curl, además si |
||
218 | // se está en el ambiente de producción y no se verifica SSL se |
||
219 | // generará una entrada en el log |
||
220 | View Code Duplication | if (!\sasco\LibreDTE\Sii::getVerificarSSL()) { |
|
221 | if (\sasco\LibreDTE\Sii::getAmbiente()==\sasco\LibreDTE\Sii::PRODUCCION) { |
||
222 | \sasco\LibreDTE\Log::write( |
||
223 | \sasco\LibreDTE\Estado::ENVIO_SSL_SIN_VERIFICAR, |
||
224 | \sasco\LibreDTE\Estado::get(\sasco\LibreDTE\Estado::ENVIO_SSL_SIN_VERIFICAR), |
||
225 | LOG_WARNING |
||
226 | ); |
||
227 | } |
||
228 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); |
||
229 | } |
||
230 | // enviar XML al SII |
||
231 | View Code Duplication | for ($i = 0; $i < $retry; $i++) { |
|
232 | $response = curl_exec($curl); |
||
233 | if ($response and $response != 'Error 500') |
||
234 | break; |
||
235 | } |
||
236 | unlink($file); |
||
237 | // verificar respuesta del envío y entregar error en caso que haya uno |
||
238 | if (!$response or $response == 'Error 500') { |
||
239 | if (!$response) |
||
240 | \sasco\LibreDTE\Log::write(\sasco\LibreDTE\Estado::ENVIO_ERROR_CURL, \sasco\LibreDTE\Estado::get(\sasco\LibreDTE\Estado::ENVIO_ERROR_CURL, curl_error($curl))); |
||
241 | if ($response == 'Error 500') |
||
242 | \sasco\LibreDTE\Log::write(\sasco\LibreDTE\Estado::ENVIO_ERROR_500, \sasco\LibreDTE\Estado::get(\sasco\LibreDTE\Estado::ENVIO_ERROR_500)); |
||
243 | return false; |
||
244 | } |
||
245 | // cerrar sesión curl |
||
246 | curl_close($curl); |
||
247 | // crear XML con la respuesta y retornar |
||
248 | try { |
||
249 | $xml = new \SimpleXMLElement($response, LIBXML_COMPACT); |
||
250 | } catch (Exception $e) { |
||
251 | \sasco\LibreDTE\Log::write(\sasco\LibreDTE\Estado::ENVIO_ERROR_XML, \sasco\LibreDTE\Estado::get(\sasco\LibreDTE\Estado::ENVIO_ERROR_XML, $e->getMessage())); |
||
252 | return false; |
||
253 | } |
||
254 | /* |
||
255 | * 0 Envío recibido OK. |
||
256 | * 1 Rut usuario autenticado no tiene permiso para enviar en empresa Cedente. |
||
257 | * 2 Error en tamaño del archivo enviado. |
||
258 | * 4 Faltan parámetros de entrada. |
||
259 | * 5 Error de autenticación, TOKEN inválido, no existe o está expirado. |
||
260 | * 6 Empresa no es DTE. |
||
261 | * 9 Error Interno. |
||
262 | * 10 Error Interno |
||
263 | */ |
||
264 | $error = [ |
||
265 | 1 => 'Rut usuario autenticado no tiene permiso para enviar en empresa Cedente', |
||
266 | 2 => 'Error en tamaño del archivo enviado', |
||
267 | 4 => 'Faltan parámetros de entrada', |
||
268 | 5 => 'Error de autenticación, TOKEN inválido, no existe o está expirado', |
||
269 | 6 => 'Empresa no es DTE', |
||
270 | 9 => 'Error Interno', |
||
271 | 10 => 'Error Interno' |
||
272 | ]; |
||
273 | if ($xml->STATUS != 0) { |
||
274 | \sasco\LibreDTE\Log::write( |
||
275 | $xml->STATUS, |
||
276 | $error[$xml->STATUS] |
||
277 | ); |
||
278 | } |
||
279 | return $xml; |
||
280 | } |
||
281 | |||
283 |
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.