You are here

protected function FastlyBackend::fastlyResponseData in Acquia Purge 8

Decode JSON from a Fastly response object body.

Parameters

\Psr\Http\Message\ResponseInterface $response: The HTTP response object.

Return value

array JSON decoded response data from the Fastly API.

Throws

\RuntimeException Thrown when common runtime issues are detected, for invalid credentials for instance. Calls to setTemporaryRuntimeError() will be made as well.

3 calls to FastlyBackend::fastlyResponseData()
FastlyBackend::invalidateEverything in src/AcquiaPlatformCdn/FastlyBackend.php
Invalidate all 'everything' invalidations.
FastlyBackend::invalidateTags in src/AcquiaPlatformCdn/FastlyBackend.php
Invalidate all 'tag' invalidations.
FastlyBackend::invalidateUrls in src/AcquiaPlatformCdn/FastlyBackend.php
Invalidate all 'url' invalidations.

File

src/AcquiaPlatformCdn/FastlyBackend.php, line 285

Class

FastlyBackend
Provides a Fastly backend for the Platform CDN purger.

Namespace

Drupal\acquia_purge\AcquiaPlatformCdn

Code

protected function fastlyResponseData(ResponseInterface $response) {
  if ($data = json_decode($response
    ->getBody(), TRUE)) {

    // Detect invalid credentials and suspend operations for a full day,
    // we do this to prevent flooding Fastly in unattended environments.
    if (isset($data['msg']) && strpos($data['msg'], 'credentials') !== FALSE) {
      $message = "Invalid credentials - please contact Acquia Support and";
      $message .= " clear the cache after the issue got resolved.";
      self::setTemporaryRuntimeError($message, 86400);
      throw new \RuntimeException($message);
    }

    // Detect invalid environments and suspend operations for 12 hours.
    if (isset($data['msg'], $data['detail']) && $data['msg'] == 'Record not found') {
      if ($data['detail'] == 'Cannot find service') {
        $message = "Invalid environment - please contact Acquia Support and";
        $message .= " clear the cache after the issue got resolved.";
        self::setTemporaryRuntimeError($message, 43200);
        throw new \RuntimeException($message);
      }
    }
    return $data;
  }
  return [];
}