class ReportWorkerBase

Same name in other branches
  1. 3.x modules/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

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
MessengerTrait::$messenger protected property The messenger. 16
MessengerTrait::messenger public function Gets the messenger. 16
MessengerTrait::setMessenger public function Sets the messenger.
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.
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
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.