You are here

abstract class ReportWorkerBase in Examples for Developers 3.x

Same name and namespace in other branches
  1. 8 cron_example/src/Plugin/QueueWorker/ReportWorkerBase.php \Drupal\cron_example\Plugin\QueueWorker\ReportWorkerBase

Provides base functionality for the ReportWorkers.

Hierarchy

Expanded class hierarchy of ReportWorkerBase

File

modules/cron_example/src/Plugin/QueueWorker/ReportWorkerBase.php, line 16

Namespace

Drupal\cron_example\Plugin\QueueWorker
View source
abstract class ReportWorkerBase extends QueueWorkerBase implements ContainerFactoryPluginInterface {
  use StringTranslationTrait;
  use MessengerTrait;

  /**
   * The state.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * The logger.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * ReportWorkerBase constructor.
   *
   * @param array $configuration
   *   The configuration of the instance.
   * @param string $plugin_id
   *   The plugin id.
   * @param mixed $plugin_definition
   *   The plugin definition.
   * @param \Drupal\Core\State\StateInterface $state
   *   The state service the instance should use.
   * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger
   *   The logger service the instance should use.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, StateInterface $state, LoggerChannelFactoryInterface $logger) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->state = $state;
    $this->logger = $logger;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $form = new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('state'), $container
      ->get('logger.factory'));
    $form
      ->setMessenger($container
      ->get('messenger'));
    return $form;
  }

  /**
   * Simple reporter log and display information about the queue.
   *
   * @param int $worker
   *   Worker number.
   * @param object $item
   *   The $item which was stored in the cron queue.
   */
  protected function reportWork($worker, $item) {
    if ($this->state
      ->get('cron_example_show_status_message')) {
      $this
        ->messenger()
        ->addMessage($this
        ->t('Queue @worker worker processed item with sequence @sequence created at @time', [
        '@worker' => $worker,
        '@sequence' => $item->sequence,
        '@time' => date('c', $item->created),
      ]));
    }
    $this->logger
      ->get('cron_example')
      ->info('Queue @worker worker processed item with sequence @sequence created at @time', [
      '@worker' => $worker,
      '@sequence' => $item->sequence,
      '@time' => date('c', $item->created),
    ]);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
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 2
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.
QueueWorkerInterface::processItem public function Works on a single queue item. 8
ReportWorkerBase::$logger protected property The logger.
ReportWorkerBase::$state protected property The state.
ReportWorkerBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
ReportWorkerBase::reportWork protected function Simple reporter log and display information about the queue.
ReportWorkerBase::__construct public function ReportWorkerBase constructor. Overrides PluginBase::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.