Passed
Branch master (7df567)
by
unknown
05:57 queued 03:05
created

getKillmails::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
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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 getKillmails
30
 */
31
class getKillmails
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
    /**
34
     * @var
35
     */
36
    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...
37
    /**
38
     * @var
39
     */
40
    var $db;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $db.

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...
41
    /**
42
     * @var
43
     */
44
    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...
45
    /**
46
     * @var
47
     */
48
    var $channelConfig;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $channelConfig.

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...
49
    /**
50
     * @var int
51
     */
52
    var $lastCheck = 0;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $lastCheck.

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...
53
    /**
54
     * @var
55
     */
56
    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...
57
    public $newestKillmailID;
58
    public $kmChannel;
59
    public $corpID;
60
    public $startMail;
61
    public $allianceID;
62
    public $lossMail;
63
    public $spamAmount;
64
    public $guild;
65
66
    /**
67
     * @param $config
68
     * @param $discord
69
     * @param $logger
70
     */
71
    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...
72
    {
73
        $this->config = $config;
74
        $this->discord = $discord;
75
        $this->logger = $logger;
76
        $this->kmChannel = $config["plugins"]["getKillmails"]["channel"];
77
        $this->corpID = $config["plugins"]["getKillmails"]["corpID"];
78
        $this->allianceID = $config["plugins"]["getKillmails"]["allianceID"];
79
        $this->startMail = $config["plugins"]["getKillmails"]["startMail"];
80
        $this->lossMail = $config["plugins"]["getKillmails"]["lossMails"];
81
        $this->spamAmount = $config["plugins"]["getKillmails"]["spamAmount"];
82
        $this->guild = $config["bot"]["guild"];
83
        if (2 > 1) {
84
            //Check for a higher set value
85
            $currentID = getPermCache("newestKillmailID");
86
            if ($currentID < $this->startMail || $currentID == null) {
87
                setPermCache("newestKillmailID", $this->startMail);
88
            }
89
90
            // Schedule it for right now
91
            setPermCache("killmailCheck{$this->corpID}", time() - 5);
92
        }
93
    }
94
95
96
97
    /**
98
     * @return array
99
     */
100
    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...
101
    {
102
        return array(
103
            "name" => "",
104
            "trigger" => array(),
105
            "information" => ""
106
        );
107
    }
108
109
    /**
110
     *
111
     */
112
    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...
113
    {
114
        $lastChecked = getPermCache("killmailCheck{$this->corpID}");
115
        if ($lastChecked <= time()) {
116
            $this->logger->addInfo("Checking for new killmails.");
117
            $oldID = getPermCache("newestKillmailID");
118
            setPermCache("newestKillmailID", $oldID++);
119
            $this->getKM();
120
            setPermCache("killmailCheck{$this->corpID}", time() + 900);
121
        }
122
123
    }
