Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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: 2 additions & 2 deletions config/ipstack.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
'base_uri' => env('IPSTACK_BASE_URI', 'https://api.ipstack.com/'),

'testing' => [
'default_ip' => env('IPSTACK_DEFAULT_TESTING_IP', '134.201.250.155')
]
'default_ip' => env('IPSTACK_DEFAULT_TESTING_IP', '134.201.250.155'),
],
];
26 changes: 15 additions & 11 deletions src/IPStack.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ public function __construct()
{
$this->client = new Client([
'base_uri' => config('ipstack.base_uri'),
'errors' => false
'errors' => false,
]);
}

/**
* @param array $ips
* @param array $ips
* @return Collection
*
* @throws GuzzleException|IPStackAPIException|InvalidIPAddressFormatException
*/
public function getBulk(array $ips): Collection
Expand All @@ -42,16 +43,18 @@ public function getBulk(array $ips): Collection
$IPStackResponses = collect();

if ($this->responseWasSuccessful($responseBody)) {
array_map(fn($result) => $IPStackResponses->push(new IPStackLookup($result)), $responseBody);
array_map(fn ($result) => $IPStackResponses->push(new IPStackLookup($result)), $responseBody);

return $IPStackResponses;
}

$this->handleUnsuccessfulResponseStatusCode($responseBody);
}

/**
* @param string|array $ips
* @param string|array $ips
* @return void
*
* @throws InvalidIPAddressFormatException
*/
private function validateIpAddress(string|array $ips): void
Expand All @@ -65,15 +68,16 @@ private function validateIpAddress(string|array $ips): void
$this->validateIpAddress($ip);
}
} else {
if (!preg_match($ipv4Pattern, $ips) && !preg_match($ipv6Pattern, $ips)) {
if (! preg_match($ipv4Pattern, $ips) && ! preg_match($ipv6Pattern, $ips)) {
throw new InvalidIPAddressFormatException($ips);
}
}
}

/**
* @param string $ip
* @param string $ip
* @return IPStackLookup
*
* @throws GuzzleException|IPStackAPIException|InvalidIPAddressFormatException
* @throws IPStackHydrationException
*/
Expand All @@ -86,7 +90,6 @@ public function get(string $ip): IPStackLookup
$responseBody = json_decode($response->getBody()->getContents(), true);

if ($this->responseWasSuccessful($responseBody)) {

return new IPStackLookup($responseBody);
}

Expand All @@ -97,21 +100,22 @@ public function get(string $ip): IPStackLookup
* IPStack responses always return a HTTP200 response. Instead, the response code is included as part of the payload
* if the request was unsuccessful. This method checks if the response was successful, and if not, throws an
* appropriate exception.
* https://ipstack.com/documentation#errors
* https://ipstack.com/documentation#errors.
*
* @param array $responseBody
* @param array $responseBody
* @return bool
*/
private function responseWasSuccessful(array $responseBody): bool
{
// Check if the request was successful.
// Strangely, the 'success' key only exists if the request was unsuccessful.
return !array_key_exists('success', $responseBody);
return ! array_key_exists('success', $responseBody);
}

/**
* @param array $responseBody
* @param array $responseBody
* @return void
*
* @throws IPStackAPIException
*/
private function handleUnsuccessfulResponseStatusCode(array $responseBody): void
Expand Down
2 changes: 1 addition & 1 deletion src/Objects/IPStackLocation.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __construct(array $data)
{
$this->geonameId = $data['geoname_id'];
$this->capital = $data['capital'];
$this->languages = array_map(fn($lang) => new IPStackLanguage($lang), $data['languages']);
$this->languages = array_map(fn ($lang) => new IPStackLanguage($lang), $data['languages']);
$this->countryFlag = $data['country_flag'];
$this->countryFlagEmoji = $data['country_flag_emoji'];
$this->countryFlagEmojiUnicode = $data['country_flag_emoji_unicode'];
Expand Down
1 change: 0 additions & 1 deletion src/Objects/IPStackLookup.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ public function __construct(array $data)
$this->connectionType = $data['connection_type'] ?? null;

$this->location = new IPStackLocation($data['location']);

} catch (\Throwable $e) {
throw new IPStackHydrationException();
}
Expand Down
6 changes: 3 additions & 3 deletions tests/IPStackTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ protected function setUp(): void
}

