You are here

public function InvalidationBase::setStateContext in Purge 8.3

Set (or reset) state context to the purger instance next in line.

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.

Parameters

string|null $purger_instance_id: The instance ID of the purger that is about to process the object, or NULL when no longer any purgers are processing it. NULL is the default.

Throws

\LogicException Thrown when the given parameter is empty, not a string or NULL.

\Drupal\purge\Plugin\Purge\Purger\Exception\BadPluginBehaviorException Thrown when the last set state was not any of:

Overrides InvalidationInterface::setStateContext

See also

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

\Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface::setState()

\Drupal\purge\Plugin\Purge\Invalidation\ImmutableInvalidationInterface::getState()

File

src/Plugin/Purge/Invalidation/InvalidationBase.php, line 125

Class

InvalidationBase
Provides base implementations for the invalidation object.

Namespace

Drupal\purge\Plugin\Purge\Invalidation

Code

public function setStateContext($purger_instance_id) {
  $new_is_string = is_string($purger_instance_id);
  $new_is_null = is_null($purger_instance_id);
  if ($new_is_string && !strlen($purger_instance_id)) {
    throw new \LogicException('Parameter $purger_instance_id is empty!');
  }
  elseif (!$new_is_string && !$new_is_null) {
    throw new \LogicException('Parameter $purger_instance_id is not NULL or a non-empty string!');
  }
  elseif ($purger_instance_id === $this->context) {
    return;
  }

  // Find out if states returning from purgers are actually valid.
  $old_is_string = is_string($this->context);
  $both_strings = $old_is_string && $new_is_string;
  $transferring = $both_strings && $this->context != $purger_instance_id;
  if ($transferring || $old_is_string && $new_is_null) {
    if (!in_array($this
      ->getState(), $this->statesAfterProcessing)) {
      throw new BadPluginBehaviorException("Only NOT_SUPPORTED, PROCESSING, SUCCEEDED and FAILED are valid outbound states.");
    }
  }
  $this->context = $purger_instance_id;
}