Test Setup Failed
Pull Request — master (#42)
by Bob
03:13
created

siphons::onMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
     * @var
34
     */
35
    var $config;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $config.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
36
    /**
37
     * @var
38
     */
39
    var $discord;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $discord.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
40
    /**
41
     * @var
42
     */
43
    var $logger;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $logger.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
44
    /**
45
     * @var
46
     */
47
    var $toDiscordChannel;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $toDiscordChannel.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
48
    public $guild;
49
    protected $keyID;
50
    protected $vCode;
51
    protected $prefix;
52
53
    /**
54
     * @param $config
55
     * @param $discord
56
     * @param $logger
57
     */
58
    function init($config, $discord, $logger)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
59
    {
60
        $this->config = $config;
61
        $this->discord = $discord;
62
        $this->logger = $logger;
63
        $this->guild = $config["bot"]["guild"];
64
        $this->toDiscordChannel = $config["plugins"]["siphons"]["channelID"];
65
        $this->keyID = $config["plugins"]["siphons"]["keyID"];
66
        $this->vCode = $config["plugins"]["siphons"]["vCode"];
67
        $this->prefix = $config["plugins"]["siphons"]["prefix"];
68
        $lastCheck = getPermCache("siphonLastChecked{$this->keyID}");
69
        if ($lastCheck == NULL) {
70
            // Schedule it for right now if first run
71
            setPermCache("siphonLastChecked{$this->keyID}", time() - 5);
72
        }
73
    }
74
75
    /**
76
     *
77
     */
78
    function tick()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
79
    {
80
        $lastChecked = getPermCache("siphonLastChecked{$this->keyID}");
81
        $keyID = $this->keyID;
82
        $vCode = $this->vCode;
83
84
        if ($lastChecked <= time()) {
85
            $this->logger->addInfo("Checking API Key {$keyID} for siphons");
86
            $this->checkTowers($keyID, $vCode);
87
        }
88
    }
89
90
    function checkTowers($keyID, $vCode)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
91
    {
92
        $discord = $this->discord;
93
94
        $url = "https://api.eveonline.com/corp/AssetList.xml.aspx?keyID={$keyID}&vCode={$vCode}";
95
        $xml = makeApiRequest($url);
96
        $siphonCount = 0;
97
        foreach ($xml->result->rowset->row as $structures) {
98
            //Check silos
99 View Code Duplication
            if ($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...
100
                if (isset($structures->rowset->row)) {
101
                    foreach ($structures->rowset->row as $silo) {
102
                        //Avoid reporting empty silos
103
                        if ($silo->attributes()->quantity != 0) {
104
                            //Check for a multiple of 50
105
                            if ($silo->attributes()->quantity % 50 != 0) {
106
                                $gooType = apiTypeName($silo->attributes()->typeID);
107
                                $systemName = apiCharacterName($structures->attributes()->locationID);
108
                                $msg = "{$this->prefix}";
109
                                $msg .= "**POSSIBLE SIPHON**\n";
110
                                $msg .= "**System: **{$systemName} has a possible siphon stealing {$gooType} from a silo.\n";
111
                                // Send the msg to the channel;
112
                                $channelID = $this->toDiscordChannel;
113
                                $guild = $discord->guilds->get('id', $this->guild);
114
                                $channel = $guild->channels->get('id', $channelID);
115
                                $channel->sendMessage($msg, false);
116
                                $this->logger->addInfo($msg);
117
                                $siphonCount++;
118
                                sleep(2); // Lets sleep for a second, so we don't rage spam
119
                            }
120
                        }
121
                    }
122
                }
123
            }
124 View Code Duplication
            if ($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...
125
                if (isset($structures->rowset->row)) {
126
                    foreach ($structures->rowset->row as $coupling) {
127
                        //Avoid reporting empty coupling arrays
128
                        if ($coupling->attributes()->quantity != 0) {
129
                            //Check for a multiple of 50
130
                            if ($coupling->attributes()->quantity % 50 != 0) {
131
                                $gooType = apiTypeName($silo->attributes()->typeID);
1 ignored issue
show
Bug introduced by
The variable $silo seems to be defined by a foreach iteration on line 101. Are you sure the iterator is never empty, otherwise this variable is not defined?

It seems like you are relying on a variable being defined by an iteration:

foreach ($a as $b) {
}

// $b is defined here only if $a has elements, for example if $a is array()
// then $b would not be defined here. To avoid that, we recommend to set a
// default value for $b.


// Better
$b = 0; // or whatever default makes sense in your context
foreach ($a as $b) {
}

// $b is now guaranteed to be defined here.
Loading history...
132
                                $systemName = apiCharacterName($structures->attributes()->locationID);
133
                                $msg = "{$this->prefix}";
134
                                $msg .= "**POSSIBLE SIPHON**\n";
135
                                $msg .= "**System: **{$systemName} has a possible siphon stealing {$gooType} from a coupling array.\n";
136
                                // Send the msg to the channel;
137
                                $channelID = $this->toDiscordChannel;
138
                                $guild = $discord->guilds->get('id', $this->guild);
139
                                $channel = $guild->channels->get('id', $channelID);
140
                                $channel->sendMessage($msg, false);
141
                                $this->logger->addInfo($msg);
142
                                $siphonCount++;
143
                                sleep(2); // Lets sleep for a second, so we don't rage spam
144
                            }
145
                        }
146
                    }
147
                }
148
            }
149
        }
150
        $cached = $xml->cachedUntil[0];
151
        $baseUnix = strtotime($cached);
152
        $cacheClr = $baseUnix - 13500;
153 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...
154
            $weirdTime = time() + 21700;
155
            $cacheTimer = gmdate("Y-m-d H:i:s", $weirdTime);
156
            setPermCache("siphonLastChecked{$keyID}", $weirdTime);
157
        } else {
158
            $cacheTimer = gmdate("Y-m-d H:i:s", $cacheClr);
159
            setPermCache("siphonLastChecked{$keyID}", $cacheClr);
160
        }
161 View Code Duplication
        if ($siphonCount > 0) {
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...
162
            $msg = "Next Siphon Check At: {$cacheTimer} EVE Time";
163
            $channelID = $this->toDiscordChannel;
164
            $guild = $discord->guilds->get('id', $this->guild);
165
            $channel = $guild->channels->get('id', $channelID);
166
            $channel->sendMessage($msg, false);
167
        }
168
        $this->logger->addInfo("Siphon Check Complete Next Check At {$cacheTimer}");
169
        return null;
170
    }
171
172
    /**
173
     *
174
     */
175
    function onMessage()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
176
    {
177
    }
178
179
    /**
180
     * @return array
181
     */
182
    function information()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
183
    {
184
        return array(
185
            "name" => "",
186
            "trigger" => array(""),
187
            "information" => ""
188
        );
189
    }
190
}
191