You are here

public function AcquiaCloudPurger::invalidateUrls in Acquia Purge 8

Invalidate a set of URL invalidations.

See also

\Drupal\purge\Plugin\Purge\Purger\PurgerInterface::invalidate()

\Drupal\purge\Plugin\Purge\Purger\PurgerInterface::routeTypeToMethod()

File

src/Plugin/Purge/Purger/AcquiaCloudPurger.php, line 320

Class

AcquiaCloudPurger
Acquia Cloud.

Namespace

Drupal\acquia_purge\Plugin\Purge\Purger

Code

public function invalidateUrls(array $invalidations) {
  $this
    ->debugger()
    ->callerAdd(__METHOD__);

  // Change all invalidation objects into the PROCESS state before kickoff.
  foreach ($invalidations as $inv) {
    $inv
      ->setState(InvalidationInterface::PROCESSING);
  }

  // Generate request objects for each balancer/invalidation combination.
  $ipv4_addresses = $this->platformInfo
    ->getBalancerAddresses();
  $token = $this->platformInfo
    ->getBalancerToken();
  $requests = function () use ($invalidations, $ipv4_addresses, $token) {
    foreach ($invalidations as $inv) {
      foreach ($ipv4_addresses as $ipv4) {
        (yield $inv
          ->getId() => function ($poolopt) use ($inv, $ipv4, $token) {
          $uri = $inv
            ->getExpression();
          $host = parse_url($uri, PHP_URL_HOST);
          $uri = str_replace($host, $ipv4, $uri);
          $opt = [
            'headers' => [
              'X-Acquia-Purge' => $token,
              'Accept-Encoding' => 'gzip',
              'User-Agent' => 'Acquia Purge',
              'Host' => $host,
            ],
          ];
          if (is_array($poolopt) && count($poolopt)) {
            $opt = array_merge($poolopt, $opt);
          }
          return $this->httpClient
            ->requestAsync('PURGE', $uri, $opt);
        });
      }
    }
  };

  // Execute the requests generator and retrieve the results.
  $results = $this
    ->getResultsConcurrently($requests);

  // Triage the results and set all invalidation states correspondingly.
  foreach ($invalidations as $invalidation) {
    $inv_id = $invalidation
      ->getId();
    if (!isset($results[$inv_id]) || !count($results[$inv_id])) {
      $invalidation
        ->setState(InvalidationInterface::FAILED);
    }
    else {
      if (in_array(FALSE, $results[$inv_id])) {
        $invalidation
          ->setState(InvalidationInterface::FAILED);
      }
      else {
        $invalidation
          ->setState(InvalidationInterface::SUCCEEDED);
      }
    }
  }
  $this
    ->debugger()
    ->callerRemove(__METHOD__);
}