Counter.php in Purge 8.3
File
src/Counter/Counter.php
View source
<?php
namespace Drupal\purge\Counter;
use Drupal\purge\Plugin\Purge\Purger\Exception\BadBehaviorException;
class Counter implements CounterInterface {
protected $callback;
protected $permissionDecrement = TRUE;
protected $permissionIncrement = TRUE;
protected $permissionSet = TRUE;
protected $value;
public function __construct($value = 0.0) {
$this
->set($value);
}
public function disableDecrement() {
$this->permissionDecrement = FALSE;
}
public function disableIncrement() {
$this->permissionIncrement = FALSE;
}
public function disableSet() {
$this->permissionSet = FALSE;
}
public function get() {
return $this->value;
}
public function getInteger() {
return (int) $this->value;
}
public function set($value) {
if (!$this->permissionSet) {
throw new \LogicException('No ::set() permission on this object.');
}
$this
->setDirectly($value);
}
public function setWriteCallback(callable $callback) {
$this->callback = $callback;
}
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);
}
}
}
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);
}
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);
}
}