You are here

protected function StatsTracker::initializeStatistics in Purge 8.3

Initialize the counter instances.

10 calls to StatsTracker::initializeStatistics()
StatsTracker::count in src/Plugin/Purge/Queue/StatsTracker.php
StatsTracker::current in src/Plugin/Purge/Queue/StatsTracker.php
Return the current element.
StatsTracker::key in src/Plugin/Purge/Queue/StatsTracker.php
Return the key of the current element.
StatsTracker::next in src/Plugin/Purge/Queue/StatsTracker.php
Move forward to next element.
StatsTracker::numberOfItems in src/Plugin/Purge/Queue/StatsTracker.php
The number of items currently in the queue.

... See full list

File

src/Plugin/Purge/Queue/StatsTracker.php, line 82

Class

StatsTracker
Provides the queue statistics tracker.

Namespace

Drupal\purge\Plugin\Purge\Queue

Code

protected function initializeStatistics() {
  if (!empty($this->instances)) {
    return;
  }

  // Fetch all statistic values from the state API at once.
  $values = $this->state
    ->getMultiple($this->stats);

  // Instantiate the persistent counters with the given values.
  foreach ($this->stats as $i => $statekey) {

    // Set a default as CounterInterface only understands integers.
    if (!isset($values[$statekey]) || is_null($values[$statekey])) {
      $values[$statekey] = 0;
    }

    // Instantiate the counter and pass a write callback that puts written
    // values directly back into $this->stateBuffer. At the end of this
    // request, ::destruct() will pick them up and save the values.
    $this->instances[$i] = new $this->statClasses[$i]($values[$statekey]);
    $this->instances[$i]
      ->setWriteCallback(function ($value) use ($statekey) {
      $this->stateBuffer[$statekey] = $value;
    });
  }
}