ScheduledTransitionJob.php in Scheduled Transitions 8
File
src/Plugin/QueueWorker/ScheduledTransitionJob.php
View source
<?php
declare (strict_types=1);
namespace Drupal\scheduled_transitions\Plugin\QueueWorker;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\scheduled_transitions\Exception\ScheduledTransitionMissingEntity;
use Drupal\scheduled_transitions\ScheduledTransitionsRunnerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ScheduledTransitionJob extends QueueWorkerBase implements ContainerFactoryPluginInterface {
const SCHEDULED_TRANSITION_ID = 'scheduled_transition_id';
protected $scheduledTransitionsRunner;
protected $scheduledTransitionStorage;
public function __construct(array $configuration, string $plugin_id, $plugin_definition, EntityTypeManagerInterface $entityTypeManager, ScheduledTransitionsRunnerInterface $scheduledTransitionsRunner) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->scheduledTransitionStorage = $entityTypeManager
->getStorage('scheduled_transition');
$this->scheduledTransitionsRunner = $scheduledTransitionsRunner;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('scheduled_transitions.runner'));
}
public function processItem($data) {
$id = $data[static::SCHEDULED_TRANSITION_ID];
$transition = $this->scheduledTransitionStorage
->load($id);
if ($transition) {
try {
$this->scheduledTransitionsRunner
->runTransition($transition);
} catch (ScheduledTransitionMissingEntity $exception) {
$transition
->delete();
}
}
}
}