Skip to content

Commit

Permalink
SDPA-5959: Fix dependency injection coding standards
Browse files Browse the repository at this point in the history
  • Loading branch information
christopher-hopper committed May 19, 2022
1 parent 26f68ea commit 9f68dc4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}

/**
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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());
}
}
Expand All @@ -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 [];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 }

0 comments on commit 9f68dc4

Please sign in to comment.