Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
<exclude-pattern>*/vendor/*</exclude-pattern>
<arg name="colors" />
<arg value="p" />
<rule ref="PSR12" />
<rule ref="PSR12" >
<exclude name="PSR12.Properties.ConstantVisibility.NotFound" />
</rule>
</ruleset>
33 changes: 23 additions & 10 deletions src/Databases/AbstractNetAcuityDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*/
Expand All @@ -23,6 +29,11 @@ abstract class AbstractNetAcuityDatabase implements NetAcuityDatabaseInterface
*/
protected $translations;

/**
* @var string
*/
protected $netacuityBaseUrl;

/**
* @var string The API User Token.
*/
Expand All @@ -36,23 +47,26 @@ 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
) {
$this->client = $client;
$this->apiUserToken = $apiUserToken;
$this->netacuityBaseUrl = $netacuityBaseUrl;
}

/**
* @param string $ip The IP to fetch the result data set for.
*
* @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)
{
Expand All @@ -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
*/
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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";
}
}
14 changes: 9 additions & 5 deletions src/Databases/EdgeDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
14 changes: 11 additions & 3 deletions src/Databases/PulseDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
9 changes: 9 additions & 0 deletions src/Exceptions/NetacuityException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace TraderInteractive\NetAcuity\Exceptions;

use Exception;

class NetacuityException extends Exception
{
}
3 changes: 3 additions & 0 deletions tests/Databases/AbstractNetAcuityDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace TraderInteractive\NetAcuity\Databases\Tests;

use TraderInteractive\NetAcuity\Databases\EdgeDatabase;
use TraderInteractive\NetAcuity\Exceptions\NetacuityException;
use TraderInteractive\NetAcuity\Tests\NetAcuityTestSuite;
use Exception;

Expand Down Expand Up @@ -154,6 +155,7 @@ public function getGeoWithExtraFieldEdge()
*/
public function getGeoNonStringIp()
{
$this->expectException(NetacuityException::class);
$this->expectExceptionMessage('NetAcuity API rejected the request, Reason: Invalid IP (1)');
$this->expectExceptionCode(400);

Expand All @@ -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);

Expand Down