/**
* Build a JsonResponse based on an API response object
*
* @param AjaxResponse $api_response The API Response
* @param bool $allow_removing_headers Alter PHP's global headers to allow caching
*
* @return JsonResponse
* @throws \RuntimeException
*/
private function buildHttpResponse(AjaxResponse $api_response, $allow_removing_headers = true)
{
if ($api_response->isCancelled()) {
return new JsonResponse(['error' => "The response was cancelled"], 400);
}
$response = new JsonResponse(['msgs' => (object) $this->msgs->dumpRegister(), 'data' => $api_response->getData()]);
$ttl = $api_response->getTtl();
if ($ttl > 0) {
// Required to remove headers set by PHP session
if ($allow_removing_headers) {
header_remove('Expires');
header_remove('Pragma');
header_remove('Cache-Control');
}
// JsonRequest sets a default Cache-Control header we don't want
$response->headers->remove('Cache-Control');
$response->setClientTtl($ttl);
// if we don't set Expires, Apache will add a far-off max-age and Expires for us.
$response->headers->set('Expires', gmdate('D, d M Y H:i:s \\G\\M\\T', time() + $ttl));
}
return $response;
}