/**
* @param Application $app
* @param Application $app
* @return class-string[]
*/
protected function getPackageProviders($app): array
{
return [
\Arimolzer\IPStack\IPStackServiceProvider::class
\Arimolzer\IPStack\IPStackServiceProvider::class,
];
}

/**
* @param Application $app
* @param Application $app
* @return class-string[]
*/
protected function getPackageAliases($app): array
Expand Down
1 change: 0 additions & 1 deletion tests/Unit/APIHydrationTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php


class APIHydrationTest extends \Arimolzer\IPStack\Tests\IPStackTestCase
{
public function test_successfully_hydrates_a_single_ipstack_response()
Expand Down
11 changes: 5 additions & 6 deletions tests/Unit/StubHydrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public function test_successfully_hydrates_single_stub()
// Read the contents of stubs/StandardLookupResponse.json and parse it into an array.
// Create a new IPStackLookup object using the array.
$lookup = new IPStackLookup(
json_decode(file_get_contents(__DIR__ . '/../stubs/StandardLookupResponse.json'), true)
json_decode(file_get_contents(__DIR__.'/../stubs/StandardLookupResponse.json'), true)
);

$this->validateHydratedLookup($lookup, 'US', 'CA', 'en');
Expand All @@ -18,10 +18,10 @@ public function test_successfully_hydrates_single_stub()
public function test_successfully_hydrates_bulk_stub()
{
// Read the contents of stubs/BulkLookupResponse.json and parse it into an array.
$data = json_decode(file_get_contents(__DIR__ . '/../stubs/BulkLookupResponse.json'), true);
$data = json_decode(file_get_contents(__DIR__.'/../stubs/BulkLookupResponse.json'), true);

// Create a collection of IPStackLookup objects using the array data
$lookups = collect(array_map(fn($lookup) => new IPStackLookup($lookup), $data));
$lookups = collect(array_map(fn ($lookup) => new IPStackLookup($lookup), $data));

$this->assertCount(3, $lookups);

Expand All @@ -35,7 +35,7 @@ public function test_hydration_from_bad_stub()
$this->expectException(\Arimolzer\IPStack\Exceptions\IPStackHydrationException::class);

// Read the contents of stubs/BadStandardLookupResponse.json and parse it into an array.
$data = json_decode(file_get_contents(__DIR__ . '/../stubs/BadStandardLookupResponse.json'), true);
$data = json_decode(file_get_contents(__DIR__.'/../stubs/BadStandardLookupResponse.json'), true);

// Create a new IPStackLookup object using the array data
new IPStackLookup($data);
Expand All @@ -46,8 +46,7 @@ private function validateHydratedLookup(
string $countryCode,
string $regionCode,
string $languageCode
): void
{
): void {
$this->assertEquals($countryCode, $lookup->getCountryCode());
$this->assertEquals($regionCode, $lookup->getRegionCode());
$this->assertEquals($languageCode, $lookup->getLocation()->getLanguages()[0]->getCode());
Expand Down
1 change: 0 additions & 1 deletion tests/Unit/ValidateConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php


class ValidateConfigurationTest extends \Arimolzer\IPStack\Tests\IPStackTestCase
{
public function test_environmental_variables_are_loaded()
Expand Down
1 change: 0 additions & 1 deletion tests/Unit/ValidateInvalidIPAddressTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?php


class ValidateInvalidIPAddressTest extends \Arimolzer\IPStack\Tests\IPStackTestCase
{
public function test_invalid_ip_address_exception()
Expand Down
4 changes: 2 additions & 2 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

use Dotenv\Dotenv;

require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__.'/../vendor/autoload.php';

// Load the .env file
$dotenv = Dotenv::createImmutable(__DIR__ . '/../');
$dotenv = Dotenv::createImmutable(__DIR__.'/../');
$dotenv->load();

// Require the API key
Expand Down