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

RNCDGIIDB::slowParser()   B

Complexity

Conditions 8
Paths 11

Size

Total Lines 33
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 24
nc 11
nop 0
dl 0
loc 33
rs 8.4444
c 1
b 0
f 0
1
<?php
2
/**
3
 * Copyright (C) 2022 Joe Nilson <joenilson at gmail dot com>
4
 * 
5
 * fsRepublicaDominicana is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 * 
10
 * fsRepublicaDominicana 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
 * 
15
 * You should have received a copy of the GNU Lesser General Public License
16
 * along with fsRepublicaDominicana. If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace FacturaScripts\Plugins\fsRepublicaDominicana\Model;
20
21
use Cassandra\Date;
22
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...
23
use FacturaScripts\Core\Model\Base;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Core\Model\Base 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
use FacturaScripts\Core\Base\DownloadTools;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Core\Base\DownloadTools 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
/**
27
 * Description of NCFDGIIDB
28
 * This table will be filled with the DGII RNC file download from the DGII web page
29
 * @author "Joe Nilson <joenilson at gmail dot com>"
30
 */
31
class RNCDGIIDB extends Base\ModelClass
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Core\Model\Base\ModelClass 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...
32
{
33
    use Base\ModelTrait;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Core\Model\Base\ModelTrait 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...
34
35
    /**
36
     * @var string
37
     */
38
    private $dgiiUrl = 'https://dgii.gov.do/app/WebApps/Consultas/RNC/DGII_RNC.zip';
39
40
    /**
41
     * @var string
42
     */
43
    private $fileName = 'MyFiles/Cache/DGII_RNC.zip';
44
45
    /**
46
     * @var string
47
     */
48
    private $fileNameTxt = 'MyFiles/Cache/TMP/DGII_RNC.TXT';
49
50
    /**
51
     * @var string
52
     */
53
    public $rnc;
54
55
    /**
56
     * @var string
57
     */
58
    public $nombre;
59
60
    /**
61
     * @var string
62
     */
63
    public $razonsocial;
64
65
    /**
66
     * @var string
67
     */
68
    public $categoria;
69
70
    /**
71
     * @var date
72
     */
73
    public $inicioactividad;
74
75
    /**
76
     * @var string
77
     */
78
    public $estado;
79
80
    /**
81
     * @var string
82
     */
83
    public $regimenpagos;
84
85
    /**
86
     * @return string
87
     */
88
    public static function primaryColumn()
89
    {
90
        return 'rnc';
91
    }
92
    
93
    /**
94
     * @return string
95
     */
96
    public static function tableName()
97
    {
98
        return 'rd_rncdgiidb';
99
    }
100
    
101
    /**
102
     * @return string
103
     */
104
    public function install()
105
    {
106
        parent::install();
107
        $sql = "";
108
        return($sql);
109
    }
110
111
    public function updateFile()
112
    {
113
        $this->downloadFile();
114
    }
115
116
    private function downloadFile()
117
    {
118
        if (file_exists($this->fileName)) {
119
            unlink($this->fileName);
120
        }
121
        if (file_exists($this->fileNameTxt)) {
122
            unlink($this->fileNameTxt);
123
        }
124
125
        DownloadTools::download($this->dgiiUrl, $this->fileName, '12600');
126
        $zip = new \ZipArchive();
127
        $zip->open($this->fileName);
128
        $zip->extractTo('MyFiles/Cache/');
129
        $zip->close();
130
        $this->slowParser();
131
    }
132
133
    private function slowParser()
134
    {
135
        $dataBase = new DataBase();
136
        $sqlDelete = "DELETE FROM " . $this->tableName() . ";";
137
        $dataBase->exec($sqlDelete);
138
139
        [$handle, $totalLines] = $this->utf8FopenRead($this->fileNameTxt);
140
        $lineNumber = 0;
141
        $maxLine = 2000;
142
        while (($raw_string = fgets($handle)) !== false) {
143
            $totalLines--;
144
            if ($maxLine === 2000) {
145
                $query = 'INSERT INTO ' . $this->tableName() .
146
                    ' (rnc, nombre, razonsocial, categoria, inicioactividad, estado, regimenpagos) VALUES ';
147
            }
148
            $linea = str_getcsv($raw_string, "|");
149
            if (count($linea) === 11) {
150
                if ($maxLine !== 1 && $totalLines !== 1) {
151
                    $query .= $this->queryConstruct($linea, false);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $query does not seem to be defined for all execution paths leading up to this point.
Loading history...
152
                }
153
154
                if ($maxLine === 1 || $totalLines === 1) {
155
                    $query .= $this->queryConstruct($linea, true);
156
                    $dataBase->exec($query);
157
                    $maxLine = 2001;
158
                }
159
                $maxLine--;
160
                $lineNumber++;
161
            } else {
162
                $this->toolBox()->i18nLog()->error("Error RNC TXT Linea: ".count($linea) . " - " . $raw_string);
163
            }
164
        }
165
        fclose($handle);
166
    }
167
168
    private function queryConstruct($colArray, $last = false)
169
    {
170
        $semiColon = ($last) ? "" : ",";
171
        $fecha = ($colArray[8] === '00/00/0000')
172
            ? "2000-01-01"
173
            : \date('Y-m-d', strtotime(str_replace("/", "-", $colArray[8])));
174
        return "('" .
175
            $colArray[0] . "','" .
176
            str_replace('"', '', str_replace("'", "''", str_replace("  ", " ", $colArray[1]))) . "','" .
177
            str_replace('"', '', str_replace("'", "''", str_replace("  ", " ", $colArray[2]))) . "','" .
178
            $colArray[3] . "','" .
179
            $fecha . "','" .
180
            $colArray[9] . "','" .
181
            $colArray[10] . "')" . $semiColon;
182
    }
183
184
    private function utf8FopenRead($fileName)
185
    {
186
        $fc = iconv('ISO-8859-15', 'UTF-8', file_get_contents($fileName));
187
        $handle = fopen("php://memory", "rw");
188
        $handle2 = fopen($fileName, "r");
189
        $lineCount = 0;
190
        while (!feof($handle2)) {
191
            $line = fgets($handle2);
0 ignored issues
show
Unused Code introduced by
The assignment to $line is dead and can be removed.
Loading history...
192
            $lineCount++;
193
        }
194
        fclose($handle2);
195
        fwrite($handle, $fc);
196
        fseek($handle, 0);
197
        return [$handle, $lineCount];
198
    }
199
}
200