You are here

public function ImmutableInvalidationBase::getState in Purge 8.3

Get the current or general state of the invalidation.

New, freshly claimed invalidations and those exiting the PurgersService always have NULL as their state context. This means that when called, ::getState() resolves the general state by triaging all stored states. So for example: when no states are known, it will evaluate to FRESH but when one state is set to SUCCEEDED and a few others to FAILED, the general state becomes FAILED. When only SUCCEEDED's is stored, it will evaluate as such.

However, the behaviors of ::getState() and ::setState() change after a call to ::setStateContext(). From this point on, both will respectively retrieve and store the state *specific* to that purger context. Context switching is handled by PurgersServiceInterface::invalidate() and therefore no understanding of this concept is required outside the purgers service code.

Return value

int Any \Drupal\purge\Plugin\Purge\Invalidation\InvStatesInterface constant.

Throws

\LogicException Thrown state are stored that should not have been stored, as is not never supposed to happen catching this exception is not recommended.

Overrides ImmutableInvalidationInterface::getState

1 call to ImmutableInvalidationBase::getState()
InvalidationBase::setStateContext in src/Plugin/Purge/Invalidation/InvalidationBase.php
Set (or reset) state context to the purger instance next in line.
1 method overrides ImmutableInvalidationBase::getState()
ImmutableInvalidation::getState in src/Plugin/Purge/Invalidation/ImmutableInvalidation.php
Get the current or general state of the invalidation.

File

src/Plugin/Purge/Invalidation/ImmutableInvalidationBase.php, line 113

Class

ImmutableInvalidationBase
Provides base implementations the immutable invalidation object.

Namespace

Drupal\purge\Plugin\Purge\Invalidation

Code

public function getState() {

  // Regardless of the context, when there are no states stored we're FRESH.
  if (empty($this->states)) {
    return self::FRESH;
  }

  // In general context, we need to resolve what the invalidation state is.
  if ($this->context === NULL) {
    $totals = [
      self::SUCCEEDED => 0,
      self::NOT_SUPPORTED => 0,
    ];
    $total = count($this->states);
    foreach ($this->states as $state) {
      if (isset($totals[$state])) {
        $totals[$state]++;
      }
    }

    // If all purgers failed to support it, its unsupported.
    if ($totals[self::NOT_SUPPORTED] === $total) {
      return self::NOT_SUPPORTED;
    }
    elseif ($totals[self::SUCCEEDED] === $total) {
      return self::SUCCEEDED;
    }
    elseif (in_array(self::FAILED, $this->states)) {
      return self::FAILED;
    }
    elseif (in_array(self::PROCESSING, $this->states)) {
      return self::PROCESSING;
    }
    elseif (in_array(self::NOT_SUPPORTED, $this->states)) {
      if (in_array(self::FAILED, $this->states)) {
        return self::FAILED;
      }
      elseif (in_array(self::PROCESSING, $this->states)) {
        return self::PROCESSING;
      }
      elseif (in_array(self::SUCCEEDED, $this->states)) {
        return self::SUCCEEDED;
      }
    }
    throw new \LogicException("Invalidation state data integrity violation");
  }
  else {
    if (isset($this->states[$this->context])) {
      return $this->states[$this->context];
    }
    return self::FRESH;
  }
}