Test Setup Failed
Push — master ( fec420...86fb9b )
by Bob
06:30 queued 03:13
created

eveApi.php ➔ apiTypeName()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 1
dl 14
loc 14
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\Logger;
27
use Monolog\Handler\StreamHandler;
28
29
/**
30
 * @param $url
31
 * @return SimpleXMLElement|null
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 => false, // Do not verify the SSL certificate
45
            CURLOPT_TIMEOUT => 15,
46
        ));
47
        // Fetch the data from the URL
48
        $data = curl_exec($ch);
49
        // Close the connection
50
        curl_close($ch);
51
        // Return a new SimpleXMLElement based upon the received data
52
        return new SimpleXMLElement($data);
53
    } catch (Exception $e) {
54
        $logger->error("EVE API Error: " . $e->getMessage());
55
        return null;
56
    }
57
}
58
59
/**
60
 * @return mixed|null
61
 */
62
63
function serverStatus()
64
{
65
    $logger = new Logger('eveApi');
66
    $logger->pushHandler(new StreamHandler(__DIR__ . '/log/libraryError.log', Logger::DEBUG));
67
    try {
68
        // Initialize a new request for this URL
69
        $ch = curl_init("https://api.eveonline.com/server/ServerStatus.xml.aspx");
70
        // Set the options for this request
71
        curl_setopt_array($ch, array(
72
            CURLOPT_FOLLOWLOCATION => true, // Yes, we want to follow a redirect
73
            CURLOPT_RETURNTRANSFER => true, // Yes, we want that curl_exec returns the fetched data
74
            CURLOPT_TIMEOUT => 8,
75
            CURLOPT_SSL_VERIFYPEER => false, // Do not verify the SSL certificate
76
        ));
77
        // Fetch the data from the URL
78
        $data = curl_exec($ch);
79
        // Close the connection
80
        curl_close($ch);
81
82
        $true = "true";
83
        //If server is down return false
84
        if ($data->serverOpen != "True") {
85
            return FALSE;
86
        }
87
        //If server is up return true
88
        return $true;
89
    } catch (Exception $e) {
90
        $logger->error("EVE API Error: " . $e->getMessage());
91
        return null;
92
    }
93
}
94
95
/**
96
 * @param string $typeID
97
 * @return mixed
98
 */
99 View Code Duplication
function apiCharacterName($typeID)
1 ignored issue
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...
100
{
101
    $url = "https://api.eveonline.com/eve/CharacterName.xml.aspx?IDs={$typeID}";
102
    $xml = makeApiRequest($url);
103
    foreach ($xml->result->rowset->row as $entity) {
104
        $name = $entity->attributes()->name;
105
    }
106
107
    if (!isset($name)) { // Make sure it's always set.
108
        $name = "Unknown";
109
    }
110
111
    return $name;
112
}
113
114
/**
115
 * @param string $typeName
116
 * @return mixed
117
 */
118 View Code Duplication
function apiCharacterID($typeName)
1 ignored issue
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...
119
{
120
    $url = "https://api.eveonline.com/eve/CharacterID.xml.aspx?names={$typeName}";
121
    $xml = makeApiRequest($url);
122
    foreach ($xml->result->rowset->row as $entity) {
123
        $ID = $entity->attributes()->characterID;
124
    }
125
126
    if (!isset($ID)) { // Make sure it's always set.
127
        $ID = "Unknown";
128
    }
129
130
    return $ID;
131
}
132
133
/**
134
 * @param string $typeID
135
 * @return mixed
136
 */
137 View Code Duplication
function apiTypeName($typeID)
1 ignored issue
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...
138
{
139
    $url = "https://api.eveonline.com/eve/TypeName.xml.aspx?IDs={$typeID}";
140
    $xml = makeApiRequest($url);
141
    foreach ($xml->result->rowset->row as $entity) {
142
        $name = $entity->attributes()->typeName;
143
    }
144
145
    if (!isset($name)) { // Make sure it's always set.
146
        $name = "Unknown";
147
    }
148
149
    return $name;
150
}
151
152
/**
153
 * @param string $typeName
154
 * @return mixed
155
 */
156 View Code Duplication
function apiTypeID($typeName)
1 ignored issue
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://www.fuzzwork.co.uk/api/typeid.php?typename={$typeName}&format=xml";
159
    $xml = makeApiRequest($url);
160
    foreach ($xml->result->rowset->row as $entity) {
161
        $ID = $entity->attributes()->typeID;
162
    }
163
164
    if (!isset($ID)) { // Make sure it's always set.
165
        $ID = "Unknown";
166
    }
167
168
    return $ID;
169
}