You are here

public function Counter::decrement in Purge 8.3

Decrease the counter.

Parameters

int|float $amount: Numeric amount to subtract from the current counter value.

Throws

\Drupal\purge\Plugin\Purge\Purger\Exception\BadBehaviorException Thrown when $amount is not a float, integer or when it is zero/negative.

\LogicException Thrown when the object got created without decrement permission.

Overrides CounterInterface::decrement

File

src/Counter/Counter.php, line 139

Class

Counter
Provides a numeric counter.

Namespace

Drupal\purge\Counter

Code

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);
}