ReportWorkerBase.php in Examples for Developers 8
File
cron_example/src/Plugin/QueueWorker/ReportWorkerBase.php
View source
<?php
namespace Drupal\cron_example\Plugin\QueueWorker;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Messenger\MessengerTrait;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\Core\State\StateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class ReportWorkerBase extends QueueWorkerBase implements ContainerFactoryPluginInterface {
use StringTranslationTrait;
use MessengerTrait;
protected $state;
protected $logger;
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;
}
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;
}
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),
]);
}
}