You are here

class EntityUsageRegenerateTrackingInfoWorker in Entity Usage 8.2

RegenerateTrackingInfoWorker class.

A worker plugin to consume items from "entity_usage_regenerate_queue" and regenerate tracking info for each of them.

Plugin annotation


@QueueWorker(
  id = "entity_usage_regenerate_queue",
  title = @Translation("Entity Usage Regenerate Tracking Queue"),
  cron = {"time" = 60}
)

Hierarchy

Expanded class hierarchy of EntityUsageRegenerateTrackingInfoWorker

File

src/Plugin/QueueWorker/EntityUsageRegenerateTrackingInfoWorker.php, line 24

Namespace

Drupal\entity_usage\Plugin\QueueWorker
View source
class EntityUsageRegenerateTrackingInfoWorker extends QueueWorkerBase implements ContainerFactoryPluginInterface {

  /**
   * The entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The logger channel factory.
   *
   * @var \Drupal\Core\Logger\LoggerChannelFactoryInterface
   */
  protected $logger;

  /**
   * The Entity Usage update manager service.
   *
   * @var \Drupal\entity_usage\EntityUpdateManager
   */
  protected $entityUsageUpdateManager;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, LoggerChannelFactoryInterface $logger_factory, EntityUpdateManager $track_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->entityTypeManager = $entity_type_manager;
    $this->logger = $logger_factory
      ->get('entity_usage');
    $this->entityUsageUpdateManager = $track_manager;
  }

  /**
   * {@inheritdoc}
   */
  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('logger.factory'), $container
      ->get('entity_usage.entity_update_manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function processItem($data) {

    // $data here is expected to contain
    // - 'entity_type': always present;
    // - 'entity_id': present when the entity isn't revisionable;
    // - 'entity_revision_id': present when the entity is revisionable;
    if (empty($data['entity_type']) || empty($data['entity_id']) && empty($data['entity_revision_id'])) {

      // Just skip this item.
      return;
    }
    $storage = $this->entityTypeManager
      ->getStorage($data['entity_type']);
    if ($storage
      ->getEntityType()
      ->isRevisionable() && !empty($data['entity_revision_id'])) {
      $entity = $storage
        ->loadRevision($data['entity_revision_id']);
    }
    elseif (!empty($data['entity_id'])) {
      $entity = $storage
        ->load($data['entity_id']);
    }
    if ($entity) {
      try {
        $this->entityUsageUpdateManager
          ->trackUpdateOnCreation($entity);
      } catch (\Exception $e) {
        $this->logger
          ->warning("An error occurred when tracking usage info for entity with data: @data. Error message: {$e->getMessage()}", [
          '@data' => json_encode($data),
        ]);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityUsageRegenerateTrackingInfoWorker::$entityTypeManager protected property The entity type manager service.
EntityUsageRegenerateTrackingInfoWorker::$entityUsageUpdateManager protected property The Entity Usage update manager service.
EntityUsageRegenerateTrackingInfoWorker::$logger protected property The logger channel factory.
EntityUsageRegenerateTrackingInfoWorker::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
EntityUsageRegenerateTrackingInfoWorker::processItem public function Works on a single queue item. Overrides QueueWorkerInterface::processItem
EntityUsageRegenerateTrackingInfoWorker::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.