Passed
Pull Request — master (#106)
by Bob
03:51
created

eveApi.php ➔ systemName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 1
dl 13
loc 13
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 Monolog\Handler\StreamHandler;
27
use Monolog\Logger;
28
29
/**
30
 * @param $url
31
 * @return SimpleXMLElement|null|string
32
 */
33
function makeApiRequest($url)
34
{
35
    $logger = new Logger('eveApi');
36
    $logger->pushHandler(new StreamHandler(__DIR__ . '/../../log/libraryError.log', Logger::DEBUG));
37
    try {
38
        // Initialize a new request for this URL
39
        $ch = curl_init($url);
40
        // Set the options for this request
41
        curl_setopt_array($ch, array(
42
            CURLOPT_FOLLOWLOCATION => true, // Yes, we want to follow a redirect
43
            CURLOPT_RETURNTRANSFER => true, // Yes, we want that curl_exec returns the fetched data
44
            CURLOPT_SSL_VERIFYPEER => true, // Do not verify the SSL certificate
45
            CURLOPT_USERAGENT => 'Dramiel Discord Bot - https://github.com/shibdib/Dramiel', // Useragent
46
            CURLOPT_TIMEOUT => 15,
47
        ));
48
        // Fetch the data from the URL
49
        $data = curl_exec($ch);
50
        // Close the connection
51
        curl_close($ch);
52
        // Return a new SimpleXMLElement based upon the received data
53
        return new SimpleXMLElement($data);
54
    } catch (Exception $e) {
55
        $logger->error('EVE API Error: ' . $e->getMessage());
56
        return null;
57
    }
58
}
59
60
/**
61
 * @return mixed|null
62
 */
63
64
function serverStatus()
65
{
66
    $logger = new Logger('eveApi');
67
    $logger->pushHandler(new StreamHandler(__DIR__ . '../../log/libraryError.log', Logger::DEBUG));
68
    try {
69
        // Initialize a new request for this URL
70
        $ch = curl_init('https://api.eveonline.com/server/ServerStatus.xml.aspx');
71
        // Set the options for this request
72
        curl_setopt_array($ch, array(
73
            CURLOPT_FOLLOWLOCATION => true, // Yes, we want to follow a redirect
74
            CURLOPT_RETURNTRANSFER => true, // Yes, we want that curl_exec returns the fetched data
75
            CURLOPT_TIMEOUT => 8,
76
            CURLOPT_SSL_VERIFYPEER => true, // Do not verify the SSL certificate
77
        ));
78
        // Fetch the data from the URL
79
        $data = curl_exec($ch);
80
        // Close the connection
81
        curl_close($ch);
82
83
        $true = 'true';
84
        //If server is down return false
85
        if ($data->serverOpen !== 'True') {
86
            return FALSE;
87
        }
88
        //If server is up return true
89
        return $true;
90
    } catch (Exception $e) {
91
        $logger->error('EVE API Error: ' . $e->getMessage());
92
        return null;
93
    }
94
}
95
96
/**
97
 * @param string $characterID
98
 * @return mixed
99
 */
100
////Char ID to name via CCP
101
function characterName($characterID)
102
{
103
    $character = characterDetails($characterID);
104
    $name = (string)$character['name'];
105
106
    if (null === $name) { // Make sure it's always set.
107
        $name = 'Unknown';
108
    }
109
110
    return $name;
111
}
112
113
/**
114
 * @param string $characterName
115
 * @return mixed
116
 */
117
////Character name to ID
118
function characterID($characterName)
119
{
120
    $characterName = urlencode($characterName);
121
    $url = "https://esi.tech.ccp.is/latest/search/?search={$characterName}&categories=character&language=en-us&strict=true&datasource=tranquility";
122
    $json = file_get_contents($url);
123
    $data = json_decode($json, TRUE);
124
    $id = (int)$data['character'][0];
125
126
    if (null === $id) { // Make sure it's always set.
127
        $id = 'Unknown';
128
    }
129
130
    return $id;
131
}
132
133
/**
134
 * @param string $characterID
135
 * @return mixed
136
 */
137
////Char ID to char data via CCP
138
function characterDetails($characterID)
139
{
140
    $url = "https://esi.tech.ccp.is/latest/characters/{$characterID}/";
141
    $json = file_get_contents($url);
142
    $data = json_decode($json, TRUE);
143
144
    if (null === $data) { // Make sure it's always set.
145
        $data = 'Unknown';
146
    }
147
148
    return $data;
149
}
150
151
/**
152
 * @param string $systemID
153
 * @return mixed
154
 */
155
////System ID to name via CCP
156 View Code Duplication
function systemName($systemID)
0 ignored issues
show
Duplication introduced by
This function 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...
157
{
158
    $url = "https://esi.tech.ccp.is/latest/universe/systems/{$systemID}/";
159
    $json = file_get_contents($url);
160
    $data = json_decode($json, TRUE);
161
    $name = (string)$data['solar_system_name'];
162
163
    if (null === $name) { // Make sure it's always set.
164
        $name = 'Unknown';
165
    }
166
167
    return $name;
168
}
169
170
/**
171
 * @param string $corpName
172
 * @return mixed
173
 */
174
////Corp name to ID
175
function corpID($corpName)
176
{
177
    $corpName = urlencode($corpName);
178
    $url = "https://esi.tech.ccp.is/latest/search/?search={$corpName}&categories=corporation&language=en-us&strict=true&datasource=tranquility";
179
    $json = file_get_contents($url);
180
    $data = json_decode($json, TRUE);
181
    $id = (int)$data['corporation'][0];
182
183
    if (null === $id) { // Make sure it's always set.
184
        $id = 'Unknown';
185
    }
186
187
    return $id;
188
}
189
190
191
/**
192
 * @param string $corpID
193
 * @return mixed
194
 */
195
////Corp ID to name via CCP
196
function corpName($corpID)
197
{
198
    $corporation = corpDetails($corpID);
199
    $name = (string)$corporation['corporation_name'];
200
201
    if (null === $name) { // Make sure it's always set.
202
        $name = 'Unknown';
203
    }
204
205
    return $name;
206
}
207
208
/**
209
 * @param string $corpID
210
 * @return mixed
211
 */
212
////Corp ID to corp data via CCP
213
function corpDetails($corpID)
214
{
215
    $url = "https://esi.tech.ccp.is/latest/corporations/{$corpID}/";
216
    $json = file_get_contents($url);
217
    $data = json_decode($json, TRUE);
218
219
    if (null === $data) { // Make sure it's always set.
220
        $data = 'Unknown';
221
    }
222
223
    return $data;
224
}
225
226
/**
227
 * @param string $allianceID
228
 * @return mixed
229
 */
230
////Alliance ID to name via CCP
231 View Code Duplication
function allianceName($allianceID)
0 ignored issues
show
Duplication introduced by
This function 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...
232
{
233
    $url = "https://esi.tech.ccp.is/latest/alliances/{$allianceID}/";
234
    $json = file_get_contents($url);
235
    $data = json_decode($json, TRUE);
236
    $name = (string)$data['alliance_name'];
237
238
    if (null === $name) { // Make sure it's always set.
239
        $name = 'Unknown';
240
    }
241
242
    return $name;
243
}
244
245
/**
246
 * @param string $systemName
247
 * @return mixed
248
 */
249
////System name to ID
250
function systemID($systemName)
251
{
252
    $systemName = urlencode($systemName);
253
    $url = "https://esi.tech.ccp.is/latest/search/?search={$systemName}&categories=solarsystem&language=en-us&strict=true&datasource=tranquility";
254
    $json = file_get_contents($url);
255
    $data = json_decode($json, TRUE);
256
    $id = (int)$data['solarsystem'][0];
257
258
    if (null === $id) { // Make sure it's always set.
259
        $id = 'Unknown';
260
    }
261
262
    return $id;
263
}
264
265
/**
266
 * @param string $typeID
267
 * @return mixed
268
 */
269
////TypeID to TypeName via CCP
270 View Code Duplication
function apiTypeName($typeID)
271
{
272
    $url = "https://esi.tech.ccp.is/latest/universe/types/{$typeID}/";
273
    $json = file_get_contents($url);
274
    $data = json_decode($json, TRUE);
275
    $name = (string)$data['type_name'];
276
277
    if (null === $name) { // Make sure it's always set.
278
        $name = 'Unknown';
279
    }
280
281
    return $name;
282
}
283
284
/**
285
 * @param string $typeName
286
 * @return mixed
287
 */
288
////TypeName to TypeID via fuzz
289 View Code Duplication
function apiTypeID($typeName)
290
{
291
    $typeName = urlencode($typeName);
292
    $url = "https://www.fuzzwork.co.uk/api/typeid.php?typename={$typeName}";
293
    $json = file_get_contents($url);
294
    $data = json_decode($json, TRUE);
295
    $id = (int)$data['typeID'];
296
297
    if (null === $id) { // Make sure it's always set.
298
        $id = 'Unknown';
299
    }
300
301
    return $id;
302
}
303
304
////Char/Object ID to name via CCP
305
function apiMoonName($moonID)
306
{
307
    $url = "https://api.eveonline.com/eve/CharacterName.xml.aspx?IDs={$moonID}";
308
    $xml = makeApiRequest($url);
309
    $name = null;
310
    foreach ($xml->result->rowset->row as $entity) {
311
        $name = $entity->attributes()->name;
312
    }
313
    if (null === $name) { // Make sure it's always set.
314
        $name = 'Unknown';
315
    }
316
    return $name;
317
}