|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DominionEnterprises\NetAcuity\Databases; |
|
4
|
|
|
|
|
5
|
|
|
use DominionEnterprises\Util\Arrays; |
|
6
|
|
|
use Exception; |
|
7
|
|
|
use GuzzleHttp\Client; |
|
8
|
|
|
use GuzzleHttp\ClientInterface; |
|
9
|
|
|
use GuzzleHttp\Exception\ClientException; |
|
10
|
|
|
use GuzzleHttp\Psr7\Request; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* The Abstract NetAcuityDatabase used as a base for each database model. |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class AbstractNetAcuityDatabase implements NetAcuityDatabaseInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var Client The GuzzleHttp Client. |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $_client; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array The translations array for the data set. |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $_translations; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string The API User Token. |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $_apiUserToken; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var int The Net Acuity Database ID. |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $_databaseIdentifier; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* AbstractNetAcuityDatabase constructor. |
|
39
|
|
|
* |
|
40
|
|
|
* @param ClientInterface $client The injected GuzzleHttp Client. |
|
41
|
|
|
* @param string $apiUserToken The Net Acuity API User Token. |
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct( |
|
44
|
|
|
ClientInterface $client, |
|
45
|
|
|
string $apiUserToken |
|
46
|
|
|
) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->_client = $client; |
|
|
|
|
|
|
49
|
|
|
$this->_apiUserToken = $apiUserToken; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param string $ip The IP to fetch the result data set for. |
|
54
|
|
|
* |
|
55
|
|
|
* @return array The formatted data set. |
|
56
|
|
|
* |
|
57
|
|
|
* @throws Exception On failure to send a Guzzle request. |
|
58
|
|
|
*/ |
|
59
|
|
|
public function fetch(string $ip) |
|
60
|
|
|
{ |
|
61
|
|
|
$queryString = $this->_buildQuery($this->_apiUserToken, $ip); |
|
62
|
|
|
$request = new Request('GET', $queryString); |
|
63
|
|
|
|
|
64
|
|
|
$body = []; |
|
65
|
|
|
try { |
|
66
|
|
|
$response = $this->_client->send($request); |
|
67
|
|
|
$body = json_decode($response->getBody()->getContents(), true); |
|
68
|
|
|
} catch (ClientException $e) { |
|
69
|
|
|
$this->_handleGuzzleException($e); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $this->_parseBody($body); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param ClientException $e The thrown exception for handling. |
|
77
|
|
|
* |
|
78
|
|
|
* @throws Exception A formatted exception masking the API User Token in the event that it becomes invalid. |
|
79
|
|
|
* |
|
80
|
|
|
* @return void |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function _handleGuzzleException(ClientException $e) |
|
83
|
|
|
{ |
|
84
|
|
|
$response = $e->getResponse(); |
|
85
|
|
|
$code = $response->getStatusCode(); |
|
86
|
|
|
|
|
87
|
|
|
if ($code === 403) { |
|
88
|
|
|
throw new Exception('NetAcuity API rejected the provided api user token.', $code); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
$error = json_decode($response->getBody()->getContents(), true); |
|
92
|
|
|
$reason = Arrays::getNested($error, 'error.message'); |
|
93
|
|
|
|
|
94
|
|
|
throw new Exception("NetAcuity API rejected the request, Reason: {$reason}", $code); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Parses the response into an array using the field definition. |
|
99
|
|
|
* |
|
100
|
|
|
* @param array $response The response from NetAcuity. |
|
101
|
|
|
* |
|
102
|
|
|
* @return array The response where the keys are from $fields and the values are from the $response. |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function _parseBody(array $response) |
|
105
|
|
|
{ |
|
106
|
|
|
$responseData = Arrays::get($response, 'response'); |
|
107
|
|
|
|
|
108
|
|
|
$result = []; |
|
109
|
|
|
Arrays::copyIfKeysExist($responseData, $result, $this->_translations); |
|
110
|
|
|
|
|
111
|
|
|
return $result; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param string $userToken Net Acuity API User Token. |
|
116
|
|
|
* @param string $ip The IP to be referenced in the lookup. |
|
117
|
|
|
* |
|
118
|
|
|
* @return string The formatted url query string. |
|
119
|
|
|
*/ |
|
120
|
|
|
protected function _buildQuery(string $userToken, string $ip) : string |
|
121
|
|
|
{ |
|
122
|
|
|
$baseUrl = 'https://usa.cloud.netacuity.com/webservice/query'; |
|
123
|
|
|
return "{$baseUrl}?u={$userToken}&dbs={$this->_databaseIdentifier}&ip={$ip}&json=true"; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.