Skip to content

Commit

Permalink
Merge pull request #299 from bigcommerce/fix_client_gettime
Browse files Browse the repository at this point in the history
Fix Client::getTime()
  • Loading branch information
TomA-R committed Jun 28, 2024
2 parents 382d1d4 + caea7b0 commit 21d51c9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ https://developer.bigcommerce.com/
Requirements
------------

- PHP 7.1 or greater
- PHP 8.1 or greater
- `curl` extension enabled

To generate an OAuth API token, [follow this guide.](https://support.bigcommerce.com/s/article/Store-API-Accounts)
Expand Down Expand Up @@ -96,12 +96,12 @@ Connecting to the store
-----------------------

To test that your configuration was correct and you can successfully connect to
the store, ping the getTime method which will return a DateTime object
representing the current timestamp of the store if successful or false if
the store, ping the getStoreTime method which will return a DateTime object
representing the current timestamp of the store if successful or null if
unsuccessful:

~~~php
$ping = Bigcommerce::getTime();
$ping = Bigcommerce::getStoreTime();

if ($ping) echo $ping->format('H:i:s');
~~~
Expand Down
25 changes: 22 additions & 3 deletions src/Bigcommerce/Api/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,19 +474,38 @@ public static function getCustomerLoginToken($id, $redirectUrl = '', $requestIp
}

/**
* Pings the time endpoint to test the connection to a store.
* Pings the time endpoint to test the connection to the BigCommerce API.
*
* @return ?DateTime
*/
public static function getTime()
{
$response = self::connection()->get(self::$api_url . '/time');

if (empty($response)) {
if (empty($response) || !is_numeric($response)) {
return null;
}

// The response from /time is unix time in milliseconds
$seconds = floor($response / 1000);
$milliseconds = $response % 1000;
return DateTime::createFromFormat('U.u', sprintf('%d.%03d', $seconds, $milliseconds));
}

/**
* Pings the time endpoint to test the connection to a store.
*
* @return ?DateTime
*/
public static function getStoreTime()
{
$response = self::connection()->get(self::$api_path . '/time');

if (!is_object($response) || !property_exists($response, 'time')) {
return null;
}

return new DateTime("@{$response}");
return new DateTime("@{$response->time}");
}

/**
Expand Down
25 changes: 22 additions & 3 deletions test/Unit/Api/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,32 @@ public function testDeleteResourceDeletesToTheRightPlace()

public function testGetTimeReturnsTheExpectedTime()
{
$now = new \DateTime();
$this->connection->expects($this->once())
->method('get')
->with('https://api.bigcommerce.com/time', false)
->will($this->returnValue($now->format('U')));
->will($this->returnValue('1718283600000'));

$this->assertEquals($now->format('U'), Client::getTime()->format('U'));
$this->assertEquals('2024-06-13 13:00:00', Client::getTime()->format('Y-m-d H:i:s'));
}

public function testGetStoreTimeReturnsTheExpectedTime()
{
$this->connection->expects($this->once())
->method('get')
->with($this->basePath . '/time')
->will($this->returnValue(json_decode('{"time": 1718283600}')));

$this->assertEquals('2024-06-13 13:00:00', Client::getStoreTime()->format('Y-m-d H:i:s'));
}

public function testGetStoreTimeReturnsNothing()
{
$this->connection->expects($this->once())
->method('get')
->with($this->basePath . '/time')
->will($this->returnValue(false));

$this->assertEquals(null, Client::getStoreTime());
}

public function testGetStoreReturnsTheResultBodyDirectly()
Expand Down

0 comments on commit 21d51c9

Please sign in to comment.