siphons::checkTowers()   D
last analyzed

Complexity

Conditions 21
Paths 46

Size

Total Lines 92
Code Lines 64

Duplication

Lines 72
Ratio 78.26 %

Importance

Changes 0
Metric Value
cc 21
eloc 64
nc 46
nop 0
dl 72
loc 92
rs 4.6955
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
 * The MIT License (MIT)
4
 *
5
 * Copyright (c) 2016 Robert Sardinia
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in all
15
 * copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE.
24
 */
25
26
use discord\discord;
27
28
/**
29
 * Class siphons
30
 */
31
class siphons
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...
32
{
33
    public $guild;
34
    /**
35
     * @var
36
     */
37
    private $config;
38
    /**
39
     * @var
40
     */
41
    private $discord;
42
    /**
43
     * @var
44
     */
45
    private $logger;
46
    /**
47
     * @var
48
     */
49
    private $groupConfig;
50
51
    /**
52
     * @param $config
53
     * @param $discord
54
     * @param $logger
55
     */
56
    public function init($config, $discord, $logger)
57
    {
58
        $this->config = $config;
59
        $this->discord = $discord;
60
        $this->logger = $logger;
61
        $this->guild = $config['bot']['guild'];
62
        $this->groupConfig = $config['plugins']['siphons']['groupConfig'];
63
    }
64
65
    /**
66
     *
67
     */
68
    public function tick()
69
    {
70
        // If config is outdated
71
        if (null === $this->groupConfig) {
72
            $this->logger->addInfo('Siphons: Update your config file to the latest version.');
73
            $msg = '**Siphon Failure:** Please update the bots config to the latest version.';
74
            queueMessage($msg, $this->config['notifications']['channelID'], $this->guild);
75
        }
76
77
        // What was the servers last reported state
78
        $lastStatus = getPermCache('serverState');
79
        if ($lastStatus === 'online') {
80
            foreach ($this->groupConfig as $siphonCorp) {
81
                //If group channel is set to 0 skip
82
                if ($siphonCorp['channelID'] === 0) {
83
                    continue;
84
                }
85
                $lastChecked = getPermCache("siphonLastChecked{$siphonCorp['keyID']}");
86
                if ($lastChecked === NULL) {
87
                    // Schedule it for right now if first run
88
                    $lastChecked = 1;
89
                }
90
91
                if ($lastChecked <= time()) {
92
                    $this->logger->addInfo("Siphons: Checking keyID - {$siphonCorp['keyID']} for siphons");
93
                    $this->checkTowers();
94
                }
95
            }
96
        }
97
    }
98
99
    private function checkTowers()
100
    {
101
        foreach ($this->groupConfig as $siphonCorp) {
102
            //If group channel is set to 0 skip
103
            if ($siphonCorp['channelID'] === 0) {
104
                continue;
105
            }
106
            $url = "https://api.eveonline.com/corp/AssetList.xml.aspx?keyID={$siphonCorp['keyID']}&vCode={$siphonCorp['vCode']}";
107
            $xml = makeApiRequest($url);
108
            $rawGoo = array(16634, 16643, 16647, 16641, 16640, 16635, 16648, 16633, 16646, 16651, 16650, 16644, 16652, 16639, 16636, 16649, 16653, 16638, 16637, 16642);
109
            foreach (@$xml->result->rowset->row as $structures) {
110
                //Check silos
111 View Code Duplication
                if ((int) @$structures->attributes()->typeID === 14343) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
112
                    if (isset($structures->rowset->row)) {
113
                        foreach (@$structures->rowset->row as $silo) {
114
                            //Avoid reporting empty silos
115
                            if ((int) @$silo->attributes()->quantity !== 0 && in_array(@$silo->attributes()->typeID, $rawGoo, false)) {
116
                                $siloID = $structures->attributes()->itemID;
117
                                $lastAmount = getPermCache("silo{$siloID}Amount");
118
                                $gooAmount = $silo->attributes()->quantity;
119
                                $gooDifference = (int) $gooAmount - (int) $lastAmount;
120
                                //Check if silo has been checked before
121
                                if (null === $lastAmount || $gooDifference < 0) {
122
                                    setPermCache("silo{$siloID}Amount", $gooAmount);
123
                                    continue;
124
                                }
125
                                //Check for a multiple of 50 in the difference
126
                                if ($gooDifference % 50 !== 0) {
127
                                    $gooType = getTypeName($silo->attributes()->typeID);
128
                                    $systemName = getSystemName($structures->attributes()->locationID);
129
                                    $msg = "{$siphonCorp['prefix']}";
130
                                    $msg .= "**POSSIBLE SIPHON**\n";
131
                                    $msg .= "**System: **{$systemName} has a possible siphon stealing {$gooType} from a silo.\n";
132
                                    // Queue the message
133
                                    priorityQueueMessage($msg, $siphonCorp['channelID'], $this->guild);
134
                                    $this->logger->addInfo("Siphons: {$msg}");
135
                                    setPermCache("silo{$siloID}Amount", $gooAmount);
136
                                } else {
137
                                    setPermCache("silo{$siloID}Amount", $gooAmount);
138
                                }
139
                            }
140
                        }
141
                    }
142
                }
143 View Code Duplication
                if ((int) @$structures->attributes()->typeID === 17982) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
144
                    if (isset($structures->rowset->row)) {
145
                        foreach (@$structures->rowset->row as $coupling) {
146
                            //Avoid reporting empty coupling arrays
147
                            if ((int) @$coupling->attributes()->quantity !== 0 && in_array(@$coupling->attributes()->typeID, $rawGoo, false)) {
148
                                $couplingID = $structures->attributes()->itemID;
149
                                $lastAmount = getPermCache("couplingArray{$couplingID}Amount");
150
                                $gooAmount = $coupling->attributes()->quantity;
151
                                $gooDifference = (int) $gooAmount - (int) $lastAmount;
152
                                //Check if silo has been checked before
153
                                if (null === $lastAmount || $gooDifference < 0) {
154
                                    setPermCache("couplingArray{$couplingID}Amount", $gooAmount);
155
                                    continue;
156
                                }
157
                                //Check for a multiple of 50 in the difference
158
                                if ($gooDifference % 50 !== 0) {
159
                                    $gooType = getTypeName($coupling->attributes()->typeID);
160
                                    $systemName = getSystemName($structures->attributes()->locationID);
161
                                    $msg = "{$siphonCorp['prefix']}";
162
                                    $msg .= "**POSSIBLE SIPHON**\n";
163
                                    $msg .= "**System: **{$systemName} has a possible siphon stealing {$gooType} from a coupling array.\n";
164
                                    // Queue the message
165
                                    priorityQueueMessage($msg, $siphonCorp['channelID'], $this->guild);
166
                                    $this->logger->addInfo("Siphons: {$msg}");
167
                                    setPermCache("couplingArray{$couplingID}Amount", $gooAmount);
168
                                } else {
169
                                    setPermCache("couplingArray{$couplingID}Amount", $gooAmount);
170
                                }
171
                            }
172
                        }
173
                    }
174
                }
175
            }
176
            $cached = $xml->cachedUntil[0];
177
            $baseUnix = strtotime($cached);
178
            $cacheClr = $baseUnix - 13500;
179 View Code Duplication
            if ($cacheClr <= time()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
180
                $weirdTime = time() + 21700;
181
                $cacheTimer = gmdate('Y-m-d H:i:s', $weirdTime);
182
                setPermCache("siphonLastChecked{$siphonCorp['keyID']}", $weirdTime);
183
            } else {
184
                $cacheTimer = gmdate('Y-m-d H:i:s', $cacheClr);
185
                setPermCache("siphonLastChecked{$siphonCorp['keyID']}", $cacheClr);
186
            }
187
            $this->logger->addInfo("Siphons: Siphon Check Complete Next Check At {$cacheTimer}");
188
            return null;
189
        }
190
    }
191
}
192