ImmutableInvalidationBase.php in Purge 8.3
File
src/Plugin/Purge/Invalidation/ImmutableInvalidationBase.php
View source
<?php
namespace Drupal\purge\Plugin\Purge\Invalidation;
use Drupal\Core\Plugin\PluginBase;
abstract class ImmutableInvalidationBase extends PluginBase implements ImmutableInvalidationInterface {
protected $id;
protected $context = NULL;
protected $expression = NULL;
protected $properties = [];
protected $states = [];
protected $statesAfterProcessing = [
self::NOT_SUPPORTED,
self::PROCESSING,
self::SUCCEEDED,
self::FAILED,
];
public function __toString() {
return is_null($this->expression) ? '' : $this->expression;
}
public function getExpression() {
return $this->expression;
}
public function getProperties() {
if (!is_null($this->context)) {
throw new \LogicException('Cannot retrieve properties in purger context.');
}
return $this->properties;
}
public function getProperty($key) {
if (is_null($this->context)) {
throw new \LogicException('Call ::setStateContext() before retrieving properties!');
}
if (isset($this->properties[$this->context][$key])) {
return $this->properties[$this->context][$key];
}
return NULL;
}
public function getState() {
if (empty($this->states)) {
return self::FRESH;
}
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 ($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;
}
}
public function getStateString() {
$mapping = [
self::FRESH => 'FRESH',
self::PROCESSING => 'PROCESSING',
self::SUCCEEDED => 'SUCCEEDED',
self::FAILED => 'FAILED',
self::NOT_SUPPORTED => 'NOT_SUPPORTED',
];
return $mapping[$this
->getState()];
}
public function getStateStringTranslated() {
$mapping = [
self::FRESH => $this
->t('New'),
self::PROCESSING => $this
->t('Currently invalidating'),
self::SUCCEEDED => $this
->t('Succeeded'),
self::FAILED => $this
->t('Failed'),
self::NOT_SUPPORTED => $this
->t('Not supported'),
];
return $mapping[$this
->getState()];
}
public function getStates() {
return $this->states;
}
public function getStateContexts() {
if (!is_null($this->context)) {
throw new \LogicException('Cannot retrieve state contexts in purger context.');
}
return array_keys($this->states);
}
public function getType() {
return $this
->getPluginId();
}
}