diff --git a/phpcs.xml b/phpcs.xml index a72f6d1..48c9d6d 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -4,5 +4,7 @@ */vendor/* - + + + diff --git a/src/Databases/AbstractNetAcuityDatabase.php b/src/Databases/AbstractNetAcuityDatabase.php index 6f05cff..8ab5e5d 100644 --- a/src/Databases/AbstractNetAcuityDatabase.php +++ b/src/Databases/AbstractNetAcuityDatabase.php @@ -2,8 +2,8 @@ namespace TraderInteractive\NetAcuity\Databases; +use TraderInteractive\NetAcuity\Exceptions\NetacuityException; use TraderInteractive\Util\Arrays; -use Exception; use GuzzleHttp\ClientInterface; use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Psr7\Request; @@ -13,6 +13,12 @@ */ abstract class AbstractNetAcuityDatabase implements NetAcuityDatabaseInterface { + /** + * @var string + * @internal + */ + const DEFAULT_NETACUITY_BASE_URL = 'https://usa.cloud.netacuity.com/webservice/query'; + /** * @var ClientInterface The GuzzleHttp Client. */ @@ -23,6 +29,11 @@ abstract class AbstractNetAcuityDatabase implements NetAcuityDatabaseInterface */ protected $translations; + /** + * @var string + */ + protected $netacuityBaseUrl; + /** * @var string The API User Token. */ @@ -36,15 +47,18 @@ abstract class AbstractNetAcuityDatabase implements NetAcuityDatabaseInterface /** * AbstractNetAcuityDatabase constructor. * - * @param ClientInterface $client The injected GuzzleHttp Client. - * @param string $apiUserToken The Net Acuity API User Token. + * @param ClientInterface $client The injected GuzzleHttp Client. + * @param string $apiUserToken The Net Acuity API User Token. + * @param string $netacuityBaseUrl The base url for the netacuity webservice. */ public function __construct( ClientInterface $client, - string $apiUserToken + string $apiUserToken, + string $netacuityBaseUrl = self::DEFAULT_NETACUITY_BASE_URL ) { $this->client = $client; $this->apiUserToken = $apiUserToken; + $this->netacuityBaseUrl = $netacuityBaseUrl; } /** @@ -52,7 +66,7 @@ public function __construct( * * @return array The formatted data set. * - * @throws Exception On failure to send a Guzzle request. + * @throws NetacuityException On failure to send a Guzzle request. */ public function fetch(string $ip) { @@ -73,7 +87,7 @@ public function fetch(string $ip) /** * @param ClientException $e The thrown exception for handling. * - * @throws Exception A formatted exception masking the API User Token in the event that it becomes invalid. + * @throws NetacuityException A formatted exception masking the API User Token in the event that it becomes invalid. * * @return void */ @@ -83,13 +97,13 @@ protected function handleGuzzleException(ClientException $e) $code = $response->getStatusCode(); if ($code === 403) { - throw new Exception('NetAcuity API rejected the provided api user token.', $code); + throw new NetacuityException('NetAcuity API rejected the provided api user token.', $code); } $error = json_decode($response->getBody()->getContents(), true); $reason = Arrays::getNested($error, 'error.message'); - throw new Exception("NetAcuity API rejected the request, Reason: {$reason}", $code); + throw new NetacuityException("NetAcuity API rejected the request, Reason: {$reason}", $code); } /** @@ -117,7 +131,6 @@ protected function parseBody(array $response) */ protected function buildQuery(string $userToken, string $ip): string { - $baseUrl = 'https://usa.cloud.netacuity.com/webservice/query'; - return "{$baseUrl}?u={$userToken}&dbs={$this->databaseIdentifier}&ip={$ip}&json=true"; + return "{$this->netacuityBaseUrl}?u={$userToken}&dbs={$this->databaseIdentifier}&ip={$ip}&json=true"; } } diff --git a/src/Databases/EdgeDatabase.php b/src/Databases/EdgeDatabase.php index 2360d15..14d6935 100644 --- a/src/Databases/EdgeDatabase.php +++ b/src/Databases/EdgeDatabase.php @@ -12,12 +12,16 @@ final class EdgeDatabase extends AbstractNetAcuityDatabase /** * EdgeDatabase constructor. * - * @param ClientInterface $client The injected GuzzleHttp Client. - * @param string $apiUserToken The Net Acuity API User Token. + * @param ClientInterface $client The injected GuzzleHttp Client. + * @param string $apiUserToken The Net Acuity API User Token. + * @param string $netacuityBaseUrl The base url for the netacuity webservice. */ - public function __construct(ClientInterface $client, string $apiUserToken) - { - parent::__construct($client, $apiUserToken); + public function __construct( + ClientInterface $client, + string $apiUserToken, + string $netacuityBaseUrl = self::DEFAULT_NETACUITY_BASE_URL + ) { + parent::__construct($client, $apiUserToken, $netacuityBaseUrl); $this->databaseIdentifier = 4; diff --git a/src/Databases/PulseDatabase.php b/src/Databases/PulseDatabase.php index 8402b64..ce00c17 100644 --- a/src/Databases/PulseDatabase.php +++ b/src/Databases/PulseDatabase.php @@ -6,9 +6,17 @@ class PulseDatabase extends AbstractNetAcuityDatabase { - public function __construct(ClientInterface $client, string $apiUserToken) - { - parent::__construct($client, $apiUserToken); + /** + * @param ClientInterface $client The injected GuzzleHttp Client. + * @param string $apiUserToken The Net Acuity API User Token. + * @param string $netacuityBaseUrl The base url for the netacuity webservice. + */ + public function __construct( + ClientInterface $client, + string $apiUserToken, + string $netacuityBaseUrl = self::DEFAULT_NETACUITY_BASE_URL + ) { + parent::__construct($client, $apiUserToken, $netacuityBaseUrl); $this->databaseIdentifier = 26; diff --git a/src/Exceptions/NetacuityException.php b/src/Exceptions/NetacuityException.php new file mode 100644 index 0000000..973de81 --- /dev/null +++ b/src/Exceptions/NetacuityException.php @@ -0,0 +1,9 @@ +expectException(NetacuityException::class); $this->expectExceptionMessage('NetAcuity API rejected the request, Reason: Invalid IP (1)'); $this->expectExceptionCode(400); @@ -171,6 +173,7 @@ public function getGeoNonStringIp() */ public function netAcuityUserTokenInvalid() { + $this->expectException(NetacuityException::class); $this->expectExceptionMessage('NetAcuity API rejected the provided api user token.'); $this->expectExceptionCode(403);