api_losses::getDescription()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/* zKillboard
3
 * Copyright (C) 2012-2015 EVE-KILL Team and EVSCO.
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Affero General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU Affero General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Affero General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
class api_losses implements apiEndpoint
20
{
21
	public function getDescription()
22
	{
23
		return array("type" => "description", "message" =>
24
				"Shows losses for various entities, using various parameters. You must as minimum pass one of the requiredParameters."
25
			);
26
	}
27
28
	public function getAcceptedParameters()
29
	{
30
		return array("type" => "parameters",
31
			"parameters" => array(
32
				"characterID" => "list losses for a certain characterID.",
33
				"corporationID" => "list losses for a certain corporationID.",
34
				"allianceID" => "list losses for a certain allianceID.",
35
				"factionID" => "list losses for a certain factionID.",
36
				"shipTypeID" => "list losses where a certain shipTypeID is involved.",
37
				"solarSystemID" => "list losses that happened in a certain solarSystemID.",
38
				"regionID" => "list losses that happened in a certain regionID.",
39
				"w-space" => "Only list losses that has happened in wormhole space-",
40
				"page" => "Pagination.",
41
				"orderDirection" => "ASC: Oldest to newest, DESC: newest to oldest (DESC is faster than ASC, by a factor 100).",
42
				"pastSeconds" => "only show losses that has happened in the past number of seconds.",
43
				"startTime" => "Show losses from a certain startTime. (Requires endTime).",
44
				"endTime" => "Show losses to a certain endTime. (Requires startTime).",
45
				"limit" => "Limit the amount of losses shown. Minimum 1, maximum 1000.",
46
				"beforeKillID" => "Show killmails from before this killID.",
47
				"afterKillID" => "Show killmails after this killID.",
48
				"iskValue" => "Only show killmails with a total iskValue above this.",
49
				"no-attackers" => "Remove attackers from the killmail.",
50
				"no-items" => "Remove items from the killmail.",
51
				"finalblow-only" => "Only show killmails where the entity caused a finalBlow (Doesn't work with solarSystemID and regionID)."
52
			),
53
			"requiredParameters" => array(
54
				"characterID",
55
				"corporationID",
56
				"allianceID",
57
				"factionID",
58
				"shipTypeID",
59
				"solarSystemID",
60
				"regionID",
61
				"w-space"
62
			)
63
		);
64
	}
65
	public function execute($parameters)
66
	{
67
		// Generate an accepted parameters array.
68
		$acceptedParameters = self::getAcceptedParameters();
69
		foreach($acceptedParameters["parameters"] as $key => $value)
70
			$acceptedParameters[] = $key;
71
72
		// Remove unwanted parameters
73
		foreach($parameters as $key => $value)
74
			if(!in_array($key, $acceptedParameters))
75
				unset($parameters[$key]);
76
77
		// It's the losses endpoint, so it has to be true..
78
		$parameters["losses"] = true;
79
80
		// If there aren't enough parameters being passed, throw an error.
81
		if(count($parameters) < 2)
82
			return array(
83
				"type" => "error",
84
				"message" => "Invalid request. Atleast two parameters are required."
85
			);
86
87
		// API is true
88
		$paramters["api"] = true;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$paramters was never initialized. Although not strictly required by PHP, it is generally a good practice to add $paramters = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
89
90
		// At least one of these parameters is required
91
		$requiredM = array("characterID", "corporationID", "allianceID", "factionID", "shipTypeID", "solarSystemID", "regionID", "w-space");
92
		$hasRequired = false;
93
		foreach($requiredM as $required)
94
			$hasRequired |= array_key_exists($required, $parameters);
95
96
		if (!isset($parameters["killID"]) && !$hasRequired)
97
			return array(
98
				"type" => "error",
99
				"message" => "Error, must pass atleast one required parameters."
100
			);
101
102
		$requestURI = $_SERVER["REQUEST_URI"];
103
		$key = md5("lossesApi:$requestURI");
104
105
		$data = Cache::get($key);
106
		if(empty($data))
107
		{
108
			$data = Feed::getKills($parameters);
109
			Cache::set($key, $data, 3600);
110
		}
111
112
		$return = array();
113
		foreach($data as $kill)
114
			$return[] = json_decode($kill, true);
115
116
		return $return;
117
	}
118
}
119