You are here

public function FastlyBackend::invalidateEverything in Acquia Purge 8

Invalidate all 'everything' invalidations.

Overrides BackendInterface::invalidateEverything

See also

\Drupal\purge\Plugin\Purge\Purger\invalidate

\Drupal\purge\Plugin\Purge\Purger\routeTypeToMethod

File

src/AcquiaPlatformCdn/FastlyBackend.php, line 161

Class

FastlyBackend
Provides a Fastly backend for the Platform CDN purger.

Namespace

Drupal\acquia_purge\AcquiaPlatformCdn

Code

public function invalidateEverything(array $invalidations) {

  // Set the 'everything' object(s) into processing mode.
  foreach ($invalidations as $invalidation) {
    $invalidation
      ->setState(InvalidationInterface::PROCESSING);
  }

  // Every response is tagged with the site identifier by ::tagsHeaderValue(),
  // so that we can use it here as key to purge all CDN content with.
  $key = current(Hash::cacheTags([
    $this->platformInfo
      ->getSiteIdentifier(),
  ]));

  // Execute the API call and triage the response.
  $success = FALSE;
  try {
    $request = new Request('POST', $this
      ->fastlyRequestUri('service/service_id/purge/%s', $key));
    $response = $this->httpClient
      ->send($request, $this
      ->fastlyRequestOpt());
    $data = $this
      ->fastlyResponseData($response);
    if (isset($data['status']) && $data['status'] === 'ok') {
      $success = TRUE;
    }
    else {
      throw new RequestException('Unexpected API response.', $request, $response);
    }
  } catch (\Exception $e) {
    $this
      ->debugger()
      ->logFailedRequest($e);
  }

  // Update the invalidation objects accordingly.
  if ($success) {
    foreach ($invalidations as $invalidation) {
      $invalidation
        ->setState(InvalidationInterface::SUCCEEDED);
    }
  }
  else {
    foreach ($invalidations as $invalidation) {
      $invalidation
        ->setState(InvalidationInterface::FAILED);
    }
  }
}