124
125
    function getKM()
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...
126
    {
127
        $discord = $this->discord;
128
        $this->newestKillmailID = getPermCache("newestKillmailID");
129
        $lastMail = $this->newestKillmailID;
130 View Code Duplication
        if ($this->allianceID == "0" & $this->lossMail == 'true') {
0 ignored issues
show
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: ($this->allianceID == '0...his->lossMail == 'true', Probably Intended Meaning: $this->allianceID == ('0...is->lossMail == 'true')

When comparing the result of a bit operation, we suggest to add explicit parenthesis and not to rely on PHP’s built-in operator precedence to ensure the code behaves as intended and to make it more readable.

Let’s take a look at these examples:

// Returns always int(0).
return 0 === $foo & 4;
return (0 === $foo) & 4;

// More likely intended return: true/false
return 0 === ($foo & 4);
Loading history...
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...
131
            $url = "https://zkillboard.com/api/xml/no-attackers/no-items/orderDirection/asc/afterKillID/{$lastMail}/corporationID/{$this->corpID}/";
132
        }
133 View Code Duplication
        if ($this->allianceID == "0" & $this->lossMail == 'false') {
0 ignored issues
show
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: ($this->allianceID == '0...is->lossMail == 'false', Probably Intended Meaning: $this->allianceID == ('0...s->lossMail == 'false')

When comparing the result of a bit operation, we suggest to add explicit parenthesis and not to rely on PHP’s built-in operator precedence to ensure the code behaves as intended and to make it more readable.

Let’s take a look at these examples:

// Returns always int(0).
return 0 === $foo & 4;
return (0 === $foo) & 4;

// More likely intended return: true/false
return 0 === ($foo & 4);
Loading history...
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...
134
            $url = "https://zkillboard.com/api/xml/no-attackers/no-items/kills/orderDirection/asc/afterKillID/{$lastMail}/corporationID/{$this->corpID}/";
135
        }
136 View Code Duplication
        if ($this->allianceID != "0" & $this->lossMail == 'true') {
0 ignored issues
show
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: ($this->allianceID != '0...his->lossMail == 'true', Probably Intended Meaning: $this->allianceID != ('0...is->lossMail == 'true')

When comparing the result of a bit operation, we suggest to add explicit parenthesis and not to rely on PHP’s built-in operator precedence to ensure the code behaves as intended and to make it more readable.

Let’s take a look at these examples:

// Returns always int(0).
return 0 === $foo & 4;
return (0 === $foo) & 4;

// More likely intended return: true/false
return 0 === ($foo & 4);
Loading history...
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...
137
            $url = "https://zkillboard.com/api/xml/no-attackers/no-items/orderDirection/asc/afterKillID/{$lastMail}/allianceID/{$this->allianceID}/";
138
        }
139 View Code Duplication
        if ($this->allianceID != "0" & $this->lossMail == 'false') {
0 ignored issues
show
Comprehensibility introduced by
Consider adding parentheses for clarity. Current Interpretation: ($this->allianceID != '0...is->lossMail == 'false', Probably Intended Meaning: $this->allianceID != ('0...s->lossMail == 'false')

When comparing the result of a bit operation, we suggest to add explicit parenthesis and not to rely on PHP’s built-in operator precedence to ensure the code behaves as intended and to make it more readable.

Let’s take a look at these examples:

// Returns always int(0).
return 0 === $foo & 4;
return (0 === $foo) & 4;

// More likely intended return: true/false
return 0 === ($foo & 4);
Loading history...
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...
140
            $url = "https://zkillboard.com/api/xml/no-attackers/no-items/kills/orderDirection/asc/afterKillID/{$lastMail}/allianceID/{$this->allianceID}/";
141
        }
142
143
        if (!isset($url)) { // Make sure it's always set.
144
            $this->logger->addInfo("ERROR - Ensure your config file is setup correctly for killmails.");
145
            return null;
146
        }
147
148
        $xml = simplexml_load_string(downloadData($url), "SimpleXMLElement", LIBXML_NOCDATA);
149
        $i = 0;
150
        $limit = $this->spamAmount;
151
        if (isset($xml->result->rowset->row)) {
152
            foreach ($xml->result->rowset->row as $kill) {
153
                if ($i < $limit) {
154
                    $killID = $kill->attributes()->killID;
155
                    if ($this->startMail > $killID) {
156
                        $killID = $this->startMail;
157
                    }
158
                    $solarSystemID = $kill->attributes()->solarSystemID;
159
                    $systemName = apiCharacterName($solarSystemID);
160
                    $killTime = $kill->attributes()->killTime;
161
                    $victimAllianceName = $kill->victim->attributes()->allianceName;
162
                    $victimName = $kill->victim->attributes()->characterName;
163
                    $victimCorpName = $kill->victim->attributes()->corporationName;
164
                    $victimShipID = $kill->victim->attributes()->shipTypeID;
165
                    $shipName = apiTypeName($victimShipID);
166
                    // Check if it's a structure
167
                    if ($victimName != "") {
168
                        $msg = "**{$killTime}**\n\n**{$shipName}** flown by **{$victimName}** of (***{$victimCorpName}|{$victimAllianceName}***) killed in {$systemName}\nhttps://zkillboard.com/kill/{$killID}/";
169
                    } elseif ($victimName == "") {
170
                        $msg = "**{$killTime}**\n\n**{$shipName}** of (***{$victimCorpName}|{$victimAllianceName}***) killed in {$systemName}\nhttps://zkillboard.com/kill/{$killID}/";
171
                    }
172
173
                    if (!isset($msg)) { // Make sure it's always set.
174
                        return null;
175
                    }
176
177
                    $channelID = $this->kmChannel;
178
                    $guild = $discord->guilds->get('id', $this->guild);
179
                    $channel = $guild->channels->get('id', $channelID);
180
                    $channel->sendMessage($msg, false);
181
                    setPermCache("newestKillmailID", $killID);
182
183
                    sleep(2);
184
                    $i++;
185
                } else {
186
                    $updatedID = getPermCache("newestKillmailID");
187
                    $this->logger->addInfo("Kill posting cap reached, newest kill id is {$updatedID}");
188
                    return null;
189
                }
190
            }
191
        }
192
        $updatedID = getPermCache("newestKillmailID");
193
        $this->logger->addInfo("All kills posted, newest kill id is {$updatedID}");
194
        return null;
195
    }
196
}
197