Skip to content

Commit 0b48042

Browse files
committed
Add Client::getStoreTime() method, update README
1 parent c1776ff commit 0b48042

3 files changed

Lines changed: 42 additions & 6 deletions

File tree

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: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -482,15 +482,32 @@ 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)) {
486486
return null;
487487
}
488488

489+
// The response from /time is unix time in milliseconds
489490
$seconds = floor($response / 1000);
490491
$microseconds = $response % 1000;
491492
return DateTime::createFromFormat('U.u', sprintf('%d.%03d', $seconds, $microseconds));
492493
}
493494

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')) {
505+
return null;
506+
}
507+
508+
return new DateTime("@{$response->time}");
509+
}
510+
494511
/**
495512
* Returns the default collection of products.
496513
*

test/Unit/Api/ClientTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ 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)
@@ -215,6 +214,26 @@ public function testGetTimeReturnsTheExpectedTime()
215214
$this->assertEquals('2024-06-13 13:00:00', Client::getTime()->format('Y-m-d H:i:s'));
216215
}
217216

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());
235+
}
236+
218237
public function testGetStoreReturnsTheResultBodyDirectly()
219238
{
220239
$body = [random_int(0, mt_getrandmax()) => random_int(0, mt_getrandmax())];

0 commit comments

Comments
 (0)