CronHookHandler.php in Entity Share Cron 3.0.x
File
src/HookHandler/CronHookHandler.php
View source
<?php
declare (strict_types=1);
namespace Drupal\entity_share_cron\HookHandler;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\State\StateInterface;
use Drupal\entity_share_cron\EntityShareCronServiceInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CronHookHandler implements ContainerInjectionInterface {
const STATE_ID = 'entity_share_cron.cron_last_run';
protected $configFactory;
protected $state;
protected $logger;
protected $entityShareCron;
public function __construct(ConfigFactoryInterface $config_factory, StateInterface $state, LoggerInterface $logger, EntityShareCronServiceInterface $entity_share_cron) {
$this->configFactory = $config_factory;
$this->state = $state;
$this->logger = $logger;
$this->entityShareCron = $entity_share_cron;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('state'), $container
->get('logger.channel.entity_share_cron'), $container
->get('entity_share_cron'));
}
public function process() {
$config = $this->configFactory
->get('entity_share_cron.settings');
$now = time();
$interval = $config
->get('cron_interval');
$last_run = $this->state
->get(self::STATE_ID) ? $this->state
->get(self::STATE_ID) : -99999;
if ($now < $last_run + $interval) {
return;
}
$remotes_config = $config
->get('remotes');
if (is_array($remotes_config)) {
foreach ($remotes_config as $remote_id => $remote_config) {
if (empty($remote_config['enabled'])) {
continue;
}
$channels_config = isset($remote_config['channels']) ? $remote_config['channels'] : [];
foreach ($channels_config as $channel_id => $channel_config) {
if ($channel_config['enabled']) {
$this->logger
->info('Enqueuing channel %channel_id from remote %remote_id for synchronization.', [
'%channel_id' => $channel_id,
'%remote_id' => $remote_id,
]);
$this->entityShareCron
->enqueue($remote_id, $channel_id, NULL);
}
}
}
}
$this->state
->set(self::STATE_ID, $now);
}
}