You are here

function entity_share_cron_cron in Entity Share Cron 8

Same name and namespace in other branches
  1. 8.2 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 11
Hook implementations for the Entity share cron module.

Code

function entity_share_cron_cron() {
  $config = \Drupal::configFactory()
    ->getEditable('entity_share_cron.settings');

  // Checks the interval since the last synchronization.
  $now = time();
  $interval = $config
    ->get('cron_interval');
  $last_run = $config
    ->get('cron_last_run');
  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.
    $config
      ->set('cron_last_run', $now)
      ->save();
  }
}