Test Setup Failed
Branch master (f413d3)
by
unknown
10:19 queued 02:58
created

getKillmails::tick()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 0
dl 0
loc 12
rs 9.4285
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->bigKill = $config["plugins"]["getKillmails"]["bigKill"];
0 ignored issues
show
Bug introduced by
The property bigKill does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
78
        if ($this->bigKill == null) {
79
            $this->bigKill = 999999999999999999;
80
        }
81
        $this->corpID = $config["plugins"]["getKillmails"]["corpID"];
82
        $this->allianceID = $config["plugins"]["getKillmails"]["allianceID"];
83
        $this->startMail = $config["plugins"]["getKillmails"]["startMail"];
84
        $this->lossMail = $config["plugins"]["getKillmails"]["lossMails"];
85
        $this->spamAmount = $config["plugins"]["getKillmails"]["spamAmount"];
86
        $this->guild = $config["bot"]["guild"];
87
        if (2 > 1) {
88
            //Check for a higher set value
89
            $currentID = getPermCache("newestKillmailID");
90
            if ($currentID < $this->startMail || $currentID == null) {
91
                setPermCache("newestKillmailID", $this->startMail);
92
            }
93
94
            // Schedule it for right now
95
            setPermCache("killmailCheck{$this->corpID}", time() - 5);
96
        }
97
    }
98
99
100
101
    /**
102
     * @return array
103
     */
104
    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...
105
    {
106
        return array(
107
            "name" => "",
108
            "trigger" => array(),
109
            "information" => ""
110
        );
111
    }
112
113
    /**
114
     *
115
     */
116
    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...
117
    {
118
        $lastChecked = getPermCache("killmailCheck{$this->corpID}");
119
        if ($lastChecked <= time()) {
120
            $this->logger->addInfo("Checking for new killmails.");
121
            $oldID = getPermCache("newestKillmailID");
122
            setPermCache("newestKillmailID", $oldID++);
123
            $this->getKM();
124
            setPermCache("killmailCheck{$this->corpID}", time() + 900);
125
        }
126
127
    }
128
129
    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...
130
    {
131
        $discord = $this->discord;
132
        $this->newestKillmailID = getPermCache("newestKillmailID");
133
        $lastMail = $this->newestKillmailID;
134 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...
135
            $url = "https://zkillboard.com/api/no-attackers/no-items/orderDirection/asc/afterKillID/{$lastMail}/corporationID/{$this->corpID}/";
136
        }
137 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...
138
            $url = "https://zkillboard.com/api/no-attackers/no-items/kills/orderDirection/asc/afterKillID/{$lastMail}/corporationID/{$this->corpID}/";
139
        }
140 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...
141
            $url = "https://zkillboard.com/api/no-attackers/no-items/orderDirection/asc/afterKillID/{$lastMail}/allianceID/{$this->allianceID}/";
142
        }
143 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...
144
            $url = "https://zkillboard.com/api/no-attackers/no-items/kills/orderDirection/asc/afterKillID/{$lastMail}/allianceID/{$this->allianceID}/";
145
        }
146
147
        if (!isset($url)) { // Make sure it's always set.
148
            $this->logger->addInfo("ERROR - Ensure your config file is setup correctly for killmails.");
149
            return null;
150
        }
151
152
        $xml = json_decode(downloadData($url), true);
153
        $i = 0;
154
        $limit = $this->spamAmount;
155
        if (isset($xml)) {
156
            foreach ($xml as $kill) {
157
                if ($i < $limit) {
158
                    $killID = $kill['killID'];
159
                    if ($this->startMail > $killID) {
160
                        $killID = $this->startMail;
161
                    }
162
                    $solarSystemID = $kill['solarSystemID'];
163
                    $systemName = apiCharacterName($solarSystemID);
164
                    $killTime = $kill['killTime'];
165
                    $victimAllianceName = $kill['victim']['allianceName'];
166
                    $victimName = $kill['victim']['characterName'];
167
                    $victimCorpName = $kill['victim']['corporationName'];
168
                    $victimShipID = $kill['victim']['shipTypeID'];
169
                    $shipName = apiTypeName($victimShipID);
170
                    $rawValue = $kill['zkb']['totalValue'];
171
                    $totalValue = number_format($kill['zkb']['totalValue']);
172
                    //$iskValue = number_format(($totalValue/100), 0);
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
173
                    // Check if it's a structure
174
                    if ($victimName != "") {
175
                        if ($rawValue >= $this->bigKill){
176
                            $msg = "@here \n :warning:***Expensive Killmail***:warning: \n **{$killTime}**\n\n**{$shipName}** worth **{$totalValue} ISK** flown by **{$victimName}** of (***{$victimCorpName}|{$victimAllianceName}***) killed in {$systemName}\nhttps://zkillboard.com/kill/{$killID}/";
177
                        }
178
                        elseif ($rawValue <= $this->bigKill)
179
                        {
180
                            $msg = "**{$killTime}**\n\n**{$shipName}** worth **{$totalValue} ISK** flown by **{$victimName}** of (***{$victimCorpName}|{$victimAllianceName}***) killed in {$systemName}\nhttps://zkillboard.com/kill/{$killID}/";
181
                        }
182
                        } elseif ($victimName == "") {
183
                        $msg = "**{$killTime}**\n\n**{$shipName}** worth **{$totalValue} ISK** owned by (***{$victimCorpName}|{$victimAllianceName}***) killed in {$systemName}\nhttps://zkillboard.com/kill/{$killID}/";
184
                    }
185
186
                    if (!isset($msg)) { // Make sure it's always set.
187
                        return null;
188
                    }
189
190
                    $channelID = $this->kmChannel;
191
                    $guild = $discord->guilds->get('id', $this->guild);
192
                    $channel = $guild->channels->get('id', $channelID);
193
                    $channel->sendMessage($msg, false);
194
                    setPermCache("newestKillmailID", $killID);
195
196
                    sleep(2);
197
                    $i++;
198
                } else {
199
                    $updatedID = getPermCache("newestKillmailID");
200
                    $this->logger->addInfo("Kill posting cap reached, newest kill id is {$updatedID}");
201
                    return null;
202
                }
203
            }
204
        }
205
        $updatedID = getPermCache("newestKillmailID");
206
        $this->logger->addInfo("All kills posted, newest kill id is {$updatedID}");
207
        return null;
208
    }
209
}
210