Elgg\Ajax\Service::buildHttpResponse PHP Method

buildHttpResponse() private method

Build a JsonResponse based on an API response object
private buildHttpResponse ( Elgg\Services\AjaxResponse $api_response, boolean $allow_removing_headers = null ) : Symfony\Component\HttpFoundation\JsonResponse
$api_response Elgg\Services\AjaxResponse The API Response
$allow_removing_headers boolean Alter PHP's global headers to allow caching
return Symfony\Component\HttpFoundation\JsonResponse
    private function buildHttpResponse(AjaxResponse $api_response, $allow_removing_headers = null)
    {
        if ($api_response->isCancelled()) {
            return new JsonResponse(['error' => "The response was cancelled"], 400);
        }
        $response = new JsonResponse($api_response->getData());
        $ttl = $api_response->getTtl();
        if ($ttl > 0) {
            // Required to remove headers set by PHP session
            if (!isset($allow_removing_headers)) {
                $allow_removing_headers = !elgg()->isTestingApplication();
            }
            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;
    }