Passed
Branch master (6ef29a)
by
unknown
03:16
created

fleetUpOperations::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 0
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 fleetUpOperations
30
 * @property  userID
31
 * @property  apiKey
32
 * @property  groupID
33
 */
34
class fleetUpOperations {
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...
35
    /**
36
     * @var
37
     */
38
    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...
39
    /**
40
     * @var
41
     */
42
    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...
43
    /**
44
     * @var
45
     */
46
    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...
47
    /**
48
     * @var
49
     */
50
    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...
51
    public $userID;
52
    public $groupID;
53
    public $apiKey;
54
    public $guild;
55
    protected $keyID;
56
    protected $vCode;
57
    protected $prefix;
58
59
    /**
60
     * @param $config
61
     * @param $discord
62
     * @param $logger
63
     */
64 View Code Duplication
    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...
Duplication introduced by
This method seems to be duplicated in 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...
65
    {
66
        $this->config = $config;
67
        $this->discord = $discord;
68
        $this->logger = $logger;
69
        $this->toDiscordChannel = $config["plugins"]["fleetUp"]["channelID"];
70
        $this->userID = $config["plugins"]["fleetUp"]["userID"];
71
        $this->groupID = $config["plugins"]["fleetUp"]["groupID"];
72
        $this->apiKey = $config["plugins"]["fleetUp"]["apiKey"];
73
        $this->guild = $config["bot"]["guild"];
74
        $lastCheck = getPermCache("fleetUpPostLastChecked");
75
        if ($lastCheck == NULL) {
76
            // Schedule it for right now if first run
77
            setPermCache("fleetUpPostLastChecked", time() - 5);
78
        }
79
    }
80
81
    /**
82
     *
83
     */
84
    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...
85
    {
86
        $lastChecked = getPermCache("fleetUpPostLastChecked");
87
88
        if ($lastChecked <= time()) {
89
            $this->postFleetUp();
90
            $this->checkFleetUp();
91
        }
92
    }
93
94
    function postFleetUp()
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...
95
    {
96
        $discord = $this->discord;
97
98
        date_default_timezone_set("UTC");
99
        $eveTime = time();
100
        //fleetUp post upcoming operations
101
        $currentID = getPermCache("fleetUpLastPostedOperation");
102
        $fleetUpOperations = json_decode(downloadData("http://api.fleet-up.com/Api.svc/tlYgBRjmuXj2Yl1lEOyMhlDId/{$this->userID}/{$this->apiKey}/Operations/{$this->groupID}"), true);
103
        foreach ($fleetUpOperations["Data"] as $operation) {
104
            $name = $operation["Subject"];
105
            $startTime = $operation["StartString"];
106
            preg_match_all('!\d+!', $operation["Start"], $epochStart);
107
            $startTimeUnix = substr($epochStart[0][0], 0, -3);
108
            $desto = $operation["Location"];
109
            $formUp = $operation["LocationInfo"];
110
            $info = $operation["Details"];
111
            $id = $operation["Id"];
112
            $link = "https://fleet-up.com/Operation#{$id}";
113
            $timeDifference = $startTimeUnix - $eveTime;
114
            if ($currentID != $id) {
115
                if ($timeDifference < 555) {
116
                    $msg = "@everyone
117
**Upcoming Operation** 
118
Title - {$name} 
119
Form Up Time - {$startTime} 
120
Form Up System - {$formUp} 
121
Target System - {$desto} 
122
Details - {$info} 
123
124
Link - {$link}";
125
                    $channelID = $this->toDiscordChannel;
126
                    $guild = $discord->guilds->get('id', $this->guild);
127
                    $channel = $guild->channels->get('id', $channelID);
128
                    $channel->sendMessage($msg, false);
129
                    setPermCache("fleetUpLastPostedOperation", $id);
130
                    $this->logger->addInfo("FleetUp: Upcoming Operation - {$name}");
131
                }
132
            }
133
        }
134
        setPermCache("fleetUpPostLastChecked", time() + 300);
135
    }
136
137
    function checkFleetUp()
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...
138
    {
139
        $discord = $this->discord;
140
141
        $lastChecked = getPermCache("fleetUpLastChecked");
142
143
        if ($lastChecked <= time()) {
144
145
            //fleetUp check for new operations
146
            $currentID = getPermCache("fleetUpLastOperation");
147
            $fleetUpOperations = json_decode(downloadData("http://api.fleet-up.com/Api.svc/tlYgBRjmuXj2Yl1lEOyMhlDId/{$this->userID}/{$this->apiKey}/Operations/{$this->groupID}"), true);
148
            foreach ($fleetUpOperations["Data"] as $operation) {
149
                $name = $operation["Subject"];
150
                $startTime = $operation["StartString"];
151
                $desto = $operation["Location"];
152
                $formUp = $operation["LocationInfo"];
153
                $info = $operation["Details"];
154
                $id = $operation["Id"];
155
                $link = "https://fleet-up.com/Operation#{$id}";
156
                var_dump($currentID);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($currentID); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
157
                var_dump($id);
158
                if ($currentID < $id) {
159
                    $msg = "
160
**New Operation Posted** 
161
Title - {$name} 
162
Form Up Time - {$startTime} 
163
Form Up System - {$formUp} 
164
Target System - {$desto} 
165
Details - {$info} 
166
167
Link - {$link}";
168
                    $channelID = $this->toDiscordChannel;
169
                    $guild = $discord->guilds->get('id', $this->guild);
170
                    $channel = $guild->channels->get('id', $channelID);
171
                    $channel->sendMessage($msg, false);
172
                }
173
                if ($id > $currentID) {
174
                    setPermCache("fleetUpLastOperation", $id);
175
                    $this->logger->addInfo("FleetUp: Newest FleetUp operation - {$name}");
176
                }
177
            }
178
            setPermCache("fleetUpLastChecked", time() + 300);
179
        }
180
    }
181
182
}
183