Passed
Branch master (f30628)
by
unknown
02:34
created

auth::onMessage()   F

Complexity

Conditions 33
Paths 381

Size

Total Lines 133
Code Lines 86

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 33
eloc 86
nc 381
nop 2
dl 0
loc 133
rs 3.425
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
use discord\discord;
26
27
/**
28
 * Class auth
29
 */
30
class auth
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...
31
{
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
    var $solarSystems;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $solarSystems.

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
    var $triggers = array();
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $triggers.

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...
46
    var $excludeChannel;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $excludeChannel.

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
    var $nameEnforce;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $nameEnforce.

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 $guildID;
49
    public $db;
50
    public $dbUser;
51
    public $dbPass;
52
    public $dbName;
53
    public $ssoUrl;
54
    public $allianceTickers;
55
    public $corpTickers;
56
    public $authGroups;
57
    public $guild;
58
59
    /**
60
     * @param $config
61
     * @param $discord
62
     * @param $logger
63
     */
64
    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...
65
    {
66
        $this->config = $config;
67
        $this->discord = $discord;
68
        $this->logger = $logger;
69
        $this->db = $config["database"]["host"];
70
        $this->dbUser = $config["database"]["user"];
71
        $this->dbPass = $config["database"]["pass"];
72
        $this->dbName = $config["database"]["database"];
73
        $this->corpTickers = $config["plugins"]["auth"]["corpTickers"];
74
        $this->nameEnforce = $config["plugins"]["auth"]["nameEnforce"];
75
        $this->ssoUrl = $config["plugins"]["auth"]["url"];
76
        $this->excludeChannel = $this->config["bot"]["restrictedChannels"];
77
        $this->authGroups = $config["plugins"]["auth"]["authGroups"];
78
        $this->guild = $config["bot"]["guild"];
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
    }
87
88
    /**
89
     * @param $msgData
90
     * @param $message
91
     * @return null
92
     */
93
    function onMessage($msgData, $message)
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...
94
    {
95
        $channelID = (int)$msgData["message"]["channelID"];
96
97
        if (in_array($channelID, $this->excludeChannel, true)) {
98
            return null;
99
        }
100
101
        $this->message = $message;
102
        $userID = $msgData["message"]["fromID"];
103
        $userName = $msgData["message"]["from"];
104
        $message = $msgData["message"]["message"];
105
        $channelInfo = $this->message->channel;
106
        $guildID = $channelInfo[@guild_id];
107
        $data = command($message, $this->information()["trigger"], $this->config["bot"]["trigger"]);
108
        if (isset($data["trigger"])) {
109
            if (isset($this->config["bot"]["primary"])) {
110
                if ($guildID != $this->config["bot"]["primary"]) {
111
                    $this->message->reply("**Failure:** The auth code your attempting to use is for another discord server");
112
                    return null;
113
                }
114
115
            }
116
            // If config is outdated
117
            if (is_null($this->authGroups)) {
118
                $this->message->reply("**Failure:** Please update the bots config to the latest version.");
119
                return null;
120
            }
121
122
            $code = $data["messageString"];
123
            $result = selectPending($this->db, $this->dbUser, $this->dbPass, $this->dbName, $code);
124
125
            if (strlen($code) < 12) {
126
                $this->message->reply("Invalid Code, check " . $this->config["bot"]["trigger"] . "help auth for more info.");
127
                return null;
128
            }
129
130
            while ($rows = $result->fetch_assoc()) {
131
                $charID = (int)$rows['characterID'];
132
                $corpID = (int)$rows['corporationID'];
133
                $allianceID = (int)$rows['allianceID'];
134
                $url = "https://api.eveonline.com/eve/CharacterName.xml.aspx?ids=$charID";
135
                $xmlCharacter = makeApiRequest($url);
136
137
138
                // We have an error, show it it
139
                if ($xmlCharacter->error) {
140
                    $this->message->reply("**Failure:** Eve API error, please try again in a little while.");
141
                    return null;
142
                }
143
144
                // Check that the api is working
145
                if (!isset($xmlCharacter->result->rowset->row)) {
146
                    $this->message->reply("**Failure:** Eve API error, please try again in a little while.");
147
                    return null;
148
                }
149
150
                //Add corp ticker to name
151
                if ($this->corpTickers == 'true') {
152
                    $url = "https://api.eveonline.com/corp/CorporationSheet.xml.aspx?corporationID={$corpID}";
153
                    $xml = makeApiRequest($url);
154
                    $setTicker = 1;
155
                    foreach ($xml->result as $corporation) {
156
                        $corpTicker = $corporation->ticker;
157
                    }
158
                }
159
160
                //Set eve name if nameCheck is true
161
                if ($this->nameEnforce == "true") {
162
                    $nameEnforce = 1;
163
                }
164
165
                $allianceRoleSet = 0;
166
                $corpRoleSet = 0;
167
168
                foreach ($xmlCharacter->result->rowset->row as $character) {
169
                    $roles = $this->message->channel->guild->roles;
170
                    $member = $this->message->channel->guild->members->get("id", $userID);
171
                    $eveName = $character->attributes()->name;
172
                    foreach ($this->authGroups as $authGroup) {
173
                        //Check if corpID matches
174
                        if ($corpID === $authGroup["corpID"]) {
175
                            foreach ($roles as $role) {
176
                                if ($role->name == $authGroup["corpMemberRole"]) {
177
                                    $member->addRole($role);
178
                                    $corpRoleSet = 1;
179
                                }
180
                            }
181
                        }
182
                        //Check if allianceID matches
183
                        if ($allianceID === $authGroup["allianceID"] && $authGroup["allianceID"] != 0) {
184
                            foreach ($roles as $role) {
185
                                if ($role->name == $authGroup["allyMemberRole"]) {
186
                                    $member->addRole($role);
187
                                    $allianceRoleSet = 1;
188
                                }
189
                            }
190
                        }
191
                        if ($allianceRoleSet == 1 || $corpRoleSet == 1) {
192
                            $guild = $this->discord->guilds->get('id', $guildID);
193
                            insertUser($this->db, $this->dbUser, $this->dbPass, $this->dbName, $userID, $charID, $eveName, 'corp');
194
                            disableReg($this->db, $this->dbUser, $this->dbPass, $this->dbName, $code);
195
                            $msg = ":white_check_mark: **Success:** {$userName} has been successfully authed.";
196
                            $this->logger->addInfo("auth: {$eveName} authed");
197
                            priorityQueueMessage($msg, $channelID, $this->guild);
198
                            //Add ticker if set and change name if nameEnforce is on
199
                            if (isset($setTicker) || isset($nameEnforce)) {
200
                                if (isset($setTicker) && isset($nameEnforce)) {
201
                                    $nick = "[{$corpTicker}] {$eveName}";
0 ignored issues
show
Bug introduced by
The variable $corpTicker does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
202
                                } elseif (!isset($setTicker) && isset($nameEnforce)) {
203
                                    $nick = "{$eveName}";
204
                                } elseif (isset($setTicker) && !isset($nameEnforce)) {
205
                                    $nick = "[{$corpTicker}] {$userName}";
206
                                }
207
                            }
208
                            if (isset($nick)) {
209
                                queueRename($userID, $nick, $this->guild);
210
                            }
211
                            $guild->members->save($member);
212
                            return null;
213
                        }
214
                    }
215
                    $this->message->reply("**Failure:** There are no roles available for your corp/alliance.");
216
                    $this->logger->addInfo("Auth: User was denied due to not being in the correct corp or alliance " . $eveName);
217
                    return null;
218
                }
219
            }
220
            $this->message->reply("**Failure:** There was an issue with your code.");
221
            $this->logger->addInfo("Auth: User was denied due to not being in the correct corp or alliance " . $userName);
222
            return null;
223
        }
224
        return null;
225
    }
226
227
    /**
228
     * @return array
229
     */
230 View Code Duplication
    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...
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...
231
    {
232
        return array(
233
            "name" => "auth",
234
            "trigger" => array($this->config["bot"]["trigger"] . "auth"),
235
            "information" => "SSO based auth system. " . $this->ssoUrl . " Visit the link and login with your main EVE account, select the correct character, and put the !auth <string> you receive in chat."
236
        );
237
    }
238
239
    function onMessageAdmin()
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...
240
    {
241
    }
242
}