diff --git a/README.md b/README.md index 46312ff..d53ff42 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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'); ~~~ diff --git a/src/Bigcommerce/Api/Client.php b/src/Bigcommerce/Api/Client.php index 1cd61e9..e64100c 100644 --- a/src/Bigcommerce/Api/Client.php +++ b/src/Bigcommerce/Api/Client.php @@ -474,7 +474,7 @@ 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 */ @@ -482,11 +482,30 @@ 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}"); } /** diff --git a/test/Unit/Api/ClientTest.php b/test/Unit/Api/ClientTest.php index 39354f4..9fb9269 100644 --- a/test/Unit/Api/ClientTest.php +++ b/test/Unit/Api/ClientTest.php @@ -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()