You are here

public function FastlyBackend::invalidateTags in Acquia Purge 8

Invalidate all 'tag' invalidations.

Overrides BackendInterface::invalidateTags

See also

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

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

File

src/AcquiaPlatformCdn/FastlyBackend.php, line 65

Class

FastlyBackend
Provides a Fastly backend for the Platform CDN purger.

Namespace

Drupal\acquia_purge\AcquiaPlatformCdn

Code

public function invalidateTags(array $invalidations) {

  // Set the invalidation objects into PROCESSING state and extract the tags.
  $tags = [];
  foreach ($invalidations as $invalidation) {
    $invalidation
      ->setState(InvalidationInterface::PROCESSING);
    $tags[] = $invalidation
      ->getExpression();
  }
  $tags = new TagsHeaderValue($tags, self::getHashedTags($tags));

  // Execute the API call and triage the response.
  $success = FALSE;
  try {
    $request = new Request('POST', $this
      ->fastlyRequestUri('service/service_id/purge'));
    $request_opt = $this
      ->fastlyRequestOpt([
      'Surrogate-Key' => $tags
        ->__toString(),
    ]);

    // Pass the TagsHeaderValue to DebuggerMiddleware (when loaded).
    if ($this
      ->debugger()
      ->enabled()) {
      $request_opt['acquia_purge_tags'] = $tags;
    }
    $response = $this->httpClient
      ->send($request, $request_opt);
    $data = $this
      ->fastlyResponseData($response);
    if (count($data)) {
      $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);
    }
  }
}