You are here

function fac_cron in Fast Autocomplete 8

Same name and namespace in other branches
  1. 7 fac.module \fac_cron()

Implements hook_cron().

File

./fac.module, line 90
This file contains the main functions of the Fast Autocomplete module.

Code

function fac_cron() {

  // Clean up the JSON files.
  if ($fac_config_ids = \Drupal::entityQuery('fac_config')
    ->execute()) {
    try {
      $fac_configs = \Drupal::EntityTypeManager()
        ->getStorage('fac_config')
        ->loadMultiple(array_keys($fac_config_ids));
      foreach ($fac_configs as $fac_config) {

        /** @var \Drupal\fac\Entity\FacConfig $fac_config */
        if ($fac_config
          ->cleanUpFiles()) {
          $expiry_time = strtotime($fac_config
            ->getFilesExpiryTime());
          $fac_config
            ->deleteFiles($expiry_time);
        }
      }
    } catch (InvalidPluginDefinitionException $e) {
      \Drupal::logger('fac')
        ->error('An error occurred: ' . $e
        ->getMessage());
    }
  }
  $key_interval = \Drupal::config('fac.settings')
    ->get('key_interval');

  // Change the key.
  $key_timestamp = \Drupal::state()
    ->get('fac_key_timestamp');
  if (time() > $key_timestamp + $key_interval) {
    \Drupal::service('fac.hash_service')
      ->getKey(TRUE);

    // Clean up files; the paths are no longer valid with the new key.
    \Drupal::service('file_system')
      ->deleteRecursive(PublicStream::basePath() . '/fac-json');
  }
}