You are here

class TxBuffer in Purge 8.3

Provides the transaction buffer.

Hierarchy

Expanded class hierarchy of TxBuffer

2 files declare their use of TxBuffer
ProxyItemTest.php in tests/src/Kernel/Queue/ProxyItemTest.php
TxBufferTest.php in tests/src/Kernel/Queue/TxBufferTest.php
1 string reference to 'TxBuffer'
purge.services.yml in ./purge.services.yml
purge.services.yml
1 service uses TxBuffer
purge.queue.txbuffer in ./purge.services.yml
Drupal\purge\Plugin\Purge\Queue\TxBuffer

File

src/Plugin/Purge/Queue/TxBuffer.php, line 11

Namespace

Drupal\purge\Plugin\Purge\Queue
View source
class TxBuffer implements TxBufferInterface {

  /**
   * Instances listing holding copies of each Invalidation object.
   *
   * @var \Drupal\purge\Plugin\Purge\Invalidation\InvalidationInterface[]
   */
  private $instances = [];

  /**
   * Instance<->state map of each object in the buffer.
   *
   * @var array|array[int]
   */
  private $states = [];

  /**
   * Instance<->property map of each object in the buffer.
   *
   * @var array|array[int]
   */
  private $properties = [];

  /**
   * {@inheritdoc}
   *
   * @see http://php.net/manual/en/countable.count.php
   */
  public function count() {
    return count($this->instances);
  }

  /**
   * {@inheritdoc}
   *
   * @see http://php.net/manual/en/class.iterator.php
   */
  public function current() {
    return current($this->instances);
  }

  /**
   * {@inheritdoc}
   */
  public function delete($invalidations) {
    if (!is_array($invalidations)) {
      $invalidations = [
        $invalidations,
      ];
    }
    foreach ($invalidations as $i) {
      unset($this->instances[$i
        ->getId()]);
      unset($this->states[$i
        ->getId()]);
      unset($this->properties[$i
        ->getId()]);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function deleteEverything() {
    $this->instances = [];
    $this->states = [];
    $this->properties = [];
  }

  /**
   * {@inheritdoc}
   */
  public function getByProperty($property, $value) {
    foreach ($this->properties as $id => $properties) {
      if (isset($properties[$property])) {
        if ($properties[$property] === $value) {
          return $this->instances[$id];
        }
      }
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getFiltered($states) {
    if (!is_array($states)) {
      $states = [
        $states,
      ];
    }
    $results = [];
    foreach ($this->states as $id => $state) {
      if (in_array($state, $states)) {
        $results[] = $this->instances[$id];
      }
    }
    return $results;
  }

  /**
   * {@inheritdoc}
   */
  public function getState(InvalidationInterface $invalidation) {
    if (!$this
      ->has($invalidation)) {
      return NULL;
    }
    return $this->states[$invalidation
      ->getId()];
  }

  /**
   * {@inheritdoc}
   */
  public function getProperty(InvalidationInterface $invalidation, $property, $default = NULL) {
    if (!isset($this->properties[$invalidation
      ->getId()][$property])) {
      return $default;
    }
    return $this->properties[$invalidation
      ->getId()][$property];
  }

  /**
   * {@inheritdoc}
   */
  public function has(InvalidationInterface $invalidation) {
    return isset($this->instances[$invalidation
      ->getId()]);
  }

  /**
   * {@inheritdoc}
   *
   * @see http://php.net/manual/en/iterator.key.php
   */
  public function key() {
    return key($this->instances);
  }

  /**
   * {@inheritdoc}
   *
   * @see http://php.net/manual/en/iterator.next.php
   */
  public function next() {
    return next($this->instances);
  }

  /**
   * {@inheritdoc}
   *
   * @see http://php.net/manual/en/iterator.rewind.php
   */
  public function rewind() {
    return reset($this->instances);
  }

  /**
   * {@inheritdoc}
   */
  public function set($invalidations, $state) {
    if (!is_array($invalidations)) {
      $invalidations = [
        $invalidations,
      ];
    }
    foreach ($invalidations as $i) {
      if (!$i instanceof InvalidationInterface) {
        throw new BadBehaviorException("Item is not a \\Drupal\\purge\\Plugin\\Purge\\Invalidation\\InvalidationInterface derivative.");
      }
      if (!$this
        ->has($i)) {
        $this->instances[$i
          ->getId()] = $i;
      }
      $this->states[$i
        ->getId()] = $state;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function setProperty(InvalidationInterface $invalidation, $property, $value) {
    if ($this
      ->has($invalidation)) {
      $this->properties[$invalidation
        ->getId()][$property] = $value;
    }
  }

  /**
   * {@inheritdoc}
   *
   * @see http://php.net/manual/en/iterator.valid.php
   */
  public function valid() {
    return is_null(key($this->instances)) ? FALSE : TRUE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TxBuffer::$instances private property Instances listing holding copies of each Invalidation object.
TxBuffer::$properties private property Instance<->property map of each object in the buffer.
TxBuffer::$states private property Instance<->state map of each object in the buffer.
TxBuffer::count public function
TxBuffer::current public function
TxBuffer::delete public function Delete the given invalidation object from the buffer. Overrides TxBufferInterface::delete
TxBuffer::deleteEverything public function Delete everything in the buffer. Overrides TxBufferInterface::deleteEverything
TxBuffer::getByProperty public function Retrieve a buffered object by property=value combination. Overrides TxBufferInterface::getByProperty
TxBuffer::getFiltered public function Only retrieve items from the buffer in a particular given state(s). Overrides TxBufferInterface::getFiltered
TxBuffer::getProperty public function Retrieve a stored property for the given invalidation object. Overrides TxBufferInterface::getProperty
TxBuffer::getState public function Request the in-buffer set state for the given invalidation object. Overrides TxBufferInterface::getState
TxBuffer::has public function Check if the given object is already in buffer our not. Overrides TxBufferInterface::has
TxBuffer::key public function
TxBuffer::next public function
TxBuffer::rewind public function
TxBuffer::set public function Set the given state on one or multiple invalidation objects. Overrides TxBufferInterface::set
TxBuffer::setProperty public function Store a named property for the given invalidation object. Overrides TxBufferInterface::setProperty
TxBuffer::valid public function
TxBufferInterface::ADDED constant Objects that just got added to the queue.
TxBufferInterface::ADDING constant Objects in the process of being added to the queue.
TxBufferInterface::CLAIMED constant Freshly claimed objects.
TxBufferInterface::DELETING constant Objects in the process of being deleted from the queue.
TxBufferInterface::RELEASED constant Objects that just got released back to the queue.
TxBufferInterface::RELEASING constant Objects in the process of being released back to the queue.