Passed
Branch dev (e28127)
by
unknown
03:00
created

eveApi.php ➔ systemName()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 17
nc 8
nop 1
dl 0
loc 24
rs 8.5125
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 View Code Duplication
function characterName($characterID)
102
{
103
    $character = characterDetails($characterID);
104
    $name = (string)$character['name'];
105
    if (null === $name || '' === $name) { // Make sure it's always set.
106
        $url = "https://api.eveonline.com/eve/CharacterName.xml.aspx?ids={$characterID}";
107
        $xml = makeApiRequest($url);
108
        foreach ($xml->result->rowset->row as $entity) {
109
            $name = $entity->attributes()->name;
110
        }
111
    }
112
    return $name;
113
}
114
115
/**
116
 * @param string $characterName
117
 * @return mixed
118
 */
119
////Character name to ID
120 View Code Duplication
function characterID($characterName)
121
{
122
    $logger = new Logger('eveESI');
123
    $logger->pushHandler(new StreamHandler(__DIR__ . '../../log/libraryError.log', Logger::DEBUG));
124
    $characterName = urlencode($characterName);
125
    $url = "https://esi.tech.ccp.is/latest/search/?search={$characterName}&categories=character&language=en-us&strict=true&datasource=tranquility";
126
127
    try {
128
        $json = file_get_contents($url);
129
        $data = json_decode($json, TRUE);
130
        $id = (int) $data['character'][0];
131
132
        if (null === $id) { // Make sure it's always set.
133
            return null;
134
        }
135
136
    } catch (Exception $e) {
137
        $logger->error('EVE ESI Error: ' . $e->getMessage());
138
        return null;
139
    }
140
141
    return $id;
142
}
143
144
/**
145
 * @param string $characterID
146
 * @return mixed
147
 */
148
////Char ID to char data via CCP
149 View Code Duplication
function characterDetails($characterID)
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...
150
{
151
    $logger = new Logger('eveESI');
152
    $logger->pushHandler(new StreamHandler(__DIR__ . '../../log/libraryError.log', Logger::DEBUG));
153
    $url = "https://esi.tech.ccp.is/latest/characters/{$characterID}/";
154
155
    try {
156
        $json = file_get_contents($url);
157
        $data = json_decode($json, TRUE);
158
159
    } catch (Exception $e) {
160
        $logger->error('EVE ESI Error: ' . $e->getMessage());
161
        return null;
162
    }
163
164
    return $data;
165
}
166
167
/**
168
 * @param string $systemID
169
 * @return mixed
170
 */
171
////System ID to name via CCP
172
function systemName($systemID)
173
{
174
    $logger = new Logger('eveESI');
175
    $logger->pushHandler(new StreamHandler(__DIR__ . '../../log/libraryError.log', Logger::DEBUG));
176
    $url = "https://esi.tech.ccp.is/latest/universe/systems/{$systemID}/";
177
178
    try {
179
        $json = file_get_contents($url);
180
        $data = json_decode($json, TRUE);
181
        $name = (string) $data['solar_system_name'];
182
        if (null === $name || '' === $name) { // Make sure it's always set.
183
            $url = "https://api.eveonline.com/eve/CharacterName.xml.aspx?ids={$systemID}";
184
            $xml = makeApiRequest($url);
185
            foreach ($xml->result->rowset->row as $entity) {
186
                $name = $entity->attributes()->name;
187
            }
188
        }
189
    } catch (Exception $e) {
190
        $logger->error('EVE ESI Error: ' . $e->getMessage());
191
        return null;
192
    }
193
194
    return $name;
195
}
196
197
/**
198
 * @param string $corpName
199
 * @return mixed
200
 */
201
////Corp name to ID
202 View Code Duplication
function corpID($corpName)
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...
203
{
204
    $logger = new Logger('eveESI');
205
    $logger->pushHandler(new StreamHandler(__DIR__ . '../../log/libraryError.log', Logger::DEBUG));
206
    $corpName = urlencode($corpName);
207
    $url = "https://esi.tech.ccp.is/latest/search/?search={$corpName}&categories=corporation&language=en-us&strict=true&datasource=tranquility";
208
209
    try {
210
        $json = file_get_contents($url);
211
        $data = json_decode($json, TRUE);
212
        $id = (int) $data['corporation'][0];
213
214
        if (null === $id) { // Make sure it's always set.
215
            return null;
216
        }
217
218
    } catch (Exception $e) {
219
        $logger->error('EVE ESI Error: ' . $e->getMessage());
220
        return null;
221
    }
222
223
    return $id;
224
}
225
226
227
/**
228
 * @param string $corpID
229
 * @return mixed
230
 */
