Passed
Pull Request — master (#98)
by Bob
02:26
created

price   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 139
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 0
dl 0
loc 139
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 12 1
C onMessage() 0 91 10
A information() 0 8 1
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
/**
27
 * Class price
28
 */
29
class price
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...
30
{
31
    /**
32
     * @var array
33
     */
34
    public $triggers = array();
35
    private $excludeChannel;
36
    private $message;
37
    private $config;
38
    private $discord;
39
    private $logger;
40
41
    /**
42
     * @param $config
43
     * @param $discord
44
     * @param $logger
45
     */
46
    public function init($config, $discord, $logger)
47
    {
48
        $this->config = $config;
49
        $this->discord = $discord;
50
        $this->logger = $logger;
51
        $this->triggers[] = $this->config['bot']['trigger'] . 'pc';
52
        $this->triggers[] = $this->config['bot']['trigger'] . strtolower('Jita');
53
        $this->triggers[] = $this->config['bot']['trigger'] . strtolower('Amarr');
54
        $this->triggers[] = $this->config['bot']['trigger'] . strtolower('Rens');
55
        $this->triggers[] = $this->config['bot']['trigger'] . strtolower('Dodixie');
56
        $this->excludeChannel = $this->config['bot']['restrictedChannels'];
57
    }
58
59
    /**
60
     * @param $msgData
61
     * @param $message
62
     * @return null
63
     */
64
    public function onMessage($msgData, $message)
65
    {
66
        $this->message = $message;
67
        $user = $msgData['message']['from'];
68
        $channelID = (int) $msgData['message']['channelID'];
69
70
        if (in_array($channelID, $this->excludeChannel, true))
71
        {
72
            return null;
73
        }
74
75
76
        // Bind a few things to vars for the plugins
77
        $message = $msgData['message']['message'];
78
79
        // Quick Lookups
80
        $quickLookUps = array(
81
            'plex' => array(
82
                'typeID' => 29668,
83
                'typeName' => "30 Day Pilot's License Extension (PLEX)"
84
            ),
85
            '30 day' => array(
86
                'typeID' => 29668,
87
                'typeName' => "30 Day Pilot's License Extension (PLEX)"
88
            )
89
        );
90
91
        $data = command(strtolower($message), $this->information()['trigger'], $this->config['bot']['trigger']);
92
93
        if (isset($data['trigger'])) {
94
95
            $systemName = $data['trigger'];
96
            $itemName = $data['messageString'];
97
            $single = apiTypeID(urlencode($itemName));
98
99
            // Quick lookups
100
            if (isset($quickLookUps[$itemName])) {
101
                $single = $quickLookUps[$itemName];
102
            }
103
104
            // Check if the channel is restricted
105
            if (in_array($channelID, $this->excludeChannel, true)) {
106
                return $this->message->reply('**Price Check not allowed in this channel**');
107
            }
108
109
            // If there is a single result, we'll get data now!
110
            if ($single) {
111
                $typeID = $single['typeID'];
112
113
                if (null === $typeID) {
114
                    $typeID = $single;
115
                }
116
117
                if ($systemName === 'pc') {
118
                    $solarSystemID = 'global';
119
                } else {
120
                    $solarSystemID = apiCharacterID(urlencode($systemName));
121
                }
122
123
                // Get pricing data
124
                if ($solarSystemID === 'global') {
125
                    $data = new SimpleXMLElement(downloadData("https://api.eve-central.com/api/marketstat?typeid={$typeID}"));
126
                } else {
127
                    $data = new SimpleXMLElement(downloadData("https://api.eve-central.com/api/marketstat?usesystem={$solarSystemID}&typeid={$typeID}"));
128
                }
129
130
                $lowBuy = number_format((float) $data->marketstat->type->buy->min, 2);
131
                $avgBuy = number_format((float) $data->marketstat->type->buy->avg, 2);
132
                $highBuy = number_format((float) $data->marketstat->type->buy->max, 2);
133
                $lowSell = number_format((float) $data->marketstat->type->sell->min, 2);
134
                $avgSell = number_format((float) $data->marketstat->type->sell->avg, 2);
135
                $highSell = number_format((float) $data->marketstat->type->sell->max, 2);
136
137
                $this->logger->addInfo("Price: Sending pricing info to {$user}");
138
                $solarSystemName = $systemName === 'pc' ? 'Global' : ucfirst($systemName);
139
                $messageData = "**System: {$solarSystemName}**
140
**Buy:**
141
   Low: {$lowBuy}
142
   Avg: {$avgBuy}
143
   High: {$highBuy}
144
**Sell:**
145
   Low: {$lowSell}
146
   Avg: {$avgSell}
147
   High: {$highSell}";
148
                $this->message->reply($messageData);
149
            } else {
150
                $this->message->reply("**Error:** ***{$itemName}*** not found");
151
            }
152
        }
153
        return null;
154
    }
155
156
    /**
157
     * @return array
158
     */
159
    private function information()
160
    {
161
        return array(
162
            'name' => 'pc',
163
            'trigger' => $this->triggers,
164
            'information' => 'Shows price information for items in EVE. To use simply type **!pc item_name** for global stats or **!jita/amarr/rens_or_dodixie item_name** for hub specific info.'
165
        );
166
    }
167
}
168