Failed Conditions
Push — issue#765 ( 54a2c2...25bda7 )
by Guilherme
09:33
created

updateByNsg()   D

Complexity

Conditions 20
Paths 36

Size

Total Lines 152
Code Lines 111

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 20
eloc 111
nc 36
nop 1
dl 0
loc 152
rs 4.7294
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
require 'stuff.php';
3
$config = parse_ini_file($argv[1]);
4
if (!$config) {
5
    echo "Couldn't parse the config file.";
6
    exit(1);
7
}
8
9
function getCountryPostal($config)
10
{
11
    $filename = getTempNam();
12
    if (!file_get_contents_curl("http://www.geopostcodes.com/inc/download.php?f=Countries&t=9", $filename)) {
13
        return false;
14
    }
15
16
    $zip = new Zip_Manager();
17
    $zip->open($filename);
18
    $zip->filteredExtractTo('./');
19
    $zip->close();
20
    unlink($filename);
21
    $filename = "GeoPC_Countries.csv";
22
    $f = fopen($filename, 'rb');
23
    if (!$f) {
24
        return false;
25
    }
26
    $row = fgetcsv($f, null, ';');
27
    if ($row != array(
28
        'iso',
29
        'country',
30
        'sovereign',
31
        'postalformat',
32
        'postalname',
33
        'geopc'
34
    )) {
35
        return false;
36
    }
37
    $pdo = getPDOConnection($config);
38
    $pdo->beginTransaction();
39
    $st1 = $pdo->prepare('select id from country where iso2 = ?');
40
    $st2 = $pdo->prepare('update country set postal_format = ?, postal_name = ? where id = ?');
41
    $st3 = $pdo->prepare('insert into country (id, name, postal_format, postal_name, iso2) values (nextval(\'country_id_seq\'), ?,?,?,?)');
42
    while ($row = fgetcsv($f, null, ';')) {
43
        if (!$st1->execute(array(
44
            $row[0]
45
        ))) {
46
            return false;
47
        }
48
        if ($r = $st1->fetchAll()) {
49
            $st2->bindParam(1, trim2($row[3]), PDO::PARAM_STR | PDO::PARAM_NULL);
50
            $st2->bindParam(2, trim2($row[4]), PDO::PARAM_STR | PDO::PARAM_NULL);
51
            $st2->bindParam(3, $r[0]['id'], PDO::PARAM_INT);
52
            if (!$st2->execute()) {
53
                print_r($row);
54
                print_r($pdo->errorInfo());
55
                exit(1);
56
            }
57
        } else {
58
            $st3->bindParam(1, trim2($row[1]), PDO::PARAM_STR | PDO::PARAM_NULL);
59
            $st3->bindParam(2, trim2($row[3]), PDO::PARAM_STR | PDO::PARAM_NULL);
60
            $st3->bindParam(3, trim2($row[4]), PDO::PARAM_STR | PDO::PARAM_NULL);
61
            $st3->bindParam(4, trim($row[0]), PDO::PARAM_STR | PDO::PARAM_NULL);
62
            if (!$st3->execute()) {
63
                print_r($row);
64
                print_r($pdo->errorInfo());
65
                return false;
66
            }
67
        }
68
    }
69
    $pdo->commit();
70
    unlink($filename);
71
    return true;
72
}
73
74
function updateByNsg($config)
75
{
76
    $filename = getTempNam();
77
    if (!file_get_contents_curl('https://nsgreg.nga.mil/NSGDOC/files/doc/Document/GENC%20Standard%20Index%20XML%20Ed2.0.zip', $filename, 'https://nsgreg.nga.mil/doc/view?i=2382')) {
78
        return false;
79
    }
80
81
    $zip = new Zip_Manager();
82
    $zip->open($filename);
83
    $zip->filteredExtractTo('./', array(
84
        '/^GENC Standard Index Ed2.0.xml$/'
85
    ));
86
    $zip->close();
87
    unlink($filename);
88
    $filename = realpath("./GENC Standard Index Ed2.0.xml");
89
    $dom = new DOMDocument();
90
    if (!$dom->load($filename)) {
91
        return false;
92
    }
93
    $dom->documentElement->removeAttributeNS('http://api.nsgreg.nga.mil/schema/genc/2.0', 'genc');
94
    $xpath = new DOMXPath($dom);
95
    // $nodes = $xpath->query('//GENCStandardBaselineIndex/GeopoliticalEntity[encoding/char3Code][encoding/char2Code][encoding/numericCode][name]');
96
    $nodes = $xpath->query('//GENCStandardBaselineIndex/GeopoliticalEntity');
97
    if (!$nodes || !$nodes->length) {
98
        return false;
99
    }
100
    $pdo = getPDOConnection($config);
101
102
    $st1 = $pdo->prepare('select id from country where iso3 = ?');
103
    $st2 = $pdo->prepare('update country set name = ? where id = ?');
104
    $st3 = $pdo->prepare('insert into country (id, name, iso2, iso3, iso_num, reviewed) values (nextval(\'country_id_seq\'), ?,?,?,?, 0)');
105
106
    $pdo->beginTransaction();
107
    foreach ($nodes as $node) {
108
        $char3Code = $node->getElementsByTagName('char3Code')->item(0)->nodeValue;
109
        if (!$st1->execute(array(
110
            $char3Code
111
        ))) {
112
            print_r($char3Code);
113
            print_r($pdo->errorInfo());
114
            return false;
115
        }
116
        if ($r = $st1->fetchAll()) {
117
            if (!$st2->execute(array(
118
                $node->getElementsByTagName('name')
119
                    ->item(0)->nodeValue,
120
                $r[0]['id']
121
            ))) {
122
                print_r($char3Code);
123
                print_r($pdo->errorInfo());
124
                return false;
125
            }
126
        } else {
127
            if (!$st3->execute(array(
128
                $node->getElementsByTagName('name')
129
                    ->item(0)->nodeValue,
130
                $node->getElementsByTagName('char2Code')
131
                    ->item(0)->nodeValue,
132
                $char3Code,
133
                $node->getElementsByTagName('numericCode')
134
                    ->item(0)->nodeValue
135
            ))) {
136
                print_r($char3Code);
137
                print_r($pdo->errorInfo());
138
                return false;
139
            }
140
        }
141
        /*
142
         * echo $node->getElementsByTagName('char3Code')->item(0)->nodeValue; echo $node->getElementsByTagName('char2Code')->item(0)->nodeValue; echo $node->getElementsByTagName('numericCode')->item(0)->nodeValue; echo $node->getElementsByTagName('name')->item(0)->nodeValue;
143
         */
144
    }
145
146
    $st1->closeCursor();
147
    $st2->closeCursor();
148
    $st3->closeCursor();
149
150
    $st1 = $pdo->prepare('select id id from state a1 where a1.iso6 = ?');
151
    $st2 = $pdo->prepare('select a1.id id from state a1 inner join country a2 on a1.country_id = a2.id where a2.iso3 = ? and a1.name = ?');
152
    $st3 = $pdo->prepare('update state set iso6 = ?, name = ?, country_id = (select id from country where iso3 = ?) where id = ?');
153
    $st4 = $pdo->prepare('insert into state (id, country_id, iso6, name, reviewed) values (nextval(\'state_id_seq\'), (select id from country where iso3 = ?), ?, ?, 0)');
154
    $xpath = new DOMXPath($dom);
155
    // $nodes = $xpath->query('//GENCStandardBaselineIndex/AdministrativeSubdivision[country][encoding/char6Code][name]');
156
    $nodes = $xpath->query('//GENCStandardBaselineIndex/AdministrativeSubdivision');
157
    if (!$nodes || !$nodes->length) {
158
        return false;
159
    }
160
    foreach ($nodes as $node) {
161
        $char6Code = $node->getElementsByTagName('char6Code')->item(0)->nodeValue;
162
        if (!$st1->execute(array(
163
            $char6Code
164
        ))) {
165
            print_r($char6Code);
166
            print_r($pdo->errorInfo());
167
            exit(1);
168
        }
169
        if ($r = $st1->fetchAll()) {
170
            if (!$st3->execute(array(
171
                $char6Code,
172
                $node->getElementsByTagName('name')
173
                    ->item(0)->nodeValue,
174
                $node->getElementsByTagName('country')
175
                    ->item(0)->nodeValue,
176
                $r[0]['id']
177
            ))) {
178
                print_r($char6Code);
179
                print_r($pdo->errorInfo());
180
                exit(1);
181
            }
182
        } else {
183
            if (!$st2->execute(array(
184
                $node->getElementsByTagName('country')
185
                    ->item(0)->nodeValue,
186
                $node->getElementsByTagName('name')
187
                    ->item(0)->nodeValue
188
            ))) {
189
                print_r($char6Code);
190
                print_r($pdo->errorInfo());
191
                exit(1);
192
            }
193
            if ($r = $st2->fetchAll()) {
194
                if (!$st3->execute(array(
195
                    $char6Code,
196
                    $node->getElementsByTagName('name')
197
                        ->item(0)->nodeValue,
198
                    $node->getElementsByTagName('country')
199
                        ->item(0)->nodeValue,
200
                    $r[0]['id']
201
                ))) {
202
                    print_r($char6Code);
203
                    print_r($pdo->errorInfo());
204
                    exit(1);
205
                }
206
            } else {
207
                if (!$st4->execute(array(
208
                    $node->getElementsByTagName('country')
209
                        ->item(0)->nodeValue,
210
                    $char6Code,
211
                    $node->getElementsByTagName('name')
212
                        ->item(0)->nodeValue
213
                ))) {
214
                    print_r($char6Code);
215
                    print_r($pdo->errorInfo());
216
                    exit(1);
217
                }
218
            }
219
        }
220
    }
221
    /*
222
     * echo $node->getElementsByTagName('country')->item(0)->nodeValue; echo $node->getElementsByTagName('char6Code')->item(0)->nodeValue; echo $node->getElementsByTagName('name')->item(0)->nodeValue;
223
     */
224
    $pdo->commit();
225
    return true;
226
}
227
exit(updateByNsg($config) && getCountryPostal($config) ? 0 : 1);
228
229
230
231