231
////Corp ID to name via CCP
232 View Code Duplication
function corpName($corpID)
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...
233
{
234
    $corporation = corpDetails($corpID);
235
    $name = (string) $corporation['corporation_name'];
236
    if (null === $name || '' === $name) { // Make sure it's always set.
237
        $url = "https://api.eveonline.com/eve/CharacterName.xml.aspx?ids={$corpID}";
238
        $xml = makeApiRequest($url);
239
        foreach ($xml->result->rowset->row as $entity) {
240
            $name = $entity->attributes()->name;
241
        }
242
    }
243
244
    return $name;
245
}
246
247
/**
248
 * @param string $corpID
249
 * @return mixed
250
 */
251
////Corp ID to corp data via CCP
252 View Code Duplication
function corpDetails($corpID)
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...
253
{
254
    $logger = new Logger('eveESI');
255
    $logger->pushHandler(new StreamHandler(__DIR__ . '../../log/libraryError.log', Logger::DEBUG));
256
    $url = "https://esi.tech.ccp.is/latest/corporations/{$corpID}/";
257
258
    try {
259
        $json = file_get_contents($url);
260
        $data = json_decode($json, TRUE);
261
262
    } catch (Exception $e) {
263
        $logger->error('EVE ESI Error: ' . $e->getMessage());
264
        return null;
265
    }
266
267
    return $data;
268
}
269
270
/**
271
 * @param string $allianceID
272
 * @return mixed
273
 */
274
////Alliance ID to name via CCP
275 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...
276
{
277
    $url = "https://esi.tech.ccp.is/latest/alliances/{$allianceID}/";
278
    $json = file_get_contents($url);
279
    $data = json_decode($json, TRUE);
280
    $name = (string) $data['alliance_name'];
281
    if (null === $name || '' === $name) { // Make sure it's always set.
282
        $url = "https://api.eveonline.com/eve/CharacterName.xml.aspx?ids={$allianceID}";
283
        $xml = makeApiRequest($url);
284
        foreach ($xml->result->rowset->row as $entity) {
285
            $name = $entity->attributes()->name;
286
        }
287
    }
288
289
    return $name;
290
}
291
292
/**
293
 * @param string $systemName
294
 * @return mixed
295
 */
296
////System name to ID
297
function systemID($systemName)
298
{
299
    $systemName = urlencode($systemName);
300
    $url = "https://esi.tech.ccp.is/latest/search/?search={$systemName}&categories=solarsystem&language=en-us&strict=true&datasource=tranquility";
301
    $json = file_get_contents($url);
302
    $data = json_decode($json, TRUE);
303
    $id = (int) $data['solarsystem'][0];
304
305
    if (null === $id) { // Make sure it's always set.
306
        $id = 'Unknown';
307
    }
308
309
    return $id;
310
}
311
312
/**
313
 * @param string $typeID
314
 * @return mixed
315
 */
316
////TypeID to TypeName via CCP
317 View Code Duplication
function apiTypeName($typeID)
318
{
319
    $url = "https://esi.tech.ccp.is/latest/universe/types/{$typeID}/";
320
    $json = file_get_contents($url);
321
    $data = json_decode($json, TRUE);
322
    $name = (string) $data['type_name'];
323
    if (null === $name || '' === $name) { // Make sure it's always set.
324
        $url = "https://api.eveonline.com/eve/TypeName.xml.aspx?ids={$typeID}";
325
        $xml = makeApiRequest($url);
326
        foreach ($xml->result->rowset->row as $entity) {
327
            $name = $entity->attributes()->typeName;
328
        }
329
    }
330
331
    return $name;
332
}
333
334
/**
335
 * @param string $typeName
336
 * @return mixed
337
 */
338
////TypeName to TypeID via fuzz
339
function apiTypeID($typeName)
340
{
341
    $typeName = urlencode($typeName);
342
    $url = "https://www.fuzzwork.co.uk/api/typeid.php?typename={$typeName}";
343
    $json = file_get_contents($url);
344
    $data = json_decode($json, TRUE);
345
    $id = (int) $data['typeID'];
346
347
    if (null === $id) { // Make sure it's always set.
348
        $id = 'Unknown';
349
    }
350
351
    return $id;
352
}
353
354
////Char/Object ID to name via CCP
355
function apiMoonName($moonID)
356
{
357
    $url = "https://api.eveonline.com/eve/CharacterName.xml.aspx?IDs={$moonID}";
358
    $xml = makeApiRequest($url);
359
    $name = null;
360
    foreach ($xml->result->rowset->row as $entity) {
361
        $name = $entity->attributes()->name;
362
    }
363
    if (null === $name) { // Make sure it's always set.
364
        $name = 'Unknown';
365
    }
366
    return $name;
367
}