You are here

class Counter in Purge 8.3

Provides a numeric counter.

Hierarchy

Expanded class hierarchy of Counter

8 files declare their use of Counter
CapacityTracker.php in src/Plugin/Purge/Purger/CapacityTracker.php
CounterTest.php in tests/src/Unit/Counter/CounterTest.php
NumberOfItemsStatistic.php in src/Plugin/Purge/Queue/NumberOfItemsStatistic.php
RuntimeMeasurement.php in src/Plugin/Purge/Purger/RuntimeMeasurement.php
TotalFailedStatistic.php in src/Plugin/Purge/Queue/TotalFailedStatistic.php

... See full list

File

src/Counter/Counter.php, line 10

Namespace

Drupal\purge\Counter
View source
class Counter implements CounterInterface {

  /**
   * The callback that is called on writes when not NULL.
   *
   * @var null|callable
   */
  protected $callback;

  /**
   * Whether it is possible to call ::decrement() or not.
   *
   * @var bool
   */
  protected $permissionDecrement = TRUE;

  /**
   * Whether it is possible to call ::increment() or not.
   *
   * @var bool
   */
  protected $permissionIncrement = TRUE;

  /**
   * Whether it is possible to call ::set() or not.
   *
   * @var bool
   */
  protected $permissionSet = TRUE;

  /**
   * The value of the counter.
   *
   * @var float
   */
  protected $value;

  /**
   * {@inheritdoc}
   */
  public function __construct($value = 0.0) {
    $this
      ->set($value);
  }

  /**
   * {@inheritdoc}
   */
  public function disableDecrement() {
    $this->permissionDecrement = FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function disableIncrement() {
    $this->permissionIncrement = FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function disableSet() {
    $this->permissionSet = FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function get() {
    return $this->value;
  }

  /**
   * {@inheritdoc}
   */
  public function getInteger() {
    return (int) $this->value;
  }

  /**
   * {@inheritdoc}
   */
  public function set($value) {
    if (!$this->permissionSet) {
      throw new \LogicException('No ::set() permission on this object.');
    }
    $this
      ->setDirectly($value);
  }

  /**
   * {@inheritdoc}
   */
  public function setWriteCallback(callable $callback) {
    $this->callback = $callback;
  }

  /**
   * Overwrite the counter value (permission bypass).
   *
   * @param int|float $value
   *   The new value.
   *
   * @throws \Drupal\purge\Plugin\Purge\Purger\Exception\BadBehaviorException
   *   Thrown when $value is not a integer, float or when it is negative.
   * @throws \LogicException
   *   Thrown when the object got created without set permission.
   */
  protected function setDirectly($value) {
    if (!(is_float($value) || is_int($value))) {
      throw new BadBehaviorException('Given $value is not a integer or float.');
    }
    if (is_int($value)) {
      $value = (double) $value;
    }
    if ($value < 0.0) {
      throw new BadBehaviorException('Given $value can only be zero or positive.');
    }
    if ($value !== $this->value) {
      $this->value = $value;
      if (!is_null($this->callback)) {
        $callback = $this->callback;
        $callback($value);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function decrement($amount = 1.0) {
    if (!$this->permissionDecrement) {
      throw new \LogicException('No ::decrement() permission on this object.');
    }
    if (!(is_float($amount) || is_int($amount))) {
      throw new BadBehaviorException('Given $amount is not a integer or float.');
    }
    if (is_int($amount)) {
      $amount = (double) $amount;
    }
    if (!($amount > 0.0)) {
      throw new BadBehaviorException('Given $amount is zero or negative.');
    }
    $new = $this->value - $amount;
    if ($new < 0.0) {
      $new = 0.0;
    }
    $this
      ->setDirectly($new);
  }

  /**
   * {@inheritdoc}
   */
  public function increment($amount = 1.0) {
    if (!$this->permissionIncrement) {
      throw new \LogicException('No ::increment() permission on this object.');
    }
    if (!(is_float($amount) || is_int($amount))) {
      throw new BadBehaviorException('Given $amount is not a integer or float.');
    }
    if (is_int($amount)) {
      $amount = (double) $amount;
    }
    if (!($amount > 0.0)) {
      throw new BadBehaviorException('Given $amount is zero or negative.');
    }
    $this
      ->setDirectly($this->value + $amount);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Counter::$callback protected property The callback that is called on writes when not NULL.
Counter::$permissionDecrement protected property Whether it is possible to call ::decrement() or not.
Counter::$permissionIncrement protected property Whether it is possible to call ::increment() or not.
Counter::$permissionSet protected property Whether it is possible to call ::set() or not.
Counter::$value protected property The value of the counter.
Counter::decrement public function Decrease the counter. Overrides CounterInterface::decrement
Counter::disableDecrement public function Disable the possibility to decrement the counter. Overrides CounterInterface::disableDecrement
Counter::disableIncrement public function Disable the possibility to increment the counter. Overrides CounterInterface::disableIncrement
Counter::disableSet public function Disable the possibility of setting counter. Overrides CounterInterface::disableSet
Counter::get public function Get the current value. Overrides CounterInterface::get
Counter::getInteger public function Get the current value as integer. Overrides CounterInterface::getInteger
Counter::increment public function Increase the counter. Overrides CounterInterface::increment
Counter::set public function Overwrite the counter value. Overrides CounterInterface::set
Counter::setDirectly protected function Overwrite the counter value (permission bypass).
Counter::setWriteCallback public function Set the callback that gets called when writes occur. Overrides CounterInterface::setWriteCallback
Counter::__construct public function Construct a counter object. Overrides CounterInterface::__construct 3