You are here

public function FastlyBackend::invalidateUrls in Acquia Purge 8

Invalidate all 'url' invalidations.

Overrides BackendInterface::invalidateUrls

See also

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

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

File

src/AcquiaPlatformCdn/FastlyBackend.php, line 117

Class

FastlyBackend
Provides a Fastly backend for the Platform CDN purger.

Namespace

Drupal\acquia_purge\AcquiaPlatformCdn

Code

public function invalidateUrls(array $invalidations) {
  $request_opt = $this
    ->fastlyRequestOpt();
  $request_opt['verify'] = FALSE;
  unset($request_opt['headers']['Accept']);
  unset($request_opt['headers']['Fastly-Key']);

  // Iterate over each URL and attempt to purge it.
  foreach ($invalidations as $invalidation) {
    $invalidation
      ->setState(InvalidationInterface::PROCESSING);

    // Execute the API call and triage the response.
    $success = FALSE;
    try {
      $request = new Request('PURGE', $invalidation
        ->getExpression());
      $response = $this->httpClient
        ->send($request, $request_opt);
      $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 object accordingly.
    if ($success) {
      $invalidation
        ->setState(InvalidationInterface::SUCCEEDED);
    }
    else {
      $invalidation
        ->setState(InvalidationInterface::FAILED);
    }
  }
}