You are here

public function SeedCalculator::calculateSeed in Views random seed 8

Calculate a seed.

Parameters

array $options: The options for the random seed handler.

string $view_name: The name of the view.

string $display: The current display.

string $db_type: The current database type (mysql(i) - pgsql).

Return value

int Seed value which is a timestamp.

File

src/SeedCalculator.php, line 73

Class

SeedCalculator
Calculates seeds.

Namespace

Drupal\views_random_seed

Code

public function calculateSeed(array $options, string $view_name, string $display, string $db_type) {
  $time = $this->serverTime
    ->getRequestTime();
  $seed_name = 'views_seed_name-' . $view_name . '-' . $display;

  // Reuse from other view.
  if (!empty($options['reuse_seed'])) {
    $seed_name = 'views_seed_name-' . $options['reuse_seed'];
  }
  $this->options = $options;
  $seed = $this
    ->getSeed($seed_name);
  $this
    ->debug('Seed in storage: ' . $seed);
  $this
    ->debug('Current time: ' . $time);
  $options += [
    'user_seed_type' => 'same_per_user',
  ];

  // Create a first seed if necessary.
  if ($seed === FALSE) {
    $this
      ->debug('No feed in storage, generating.');
    $seed = $this
      ->generateSeed($seed_name, $time, $db_type);
  }

  // Reset seed or not? -1 is never, 0 is custom.
  if ($options['reset_seed_int'] !== -1) {
    $reset_time = $options['reset_seed_int'] === 0 ? $options['reset_seed_custom'] : $options['reset_seed_int'];
    $this
      ->debug('reset time: ' . $reset_time);
    $this
      ->debug('seed time: ' . $seed);
    $this
      ->debug('compare: ' . ($seed + $reset_time));
    if ($seed + $reset_time < $time) {
      $this
        ->debug('Resetting seed.');
      $seed = $this
        ->generateSeed($seed_name, $time, $db_type);

      // Invalidate cache for the current view when generating a new seed.
      Cache::invalidateTags([
        "views_random_seed-{$view_name}-{$display}",
      ]);
    }
  }

  // Return seed.
  return $seed;
}