Skip to content

Commit 21d51c9

Browse files
authored
Merge pull request #299 from bigcommerce/fix_client_gettime
Fix Client::getTime()
2 parents 382d1d4 + caea7b0 commit 21d51c9

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ https://developer.bigcommerce.com/
1212
Requirements
1313
------------
1414

15-
- PHP 7.1 or greater
15+
- PHP 8.1 or greater
1616
- `curl` extension enabled
1717

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

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

103103
~~~php
104-
$ping = Bigcommerce::getTime();
104+
$ping = Bigcommerce::getStoreTime();
105105

106106
if ($ping) echo $ping->format('H:i:s');
107107
~~~

src/Bigcommerce/Api/Client.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -474,19 +474,38 @@ public static function getCustomerLoginToken($id, $redirectUrl = '', $requestIp
474474
}
475475

476476
/**
477-
* Pings the time endpoint to test the connection to a store.
477+
* Pings the time endpoint to test the connection to the BigCommerce API.
478478
*
479479
* @return ?DateTime
480480
*/
481481
public static function getTime()
482482
{
483483
$response = self::connection()->get(self::$api_url . '/time');
484484

485-
if (empty($response)) {
485+
if (empty($response) || !is_numeric($response)) {
486+
return null;
487+
}
488+
489+
// The response from /time is unix time in milliseconds
490+
$seconds = floor($response / 1000);
491+
$milliseconds = $response % 1000;
492+
return DateTime::createFromFormat('U.u', sprintf('%d.%03d', $seconds, $milliseconds));
493+
}
494+
495+
/**
496+
* Pings the time endpoint to test the connection to a store.
497+
*
498+
* @return ?DateTime
499+
*/
500+
public static function getStoreTime()
501+
{
502+
$response = self::connection()->get(self::$api_path . '/time');
503+
504+
if (!is_object($response) || !property_exists($response, 'time')) {
486505
return null;
487506
}
488507

489-
return new DateTime("@{$response}");
508+
return new DateTime("@{$response->time}");
490509
}
491510

492511
/**

test/Unit/Api/ClientTest.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,13 +206,32 @@ public function testDeleteResourceDeletesToTheRightPlace()
206206

207207
public function testGetTimeReturnsTheExpectedTime()
208208
{
209-
$now = new \DateTime();
210209
$this->connection->expects($this->once())
211210
->method('get')
212211
->with('https://api.bigcommerce.com/time', false)
213-
->will($this->returnValue($now->format('U')));
212+
->will($this->returnValue('1718283600000'));
214213

215-
$this->assertEquals($now->format('U'), Client::getTime()->format('U'));
214+
$this->assertEquals('2024-06-13 13:00:00', Client::getTime()->format('Y-m-d H:i:s'));
215+
}
216+
217+
public function testGetStoreTimeReturnsTheExpectedTime()
218+
{
219+
$this->connection->expects($this->once())
220+
->method('get')
221+
->with($this->basePath . '/time')
222+
->will($this->returnValue(json_decode('{"time": 1718283600}')));
223+
224+
$this->assertEquals('2024-06-13 13:00:00', Client::getStoreTime()->format('Y-m-d H:i:s'));
225+
}
226+
227+
public function testGetStoreTimeReturnsNothing()
228+
{
229+
$this->connection->expects($this->once())
230+
->method('get')
231+
->with($this->basePath . '/time')
232+
->will($this->returnValue(false));
233+
234+
$this->assertEquals(null, Client::getStoreTime());
216235
}
217236

218237
public function testGetStoreReturnsTheResultBodyDirectly()

0 commit comments

Comments
 (0)