* @copyright 2007-2020 PrestaShop SA * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA */ namespace PrestaShop\Module\PsEventbus\Api; use GuzzleHttp\Message\ResponseInterface; /** * Handle api response. */ class ResponseApiHandler { /** * Format api 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) { // Directly return true, no need to check the body for a 204 status code // 204 status code is only send by /payments/order/update if (204 === $httpStatusCode) { return true; } return '2' === substr((string) $httpStatusCode, 0, 1) && null !== $responseContents; } }