WebserviceDgii::curlSearch()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 16
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 21
rs 9.7333
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\Core\Tools;
23
24
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...
25
26
class WebserviceDgii
27
{
28
    /**
29
     * @var string
30
     */
31
    public $wsdlDGII = 'https://www.dgii.gov.do/wsMovilDGII/WSMovilDGII.asmx?WSDL';
32
33
    public $searchInitiator = 'https://dgii.gov.do/herramientas/consultas/Paginas/RNC.aspx';
34
    public $searchProcessor = 'https://dgii.gov.do/app/WebApps/ConsultasWeb2/ConsultasWeb/consultas/rnc.aspx';
35
36
    private $externalAPI = 'https://apiv1.artesanik.com/contribuyente';
37
38
//    private $viewState;
39
//    private $viewStateGenerator;
40
//    private $eventValidation;
41
42
    public function getExternalAPI($queryParam)
43
    {
44
        //$content = file_get_contents($this->externalAPI."?query='$queryParam'");
45
        $ch = curl_init();
46
        $headers = array(
47
            'Accept: application/json',
48
            'Content-Type: application/json',
49
50
        );
51
        curl_setopt($ch, CURLOPT_USERAGENT, 'curl/FacturaScripts/2022.4');
52
        curl_setopt($ch, CURLOPT_URL, $this->externalAPI."?query=$queryParam");
53
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
54
        curl_setopt($ch, CURLOPT_HEADER, 0);
55
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
56
        $response = curl_exec($ch);
57
        $result = \json_decode($response, false, 512, JSON_THROW_ON_ERROR);
0 ignored issues
show
Bug introduced by
It seems like $response can also be of type true; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
        $result = \json_decode(/** @scrutinizer ignore-type */ $response, false, 512, JSON_THROW_ON_ERROR);
Loading history...
58
        curl_close($ch);
59
        return $result;
60
    }
61
62
    /**
63
     * @return void
64
     * @throws \SoapFault
65
     */
66
    public function wdslConnection(): object
67
    {
68
        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...
69
    }
70
71
    /**
72
     * @param object $wsdlConn
73
     * @param integer $patronBusqueda
74
     * @param string $paramValue
75
     * @param integer $inicioFilas
76
     * @param integer $filaFilas
77
     * @return void
78
     * @throws \JsonException
79
     */
80
    public function wdslSearch(
81
        int $patronBusqueda = 0,
82
        string $paramValue = '',
83
        int $inicioFilas = 1,
84
        int $filaFilas = 1
85
    ): string {
86
        $opciones = [
87
            'patronBusqueda' => $patronBusqueda,
88
            'value' => $paramValue,
89
            'inicioFilas' => $inicioFilas,
90
            'filaFilas' => $filaFilas,
91
            'IMEI' => 0
92
        ];
93
94
        $wsdlConn = $this->wdslConnection();
95
96
        $result = $wsdlConn->__soapCall('GetContribuyentes', ['GetContribuyentes' => $opciones]);
97
        $list = array();
0 ignored issues
show
Unused Code introduced by
The assignment to $list is dead and can be removed.
Loading history...
98
        $getResult = explode("@@@", $result->GetContribuyentesResult);
99
100
        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...
101
    }
102
103
    /**
104
     * @param object $wsdlConn
105
     * @param string $paramValue
106
     * @return object
107
     */
108
    public function wdslSearchCount(object $wsdlConn, string $paramValue = ''): object
109
    {
110
        $opciones = [
111
            'value' => $paramValue,
112
            'IMEI' => 0
113
        ];
114
115
        $result = $wsdlConn->__soapCall('GetContribuyentesCount', ['GetContribuyentesCount' => $opciones]);
116
        return $result->GetContribuyentesCountResult;
117
    }
118
119
    /**
120
     * @param object $item
121
     * @return void
122
     */
123
    public function buscarCliente(&$item): void
124
    {
125
        $item->existe = false;
126
        $item->codcliente = '';
127
        $cli = new Cliente();
128
        $where = [
129
            new DataBaseWhere('cifnif', $item->RGE_RUC)
130
        ];
131
        $cli->all($where);
132
        if ($cli[0] !== null) {
133
            $cliente = $cli[0];
134
            $item->existe = true;
135
            $item->codcliente = $cliente->codcliente;
136
        }
137
    }
138
139
    private function curlSearch($page, $postData = '')
0 ignored issues
show
Unused Code introduced by
The method curlSearch() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
140
    {
141
        $result = "";
0 ignored issues
show
Unused Code introduced by
The assignment to $result is dead and can be removed.
Loading history...
142
        $h = curl_init();
143
        curl_setopt($h, CURLOPT_URL, $page);
144
        curl_setopt(
145
            $h,
146
            CURLOPT_USERAGENT,
147
            'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0'
148
        );
149
        curl_setopt($h, CURLOPT_REFERER, $this->searchInitiator);
150
        if ($postData !== '') {
151
            curl_setopt($h, CURLOPT_POST, true);
152
            curl_setopt($h, CURLOPT_POSTFIELDS, $postData);
153
        }
154
155
        curl_setopt($h, CURLOPT_HEADER, false);
156
        curl_setopt($h, CURLOPT_RETURNTRANSFER, 1);
157
        $result = curl_exec($h);
158
        curl_close($h);
159
        return $result;
160
    }
161
162
    //Pedimos que nos den el VIEWSTATE y el EVENTVALIDATION a la página de busqueda
163
//    public function autorizacionWeb(): void
164
//    {
165
//        $result = $this->curlSearch($this->searchInitiator);
166
//        $html = new HtmlDocument();
167
//        $html->load($result);
168
//
169
//        $this->viewState = $html->getElementById('#__VIEWSTATE', 0)->value;
170
//
171
//        $this->viewStateGenerator = $html->getElementById('#__VIEWSTATEGENERATOR', 0)->value;
172
//
173
//        $this->eventValidation = $html->getElementById('#__EVENTVALIDATION', 0)->value;
174
//    }
175
176
    //Si la busqueda no es por RNC y en su lugar es por nombre actualizamos viewstate y eventvalidation
177
//    public function actualizarAutorizacion($tipoBusqueda)
178
//    {
179
//        $post = array(
180
//            '__EVENTTARGET' => 'rbtnlTipoBusqueda$1',
181
//            '__EVENTARGUMENT' => "",
182
//            '__LASTFOCUS' => "",
183
//            '__VIEWSTATE' => $this->viewState,
184
//            '__VIEWSTATEGENERATOR' => $this->viewStateGenerator,
185
//            '__EVENTVALIDATION' => $this->eventValidation,
186
//            'rbtnlTipoBusqueda' => $tipoBusqueda,
187
//            'txtRncCed' => ''
188
//        );
189
//
190
//        $query = http_build_query($post);
191
//        $result = $this->curlSearch($this->searchProcessor, $query);
192
//
193
//        $doc = new HtmlDocument();
194
//        $html = $doc->load($result);
195
//        $this->viewState = $html->getElementById('#__VIEWSTATE', 0)->value;
196
//        $this->eventValidation = $html->getElementById('#__EVENTVALIDATION', 0)->value;
197
//    }
198
199
//    public function buscar($rnc = '', $nombre = '')
200
//    {
201
//        $resultados = '';
202
//        $this->autorizacionWeb();
203
//        $tipoBusqueda = (!empty($rnc)) ? 0 : 1;
204
//        $valorBuscar = (!empty($rnc)) ? $rnc : strtoupper(trim($nombre));
205
//        $this->rnc = $rnc;
206
//        $this->nombre = $nombre;
207
//        $campo = (!empty($rnc)) ? 'ctl00$cphMain$txtRNCCedula' : 'ctl00$cphMain$txtRazonSocial';
208
//        $smMain = (!empty($rnc)) ? 'ctl00$cphMain$upBusqueda|ctl00$cphMain$btnBuscarPorRNC': '';
209
//        $boton = (!empty($rnc)) ? 'ctl00$cphMain$btnBuscarPorRNC' : 'ctl00$cphMain$btnBuscarPorRazonSocial';
210
//
211
//        if ($tipoBusqueda === 1) {
212
//            $this->actualizarAutorizacion($tipoBusqueda);
213
//        }
214
//
215
//        $post = array(
216
//            '__EVENTTARGET' => "",
217
//            '__EVENTARGUMENT' => "",
218
//            '__LASTFOCUS' => "",
219
//            '__VIEWSTATE' => $this->viewState,
220
//            '__VIEWSTATEGENERATOR' => $this->viewStateGenerator,
221
//            '__EVENTVALIDATION' => $this->eventValidation,
222
//            'rbtnlTipoBusqueda' => $tipoBusqueda,
223
//            'ctl00$smMain' => $smMain,
224
//            '__ASYNCPOST' => 'true',
225
//            $campo => $valorBuscar,
226
//            $boton => 'BUSCAR'
227
//        );
228
//
229
//        $query = http_build_query($post);
230
//        $result = $this->curlSearch($this->searchProcessor, $query);
231
//
232
//        $doc = new HtmlDocument();
233
//        $html = $doc->load($result);
234
//
235
//        $vacio = trim($html->getElementById('#cphMain_lblInformacion', 0)->value);
236
//
237
//        if ($vacio !== '') {
238
//            $resultados = $html->getElementById('#cphMain_lblInformacion', 0)->value;
239
//            return $resultados;
240
//        } else {
241
//            $cabeceras = array();
242
//            $detalles = array();
243
//            $table = $html->getElementById('#cphMain_dvDatosContribuyentes');
244
//            $tbody = $table->find('tbody');
245
//            foreach ($html->getElementById('#cphMain_dvDatosContribuyentes') as $lista) {
246
//                //$cabeceras = $this->loopLista($lista);
247
//            }
248
//            $this->cabecera = $cabeceras;
249
//            $lista_interna = 0;
250
//            foreach ($html->find('.GridItemStyle') as $lista) {
251
//                $detalles[$lista_interna] = $this->loopLista($lista);
252
//                $lista_interna++;
253
//            }
254
//            foreach ($html->find('.bg_celdas_alt') as $lista) {
255
//                $detalles[$lista_interna] = $this->loopLista($lista);
256
//                $lista_interna++;
257
//            }
258
//            $this->detalle = $detalles;
259
////            $this->total_cabecera = count($cabeceras);
260
////            $this->total_resultados = count($this->detalle);
261
//            return $cabeceras;
262
//        }
263
//    }
264
265
//    private function loopLista($lista)
266
//    {
267
//        $array = array();
268
//        foreach ($lista->find('td') as $item) {
269
//            $array[] = $item->plaintext;
270
//        }
271
//        return $array;
272
//    }
273
}