You are here

function entity_share_cron_cron in Entity Share Cron 8.2

Same name and namespace in other branches
  1. 8 entity_share_cron.module \entity_share_cron_cron()
  2. 3.0.x entity_share_cron.module \entity_share_cron_cron()

Implements hook_cron().

File

./entity_share_cron.module, line 13

Code

function entity_share_cron_cron() {
  $config = \Drupal::config('entity_share_cron.settings');
  $state = \Drupal::state();

  // Checks the interval since the last synchronization.
  $now = time();
  $interval = $config
    ->get('cron_interval');
  $last_run = $state
    ->get('entity_share_cron.cron_last_run') ? $state
    ->get('entity_share_cron.cron_last_run') : -99999;
  if ($last_run + $interval < $now) {
    $service = \Drupal::service('entity_share_cron');
    $logger = \Drupal::logger('entity_share_cron');

    // Enqueues enabled remotes and channels for synchronization.
    $remotes_config = $config
      ->get('remotes');
    foreach ($remotes_config as $remote_id => $remote_config) {

      // Checks if synchronization of this remote is enabled.
      if (!empty($remote_config['enabled'])) {
        $channels_config = isset($remote_config['channels']) ? $remote_config['channels'] : [];
        foreach ($channels_config as $channel_id => $channel_config) {

          // Checks if synchronization of this channel is enabled.
          if (!empty($channel_config['enabled']) && !empty($channel_config['url'])) {

            // Enqueues the channel for synchronization.
            $logger
              ->info('Enqueuing channel %channel_id from remote %remote_id for synchronization.', [
              '%channel_id' => $channel_id,
              '%remote_id' => $remote_id,
            ]);
            $service
              ->enqueue($remote_id, $channel_id, $channel_config);
          }
        }
      }
    }

    // Updates last run timestamp.
    $state
      ->set('entity_share_cron.cron_last_run', $now);
  }
}