Passed
Push — development ( 2c0f7a...866d79 )
by Thomas
01:59
created

CacheLocation::run()   D

Complexity

Conditions 11
Paths 148

Size

Total Lines 143
Code Lines 75

Duplication

Lines 23
Ratio 16.08 %

Importance

Changes 0
Metric Value
cc 11
eloc 75
nc 148
nop 0
dl 23
loc 143
rs 4.9629
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
 *  For license information see doc/license.txt
4
 *
5
 *
6
 *  Cleanup the table sys_temptables from entries of dead threads
7
 *
8
 *                         run it once a day
9
 *
10
 ***************************************************************************/
11
12
checkJob(new CacheLocation());
13
14
class CacheLocation
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
15
{
16
    public $name = 'cache_location';
17
    public $interval = 0;
18
19
    public function run()
20
    {
21
        global $opt;
22
23
        $rsCache = sql(
24
            'SELECT `caches`.`cache_id`, `caches`.`latitude`, `caches`.`longitude`
25
            FROM `caches`
26
            LEFT JOIN `cache_location`
27
                ON `caches`.`cache_id`=`cache_location`.`cache_id`
28
            WHERE ISNULL(`cache_location`.`cache_id`)
29
            UNION
30
            SELECT `caches`.`cache_id`, `caches`.`latitude`, `caches`.`longitude`
31
            FROM `caches`
32
            INNER JOIN `cache_location`
33
                ON `caches`.`cache_id`=`cache_location`.`cache_id`
34
            WHERE `caches`.`last_modified`>`cache_location`.`last_modified`'
35
        );
36
        while ($rCache = sql_fetch_assoc($rsCache)) {
37
            $sCode = '';
38
39
            $rsLayers = sql(
40
                "SELECT `level`, `code`, AsText(`shape`) AS `geometry`
41
                FROM `nuts_layer`
42
                WHERE MBRWITHIN(GeomFromText('&1'), `shape`) ORDER BY `level` DESC",
43
                'POINT(' . $rCache['longitude'] . ' ' . $rCache['latitude'] . ')'
44
            );
45
            while ($rLayers = sql_fetch_assoc($rsLayers)) {
46 View Code Duplication
                if (gis::ptInLineRing(
47
                    $rLayers['geometry'],
48
                    'POINT(' . $rCache['longitude'] . ' ' . $rCache['latitude'] . ')'
49
                )
50
                ) {
51
                    $sCode = $rLayers['code'];
52
                    break;
53
                }
54
            }
55
            sql_free_result($rsLayers);
56
57
            if ($sCode !== '') {
58
                $adm1 = null;
59
                $code1 = null;
60
                $adm2 = null;
61
                $code2 = null;
62
                $adm3 = null;
63
                $code3 = null;
64
                $adm4 = null;
65
                $code4 = null;
66
67
                if (mb_strlen($sCode) > 5) {
68
                    $sCode = mb_substr($sCode, 0, 5);
69
                }
70
71 View Code Duplication
                if (mb_strlen($sCode) === 5) {
72
                    $code4 = $sCode;
73
                    $adm4 = sql_value("SELECT `name` FROM `nuts_codes` WHERE `code`='&1'", null, $sCode);
74
                    $sCode = mb_substr($sCode, 0, 4);
75
                }
76
77 View Code Duplication
                if (mb_strlen($sCode) === 4) {
78
                    $code3 = $sCode;
79
                    $adm3 = sql_value("SELECT `name` FROM `nuts_codes` WHERE `code`='&1'", null, $sCode);
80
                    $sCode = mb_substr($sCode, 0, 3);
81
                }
82
83 View Code Duplication
                if (mb_strlen($sCode) === 3) {
84
                    $code2 = $sCode;
85
                    $adm2 = sql_value("SELECT `name` FROM `nuts_codes` WHERE `code`='&1'", null, $sCode);
86
                    $sCode = mb_substr($sCode, 0, 2);
87
                }
88
89
                if (mb_strlen($sCode) === 2) {
90
                    $code1 = $sCode;
91
92
                    // try to get localised name first
93
                    $adm1 = sql_value(
94
                        "SELECT IFNULL(`sys_trans_text`.`text`, `countries`.`name`)
95
                         FROM `countries`
96
                         LEFT JOIN `sys_trans`
97
                           ON `countries`.`trans_id`=`sys_trans`.`id`
98
                           AND `countries`.`name`=`sys_trans`.`text`
99
                         LEFT JOIN `sys_trans_text`
100
                           ON `sys_trans`.`id`=`sys_trans_text`.`trans_id`
101
                           AND `sys_trans_text`.`lang`='&2'
102
                         WHERE `countries`.`short`='&1'",
103
                        null,
104
                        $sCode,
105
                        $opt['template']['default']['locale']
106
                    );
107
108
                    if ($adm1 == null) {
109
                        $adm1 = sql_value("SELECT `name` FROM `nuts_codes` WHERE `code`='&1'", null, $sCode);
110
                    }
111
                }
112
113
                sql(
114
                    "INSERT INTO `cache_location` (`cache_id`, `adm1`, `adm2`, `adm3`, `adm4`, `code1`, `code2`, `code3`, `code4`)
115
                    VALUES ('&1', '&2', '&3', '&4', '&5', '&6', '&7', '&8', '&9')
116
                    ON DUPLICATE KEY UPDATE `adm1`='&2', `adm2`='&3', `adm3`='&4', `adm4`='&5', `code1`='&6', `code2`='&7', `code3`='&8', `code4`='&9'",
117
                    $rCache['cache_id'],
118
                    $adm1,
119
                    $adm2,
120
                    $adm3,
121
                    $adm4,
122
                    $code1,
123
                    $code2,
124
                    $code3,
125
                    $code4
126
                );
127
            } else {
128
                $sCountry = sql_value(
129
                    "SELECT IFNULL(`sys_trans_text`.`text`, `countries`.`name`)
130
                     FROM `caches`
131
                     INNER JOIN `countries`
132
                       ON `caches`.`country`=`countries`.`short`
133
                     LEFT JOIN `sys_trans`
134
                       ON `countries`.`trans_id`=`sys_trans`.`id`
135
                       AND `countries`.`name`=`sys_trans`.`text`
136
                     LEFT JOIN `sys_trans_text`
137
                       ON `sys_trans`.`id`=`sys_trans_text`.`trans_id`
138
                       AND `sys_trans_text`.`lang`='&2'
139
                     WHERE `caches`.`cache_id`='&1'",
140
                    null,
141
                    $rCache['cache_id'],
142
                    $opt['template']['default']['locale']
143
                );
144
                $sCode1 = sql_value(
145
                    "SELECT `caches`.`country` FROM `caches` WHERE `caches`.`cache_id`='&1'",
146
                    null,
147
                    $rCache['cache_id']
148
                );
149
                sql(
150
                    "INSERT INTO `cache_location` (`cache_id`, `adm1`, `code1`)
151
                    VALUES ('&1', '&2', '&3')
152
                    ON DUPLICATE KEY UPDATE `adm1`='&2', `adm2`=NULL, `adm3`=NULL, `adm4`=NULL, 
153
                                            `code1`='&3', `code2`=NULL, `code3`=NULL, `code4`=NULL",
154
                    $rCache['cache_id'],
155
                    $sCountry,
156
                    $sCode1
157
                );
158
            }
159
        }
160
        sql_free_result($rsCache);
161
    }
162
}
163