diff --git a/modules/tide_block_inactive_users/src/Commands/TideInactiveUsersManagementCommands.php b/modules/tide_block_inactive_users/src/Commands/TideInactiveUsersManagementCommands.php index ccba783c7..47a16c49d 100644 --- a/modules/tide_block_inactive_users/src/Commands/TideInactiveUsersManagementCommands.php +++ b/modules/tide_block_inactive_users/src/Commands/TideInactiveUsersManagementCommands.php @@ -4,9 +4,10 @@ use Drupal\block_inactive_users\InactiveUsersHandler; use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\KeyValueStore\KeyValueFactory; use Drupal\Core\Logger\LoggerChannelFactory; use Drupal\Core\Queue\QueueFactory; -use Drupal\user\Entity\User; use Drush\Commands\DrushCommands; /** @@ -72,10 +73,24 @@ class TideInactiveUsersManagementCommands extends DrushCommands { */ protected $queue; + /** + * KeyValue service. + * + * @var \Drupal\Core\KeyValueStore\KeyValueFactory + */ + protected $keyvalue; + + /** + * KeyValue service. + * + * @var \Drupal\Core\Entity\EntityTypeManagerInterface + */ + protected $entityTypeManager; + /** * {@inheritdoc} */ - public function __construct(ConfigFactoryInterface $configFactory, InactiveUsersHandler $handler, LoggerChannelFactory $logger, QueueFactory $queueFactory) { + public function __construct(ConfigFactoryInterface $configFactory, InactiveUsersHandler $handler, LoggerChannelFactory $logger, QueueFactory $queueFactory, KeyValueFactory $keyValueFactory, EntityTypeManagerInterface $entityTypeManager) { parent::__construct(); $this->config = $configFactory; $this->blockUserhandler = $handler; @@ -85,6 +100,8 @@ public function __construct(ConfigFactoryInterface $configFactory, InactiveUsers $this->includeNeverAccessed = $this->blockInactiveUsers->get('block_inactive_users_include_never_accessed'); $this->excludeUserRoles = $this->blockInactiveUsers->get('block_inactive_users_exclude_roles'); $this->queue = $queueFactory->get('tide_block_inactive_users_queue'); + $this->keyvalue = $keyValueFactory; + $this->entityTypeManager = $entityTypeManager; } /** @@ -102,8 +119,7 @@ public function notify() { if ($last_access != 0 && !$user->hasRole('administrator')) { if ($this->blockUserhandler->timestampdiff($last_access, $current_time) >= $this->idleTime) { // Ensure the email only send once. - if (!\Drupal::keyValue('tide_inactive_users_management') - ->get($user->id())) { + if (!$this->keyvalue->get('tide_inactive_users_management')->get($user->id())) { $item = new \stdClass(); $item->uid = $user->id(); $this->queue->createItem($item); @@ -112,8 +128,7 @@ public function notify() { } if ($this->includeNeverAccessed == 1 && $last_access == 0) { if ($this->blockUserhandler->timestampdiff($user->getCreatedTime(), $current_time) >= $this->idleTime) { - if (!\Drupal::keyValue('tide_inactive_users_management') - ->get($user->id())) { + if (!$this->keyvalue->get('tide_inactive_users_management')->get($user->id())) { $item = new \stdClass(); $item->uid = $user->id(); $this->queue->createItem($item); @@ -131,14 +146,14 @@ public function notify() { * @aliases inactive-block */ public function block() { - $tide_inactive_users_management_results = \Drupal::keyValue('tide_inactive_users_management'); + $tide_inactive_users_management_results = $this->keyvalue->get('tide_inactive_users_management'); if ($times = $tide_inactive_users_management_results->getAll()) { foreach ($times as $uid => $time) { - $user = User::load($uid); + $user = $this->entityTypeManager->getStorage('user')->load($uid); if ($user && time() > $time) { $user->block(); $user->save(); - \Drupal::keyValue('tide_inactive_users_management') + $this->keyvalue->get('tide_inactive_users_management') ->delete($user->id()); } } @@ -149,13 +164,13 @@ public function block() { * Gets users. */ public function getUsers() { - $query = \Drupal::entityQuery('user')->condition('status', 1); + $query = $this->entityTypeManager->getStorage('user')->getQuery()->condition('status', 1); if (!empty($this->excludeUserRoles)) { $query->condition('roles.target_id', $this->excludeUserRoles, 'NOT IN'); } $user_ids = $query->execute(); if ($user_ids) { - return User::loadMultiple($user_ids); + return $this->entityTypeManager->getStorage('user')->loadMultiple($user_ids); } return []; } diff --git a/modules/tide_block_inactive_users/tide_block_inactive_users.services.yml b/modules/tide_block_inactive_users/tide_block_inactive_users.services.yml index 6ad926bad..b1b7d15a3 100644 --- a/modules/tide_block_inactive_users/tide_block_inactive_users.services.yml +++ b/modules/tide_block_inactive_users/tide_block_inactive_users.services.yml @@ -1,6 +1,6 @@ services: tide_inactive_users_management.commands: - class: \Drupal\tide_block_inactive_users\Commands\TideInactiveUsersManagementCommands - arguments: ['@config.factory','@block_inactive_users.deactivate_users','@logger.factory','@queue' ] + class: Drupal\tide_block_inactive_users\Commands\TideInactiveUsersManagementCommands + arguments: ['@config.factory','@block_inactive_users.deactivate_users','@logger.factory','@queue', '@keyvalue', '@entity_type.manager' ] tags: - { name: drush.command }