|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace StarCitizen; |
|
4
|
|
|
|
|
5
|
|
|
use StarCitizen\Models\Model; |
|
6
|
|
|
use StarCitizen\Client\StarCitizensClient; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class StarCitizens |
|
10
|
|
|
* @package StarCitizen |
|
11
|
|
|
* |
|
12
|
|
|
* @method accounts($id, $profileType = false, $cache = false, $raw = false) Get accounts system results |
|
13
|
|
|
* @method organizations($id, $profileType = false, $cache = false, $raw = false) Get org system results |
|
14
|
|
|
*/ |
|
15
|
|
|
final class StarCitizens |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var bool|StarCitizensClient The client object |
|
19
|
|
|
*/ |
|
20
|
|
|
private static $client = false; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array The config for the StarCitizens todo move this to a separate file |
|
24
|
|
|
*/ |
|
25
|
|
|
private $systems = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
public $configFile; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* StarCitizens constructor. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $configFile |
|
36
|
|
|
*/ |
|
37
|
20 |
|
public function __construct($configFile = "Resources/config/typesConfig.php") |
|
38
|
|
|
{ |
|
39
|
20 |
|
$this->configFile = $configFile; |
|
40
|
20 |
|
self::setupClient(); |
|
41
|
20 |
|
$this->readConfig(); |
|
42
|
20 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Reads the basic config. |
|
46
|
|
|
*/ |
|
47
|
20 |
|
private function readConfig() |
|
48
|
|
|
{ |
|
49
|
20 |
|
$this->systems = include $this->configFile; |
|
50
|
20 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Find an entity based on id and return the correct model or raw json output |
|
54
|
|
|
* |
|
55
|
|
|
* @param $id |
|
56
|
|
|
* @param $system |
|
57
|
|
|
* @param $profileType |
|
58
|
|
|
* @param bool $raw |
|
59
|
|
|
* @param bool $cache |
|
60
|
|
|
* @return bool|mixed |
|
61
|
|
|
*/ |
|
62
|
19 |
|
private function get($id, $system, $profileType, $raw = false, $cache = false) |
|
63
|
|
|
{ |
|
64
|
19 |
|
$response = json_decode( |
|
65
|
|
|
self::$client |
|
66
|
19 |
|
->getResult( |
|
67
|
19 |
|
$this->getCallParams($id, $system, $profileType, $cache) |
|
68
|
19 |
|
) |
|
69
|
19 |
|
->getBody() |
|
70
|
19 |
|
->getContents(), |
|
71
|
|
|
true |
|
72
|
19 |
|
); |
|
73
|
|
|
|
|
74
|
19 |
|
return $this->checkResponse($response, $this->systems[$system]['actions'][$profileType], $raw); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Returns the params needed for an API call |
|
79
|
|
|
* |
|
80
|
|
|
* @param $id |
|
81
|
|
|
* @param $system |
|
82
|
|
|
* @param $profileType |
|
83
|
|
|
* @param $cache |
|
84
|
|
|
* @return array |
|
85
|
|
|
*/ |
|
86
|
19 |
|
private function getCallParams($id, $system, $profileType, $cache) |
|
87
|
|
|
{ |
|
88
|
19 |
|
$cache = ($cache === true) ? "cache" : "live"; |
|
89
|
|
|
|
|
90
|
|
|
return [ |
|
91
|
19 |
|
'api_source' => $cache, |
|
92
|
19 |
|
'system' => $system, |
|
93
|
19 |
|
'action' => $profileType, |
|
94
|
19 |
|
'target_id' => $id, |
|
95
|
19 |
|
'expedite' => '0', |
|
96
|
19 |
|
'format' => 'json', |
|
97
|
19 |
|
'start_page' => '1', |
|
98
|
|
|
'end_page' => '5' |
|
99
|
19 |
|
]; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Checks the response for success messages and returns the raw response or model |
|
104
|
|
|
* |
|
105
|
|
|
* @param $response |
|
106
|
|
|
* @param $profileType |
|
107
|
|
|
* @param $raw |
|
108
|
|
|
* |
|
109
|
|
|
* @return bool|mixed |
|
110
|
|
|
*/ |
|
111
|
19 |
|
private function checkResponse($response, $profileType, $raw) |
|
112
|
|
|
{ |
|
113
|
19 |
|
if ($response['request_stats']['query_status'] == "success") { |
|
114
|
19 |
|
if ($raw === true) { |
|
115
|
1 |
|
return $response; |
|
116
|
|
|
} else { |
|
117
|
19 |
|
return $this->fillModel($profileType, $response['data']); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
3 |
|
return false; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Setup the client |
|
126
|
|
|
*/ |
|
127
|
20 |
|
private static function setupClient() |
|
128
|
|
|
{ |
|
129
|
20 |
|
if (self::$client === false) { |
|
130
|
1 |
|
self::$client = new StarCitizensClient(); |
|
131
|
1 |
|
} |
|
132
|
20 |
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Fills our model in with the provided data or sends the data to a store |
|
136
|
|
|
* |
|
137
|
|
|
* @param $model |
|
138
|
|
|
* @param $fillData |
|
139
|
|
|
* |
|
140
|
|
|
* @return Model |
|
141
|
|
|
*/ |
|
142
|
19 |
|
private function fillModel($model, $fillData) |
|
143
|
|
|
{ |
|
144
|
19 |
|
if (is_array($model)) { |
|
145
|
13 |
|
list($className, $dataRoot, $idName) =$model; |
|
146
|
13 |
|
$object = new \ReflectionClass('StarCitizen\Models\Store'); |
|
147
|
13 |
|
return $object->newInstance($fillData, $className, $dataRoot, $idName); |
|
148
|
|
|
} else { |
|
149
|
8 |
|
$object = new \ReflectionClass('StarCitizen\Models' . $model); |
|
150
|
3 |
|
return $object->newInstance($fillData); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Magic call function based on the config information |
|
156
|
|
|
* |
|
157
|
|
|
* @param $name |
|
158
|
|
|
* @param $arguments |
|
159
|
|
|
* @return bool|mixed |
|
160
|
|
|
* @throws \Exception |
|
161
|
|
|
*/ |
|
162
|
20 |
|
public function __call($name, $arguments) |
|
163
|
|
|
{ |
|
164
|
20 |
|
if (array_key_exists($name, $this->systems)) { |
|
165
|
19 |
|
return $this->doCall($name, $arguments); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
1 |
|
throw new \Exception("Method {$name} not found"); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* This is the real call function |
|
173
|
|
|
* |
|
174
|
|
|
* @param $system |
|
175
|
|
|
* @param array $arguments |
|
176
|
|
|
* @return bool|mixed |
|
177
|
|
|
*/ |
|
178
|
19 |
|
private function doCall($system, array $arguments = []) |
|
179
|
|
|
{ |
|
180
|
19 |
|
$id = ''; |
|
181
|
19 |
|
$cache = false; |
|
182
|
19 |
|
$raw = false; |
|
183
|
19 |
|
$action = ""; |
|
184
|
|
|
|
|
185
|
19 |
|
$fixedArguments = $this->standardGetArguments($arguments); |
|
186
|
19 |
|
extract($fixedArguments, EXTR_OVERWRITE); |
|
187
|
19 |
|
$action = ($action == "") ? $this->systems[$system]['base_action'] : $action; |
|
188
|
19 |
|
return $this->get($id, $system, $action, $raw, $cache); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Get the standard arguments for a find call and checks that we |
|
193
|
|
|
* have the correct arguments passed to the magic function |
|
194
|
|
|
* |
|
195
|
|
|
* @param array $arguments |
|
196
|
|
|
* @return array |
|
197
|
|
|
*/ |
|
198
|
19 |
|
private function standardGetArguments(array $arguments) |
|
199
|
|
|
{ |
|
200
|
|
|
|
|
201
|
|
|
$defaults = [ |
|
202
|
19 |
|
'id' => '', |
|
203
|
19 |
|
'action' => '', |
|
204
|
19 |
|
'raw' => false, |
|
205
|
19 |
|
'cache' => false, |
|
206
|
19 |
|
]; |
|
207
|
|
|
|
|
208
|
19 |
|
$varNames = array_keys($defaults); |
|
209
|
|
|
|
|
210
|
19 |
|
for ($argumentCount = 0; $argumentCount < 4; $argumentCount++) { |
|
211
|
19 |
|
if (array_key_exists($argumentCount, $arguments)) { |
|
212
|
19 |
|
$defaults[$varNames[$argumentCount]] = $arguments[$argumentCount]; |
|
213
|
19 |
|
} |
|
214
|
19 |
|
} |
|
215
|
|
|
|
|
216
|
19 |
|
return $defaults; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* Allows our functions to be called statically, this is a bit hacky tbh. |
|
221
|
|
|
* |
|
222
|
|
|
* @param $name |
|
223
|
|
|
* @param $arguments |
|
224
|
|
|
* @return bool|mixed |
|
225
|
|
|
* @throws \Exception |
|
226
|
|
|
*/ |
|
227
|
1 |
|
public static function __callStatic($name, $arguments) |
|
228
|
|
|
{ |
|
229
|
1 |
|
$starCitizens = new StarCitizens(); |
|
230
|
1 |
|
return $starCitizens->__call($name, $arguments); |
|
231
|
|
|
} |
|
232
|
|
|
} |