Completed
Push — master ( 8ee8e5...542744 )
by Joe Nilson
03:39 queued 03:39
created

WebserviceDgii::buscar()   F

Complexity

Conditions 11
Paths 576

Size

Total Lines 64
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 48
nc 576
nop 2
dl 0
loc 64
rs 3.7388
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/*
3
 * Copyright (C) 2022 Joe Nilson <[email protected]>
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as
7
 * published by the Free Software Foundation, either version 3 of the
8
 * License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Lesser General Public License for more details.
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
namespace FacturaScripts\Plugins\fsRepublicaDominicana\Lib;
19
20
use FacturaScripts\Core\Base\DataBase;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Core\Base\DataBase was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Core\Base\DataBase\DataBaseWhere was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
use FacturaScripts\Dinamic\Model\Cliente;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Dinamic\Model\Cliente was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
23
use simplehtmldom\HtmlDocument;
0 ignored issues
show
Bug introduced by
The type simplehtmldom\HtmlDocument was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
25
class WebserviceDgii
26
{
27
    /**
28
     * @var string
29
     */
30
    public $wsdlDGII = 'https://www.dgii.gov.do/wsMovilDGII/WSMovilDGII.asmx?WSDL';
31
32
    public $searchInitiator = 'https://dgii.gov.do/herramientas/consultas/Paginas/RNC.aspx';
33
    public $searchProcessor = 'https://dgii.gov.do/app/WebApps/ConsultasWeb2/ConsultasWeb/consultas/rnc.aspx';
34
35
    private $viewState;
36
    private $viewStateGenerator;
37
    private $eventValidation;
38
39
    /**
40
     * @return void
41
     * @throws \SoapFault
42
     */
43
    public function wdslConnection(): object
44
    {
45
        return new \SoapClient($this->wsdlDGII, array('encoding' => 'UTF-8'));
0 ignored issues
show
Bug Best Practice introduced by
The expression return new SoapClient($t...'encoding' => 'UTF-8')) returns the type SoapClient which is incompatible with the documented return type void.
Loading history...
46
    }
47
48
    /**
49
     * @param object $wsdlConn
50
     * @param integer $patronBusqueda
51
     * @param string $paramValue
52
     * @param integer $inicioFilas
53
     * @param integer $filaFilas
54
     * @return void
55
     * @throws \JsonException
56
     */
57
    public function wdslSearch(
58
        int $patronBusqueda = 0,
59
        string $paramValue = '',
60
        int $inicioFilas = 1,
61
        int $filaFilas = 1
62
    ): string
63
    {
64
        $opciones = [
65
            'patronBusqueda' => $patronBusqueda,
66
            'value' => $paramValue,
67
            'inicioFilas' => $inicioFilas,
68
            'filaFilas' => $filaFilas,
69
            'IMEI' => 0
70
        ];
71
72
        $wsdlConn = $this->wdslConnection();
73
74
        $result = $wsdlConn->__soapCall('GetContribuyentes', ['GetContribuyentes' => $opciones]);
75
        $list = array();
0 ignored issues
show
Unused Code introduced by
The assignment to $list is dead and can be removed.
Loading history...
76
        $getResult = explode("@@@", $result->GetContribuyentesResult);
77
78
        return $getResult[0];
0 ignored issues
show
Bug Best Practice introduced by
The expression return $getResult[0] returns the type string which is incompatible with the documented return type void.
Loading history...
79
    }
80
81
    /**
82
     * @param object $wsdlConn
83
     * @param string $paramValue
84
     * @return object
85
     */
86
    public function wdslSearchCount(object $wsdlConn, string $paramValue = ''): object
87
    {
88
        $opciones = [
89
            'value' => $paramValue,
90
            'IMEI' => 0
91
        ];
92
93
        $result = $wsdlConn->__soapCall('GetContribuyentesCount', ['GetContribuyentesCount' => $opciones]);
94
        return $result->GetContribuyentesCountResult;
95
    }
96
97
    /**
98
     * @param object $item
99
     * @return void
100
     */
101
    public function buscarCliente(&$item): void
102
    {
103
        $item->existe = false;
104
        $item->codcliente = '';
105
        $cli = new Cliente();
106
        $where = [
107
            new DataBaseWhere('cifnif', $item->RGE_RUC)
108
        ];
109
        $cli->all($where);
110
        if ($cli[0] !== null) {
111
            $cliente = $cli[0];
112
            $item->existe = true;
113
            $item->codcliente = $cliente->codcliente;
114
        }
115
    }
116
117
    private function curlSearch($page, $postData = '')
118
    {
119
        $result = "";
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
120
        $h = curl_init();
121
        curl_setopt($h, CURLOPT_URL, $page);
122
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $curl seems to be never defined.
Loading history...
123
        curl_setopt($curl, CURLOPT_REFERER, $this->searchInitiator);
124
        if ($postData !== '') {
125
            curl_setopt($h, CURLOPT_POST, true);
126
            curl_setopt($h, CURLOPT_POSTFIELDS, $postData);
127
        }
128
129
        curl_setopt($h, CURLOPT_HEADER, false);
130
        curl_setopt($h, CURLOPT_RETURNTRANSFER, 1);
131
        $result = curl_exec($h);
132
        curl_close($h);
133
        return $result;
134
    }
135
136
    //Pedimos que nos den el VIEWSTATE y el EVENTVALIDATION a la página de busqueda
137
    public function autorizacionWeb(): void
138
    {
139
        $result = $this->curlSearch($this->searchInitiator);
140
        $html = new HtmlDocument();
141
        $html->load($result);
142
143
        $this->viewState = $html->getElementById('#__VIEWSTATE', 0)->value;
144
//        var_dump($html->getElementById('#__VIEWSTATE', 0)->value);
145
146
        $this->viewStateGenerator = $html->getElementById('#__VIEWSTATEGENERATOR', 0)->value;
147
148
        $this->eventValidation = $html->getElementById('#__EVENTVALIDATION', 0)->value;
149
//        var_dump($html->getElementById('#__EVENTVALIDATION', 0)->value);
150
    }
151
152
    //Si la busqueda no es por RNC y en su lugar es por nombre actualizamos viewstate y eventvalidation
153
    public function actualizarAutorizacion($tipoBusqueda)
154
    {
155
        $post = array(
156
            '__EVENTTARGET' => 'rbtnlTipoBusqueda$1',
157
            '__EVENTARGUMENT' => "",
158
            '__LASTFOCUS' => "",
159
            '__VIEWSTATE' => $this->viewState,
160
            '__VIEWSTATEGENERATOR' => $this->viewStateGenerator,
161
            '__EVENTVALIDATION' => $this->eventValidation,
162
            'rbtnlTipoBusqueda' => $tipoBusqueda,
163
            'txtRncCed' => ''
164
        );
165
166
        $query = http_build_query($post);
167
        $result = $this->curlSearch($this->searchProcessor, $query);
168
169
        $doc = new HtmlDocument();
170
        $html = $doc->load($result);
171
        $this->viewState = $html->getElementById('#__VIEWSTATE', 0)->value;
172
        $this->eventValidation = $html->getElementById('#__EVENTVALIDATION', 0)->value;
173
    }
174
175
    public function buscar($rnc = '', $nombre = '')
176
    {
177
        $resultados = '';
0 ignored issues
show
Unused Code introduced by
The assignment to $resultados is dead and can be removed.
Loading history...
178
        $this->autorizacionWeb();
179
        $tipoBusqueda = (!empty($rnc)) ? 0 : 1;
180
        $valorBuscar = (!empty($rnc)) ? $rnc : strtoupper(trim($nombre));
181
        $this->rnc = $rnc;
0 ignored issues
show
Bug Best Practice introduced by
The property rnc does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
182
        $this->nombre = $nombre;
0 ignored issues
show
Bug Best Practice introduced by
The property nombre does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
183
        $campo = (!empty($rnc)) ? 'ctl00$cphMain$txtRNCCedula' : 'ctl00$cphMain$txtRazonSocial';
184
        $smMain = (!empty($rnc)) ? 'ctl00$cphMain$upBusqueda|ctl00$cphMain$btnBuscarPorRNC': '';
185
        $boton = (!empty($rnc)) ? 'ctl00$cphMain$btnBuscarPorRNC' : 'ctl00$cphMain$btnBuscarPorRazonSocial';
186
187
        if ($tipoBusqueda === 1) {
188
            $this->actualizarAutorizacion($tipoBusqueda);
189
        }
190
191
        $post = array(
192
            '__EVENTTARGET' => "",
193
            '__EVENTARGUMENT' => "",
194
            '__LASTFOCUS' => "",
195
            '__VIEWSTATE' => $this->viewState,
196
            '__VIEWSTATEGENERATOR' => $this->viewStateGenerator,
197
            '__EVENTVALIDATION' => $this->eventValidation,
198
            'rbtnlTipoBusqueda' => $tipoBusqueda,
199
            'ctl00$smMain' => $smMain,
200
            '__ASYNCPOST' => 'true',
201
            $campo => $valorBuscar,
202
            $boton => 'BUSCAR'
203
        );
204
205
        $query = http_build_query($post);
206
        $result = $this->curlSearch($this->searchProcessor, $query);
207
208
        $doc = new HtmlDocument();
209
        $html = $doc->load($result);
210
211
        $vacio = trim($html->getElementById('#cphMain_lblInformacion', 0)->value);
212
213
        if ($vacio !== '') {
214
            $resultados = $html->getElementById('#cphMain_lblInformacion', 0)->value;
215
            return $resultados;
216
        } else {
217
            $cabeceras = array();
218
            $detalles = array();
219
            $table = $html->getElementById('#cphMain_dvDatosContribuyentes');
220
            $tbody = $table->find('tbody');
0 ignored issues
show
Unused Code introduced by
The assignment to $tbody is dead and can be removed.
Loading history...
221
            foreach ($html->getElementById('#cphMain_dvDatosContribuyentes') as $lista) {
222
                print_r($lista);
223
                //$cabeceras = $this->loopLista($lista);
224
            }
225
            $this->cabecera = $cabeceras;
0 ignored issues
show
Bug Best Practice introduced by
The property cabecera does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
226
            $lista_interna = 0;
227
            foreach ($html->find('.GridItemStyle') as $lista) {
228
                $detalles[$lista_interna] = $this->loopLista($lista);
229
                $lista_interna++;
230
            }
231
            foreach ($html->find('.bg_celdas_alt') as $lista) {
232
                $detalles[$lista_interna] = $this->loopLista($lista);
233
                $lista_interna++;
234
            }
235
            $this->detalle = $detalles;
0 ignored issues
show
Bug Best Practice introduced by
The property detalle does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
236
//            $this->total_cabecera = count($cabeceras);
237
//            $this->total_resultados = count($this->detalle);
238
            return $cabeceras;
239
        }
240
    }
241
242
    private function loopLista($lista)
243
    {
244
        $array = array();
245
        foreach ($lista->find('td') as $item) {
246
            $array[] = $item->plaintext;
247
        }
248
        return $array;
249
    }
250
}