You are here

NotifyUsersQueueWorker.php in Auto Purge Users 8.3

File

src/Plugin/QueueWorker/NotifyUsersQueueWorker.php
View source
<?php

declare (strict_types=1);
namespace Drupal\purge_users\Plugin\QueueWorker;

use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\purge_users\Services\UserManagementServiceInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;

/**
 * Processes cron queue.
 *
 * @QueueWorker(
 *   id = "notification_users",
 *   title = @Translation("Notify Users Tasks Worker: Notification Users"),
 *   cron = {"time" = 15}
 * )
 */
class NotifyUsersQueueWorker extends QueueWorkerBase implements ContainerFactoryPluginInterface {

  /**
   * The purge user manager.
   *
   * @var \Drupal\purge_users\Services\UserManagementServiceInterface
   */
  protected $purgeUserManager;

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

  /**
   * Constructs a new NotifyUsersQueueWorker instance.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\purge_users\Services\UserManagementServiceInterface $purge_user
   *   The purge user manager service.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, UserManagementServiceInterface $purge_user, EntityTypeManagerInterface $entity_type_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->purgeUserManager = $purge_user;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) : self {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('purge_users.user_management'), $container
      ->get('entity_type.manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function processItem($user_id) : void {
    $account = $this->entityTypeManager
      ->getStorage('user')
      ->load($user_id);
    if (!$account) {
      return;
    }
    $this->purgeUserManager
      ->notifyUser($account);
  }

}

Classes

Namesort descending Description
NotifyUsersQueueWorker Processes cron queue.