1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
// @todo Go over this ENTIRE file again as there's been major refactors since and it's likely broken! |
4
|
|
|
|
5
|
|
|
namespace Ps2alerts\Api\Controller\Endpoint\Leaderboards; |
6
|
|
|
|
7
|
|
|
use Ps2alerts\Api\Controller\Endpoint\AbstractEndpointController; |
8
|
|
|
use Ps2alerts\Api\Exception\InvalidArgumentException; |
9
|
|
|
|
10
|
|
|
abstract class AbstractLeaderboardEndpointController extends AbstractEndpointController |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Validates the request variables |
14
|
|
|
* |
15
|
|
|
* @throws InvalidArgumentException |
16
|
|
|
* @return boolean |
17
|
|
|
*/ |
18
|
|
|
public function validateRequestVars() |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
try { |
21
|
|
|
if (! empty($_GET['field'])) { |
22
|
|
|
$this->parseField($_GET['field']); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
if (! empty($_GET['server'])) { |
26
|
|
|
$this->parseServer($_GET['server']); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
if (! empty($_GET['limit'])) { |
30
|
|
|
$this->parseOffset($_GET['limit']); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if (! empty($_GET['offset'])) { |
34
|
|
|
$this->parseOffset($_GET['offset']); |
35
|
|
|
} |
36
|
|
|
} catch (InvalidArgumentException $e) { |
37
|
|
|
return $e; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return true; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Validate the field requested |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
|
|
public function parseField($field) |
49
|
|
|
{ |
50
|
|
|
$validFields = [ |
51
|
|
|
'kills', |
52
|
|
|
'deaths', |
53
|
|
|
'teamkills', |
54
|
|
|
'suicides', |
55
|
|
|
'headshots', |
56
|
|
|
'captures' |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
if (! empty($field) && in_array($field, $validFields)) { |
60
|
|
|
return $field; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
throw new InvalidArgumentException("Field '{$field}' is not supported."); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Validate the server requested |
68
|
|
|
* |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function parseServer($server) |
72
|
|
|
{ |
73
|
|
|
$validServers = $this->getConfigItem('servers'); |
74
|
|
|
|
75
|
|
|
// Remove Jaeger |
76
|
|
|
if (($key = array_search(19, $validServers)) !== false) { |
77
|
|
|
unset($validServers[$key]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (! empty($server) && in_array($server, $validServers)) { |
81
|
|
|
return $server; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
throw new InvalidArgumentException("Server '{$server}' is not supported."); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Parses limit, making sure it's numerical and valid |
89
|
|
|
* |
90
|
|
|
* @return boolean |
91
|
|
|
*/ |
92
|
|
|
public function parseLimit($limit) |
93
|
|
|
{ |
94
|
|
|
if (! isset($limit) && ! is_numeric($limit)) { |
95
|
|
|
throw new InvalidArgumentException("Limit needs to be in numerical format."); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $limit; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Parses offset, making sure it's numerical and valid |
103
|
|
|
*/ |
104
|
|
|
public function parseOffset($offset) |
105
|
|
|
{ |
106
|
|
|
if (! isset($offset) && ! is_numeric($offset)) { |
107
|
|
|
throw new InvalidArgumentException("Offset needs to be in numerical format."); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return $offset; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: