* @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 */ namespace PrestaShop\Module\PsAccounts\Handler\Response; use GuzzleHttp\Message\ResponseInterface; /** * Handle api response. */ class ApiResponseHandler { /** * Format api response. * * @param ResponseInterface $response * * @return array */ public function handleResponse(ResponseInterface $response) { $responseContents = json_decode($response->getBody()->getContents(), true); return [ 'status' => $this->responseIsSuccessful($responseContents, $response->getStatusCode()), 'httpCode' => $response->getStatusCode(), 'body' => $responseContents, ]; } /** * Check if the response is successful or not (response code 200 to 299). * * @param array $responseContents * @param int $httpStatusCode * * @return bool */ private function responseIsSuccessful($responseContents, $httpStatusCode) { return '2' === substr((string) $httpStatusCode, 0, 1); } }