api_related::execute()   D
last analyzed

Complexity

Conditions 9
Paths 40

Size

Total Lines 34
Code Lines 25

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 34
rs 4.909
cc 9
eloc 25
nc 40
nop 1
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_related implements apiEndpoint
20
{
21
	public function getDescription()
22
	{
23
		return array("type" => "description", "message" =>
24
				"Shows killmails that are related to a killmail done at a certain time, in a certain system."
25
			);
26
	}
27
28
	public function getAcceptedParameters()
29
	{
30
		return array("type" => "parameters", "parameters" =>
31
			array(
32
				"systemID" => "The solarSystemID that the kills happened in.",
33
				"timestamp" => "The timestamp for when the kills should've happened. (YYYYmmddHHii)",
34
				"exHours" => "Amount of hours to span the battle over. 1 to 12."
35
			)
36
		);
37
	}
38
	public function execute($parameters)
39
	{
40
		$systemID = isset($parameters["solarSystemID"]) ? $parameters["solarSystemID"] : NULL;
41
		$timestamp = isset($parameters["timestamp"]) ? (int) $parameters["timestamp"] : NULL;
42
		$exHours = isset($parameters["exHours"]) ? (int) $parameters["exHours"] : 1;
43
44
		if(!$systemID)
45
			return array(
46
					"type" => "error",
47
					"message" => "solarSystemID isn't set."
48
				);
49
		if(!$timestamp)
0 ignored issues
show
Bug Best Practice introduced by
The expression $timestamp of type integer|null is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
50
			return array(
51
					"type" => "error",
52
					"message" => "timestamp isn't set."
53
				);
54
55
		if($exHours < 1 || $exHours > 12)
56
			return array(
57
					"type" => "error",
58
					"message" => "exHours is below 1 or above 12."
59
				);
60
61
		$params = array("solarSystemID" => $systemID, "relatedTime" => $timestamp, "exHours" => $exHours);
62
		$md5 = md5(serialize($params));
63
		$cache = Cache::get($md5);
64
		if($cache)
0 ignored issues
show
Bug Best Practice introduced by
The expression $cache of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
65
			return $cache;
66
67
		$kills = Kills::getKills($params);
68
		$data = Related::buildSummary($kills, $params, array());
69
		Cache::set($md5, $data, 3600);
70
		return $data;
71
	}
72
}
73