Skip to content
Snippets Groups Projects

Issue #3467383 by TR, urvashi_vora: Dependency injection in Drush commands

Files
2
@@ -2,11 +2,16 @@
@@ -2,11 +2,16 @@
namespace Drupal\votingapi\Drush\Commands;
namespace Drupal\votingapi\Drush\Commands;
// cspell:ignore genv vcalc vflush vtype etype resultfunction
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Database\Connection;
 
use Drupal\Core\Entity\EntityTypeManagerInterface;
 
use Drupal\votingapi\Entity\Vote;
 
use Drupal\votingapi\VoteResultFunctionManager;
use Drush\Attributes as CLI;
use Drush\Attributes as CLI;
use Drush\Commands\DrushCommands;
use Drush\Commands\DrushCommands;
use Drupal\votingapi\Entity\Vote;
use Symfony\Component\DependencyInjection\ContainerInterface;
 
 
// cspell:ignore genv vcalc vflush vtype etype resultfunction
/**
/**
* Drush 12+ commands for the Voting API module.
* Drush 12+ commands for the Voting API module.
@@ -16,6 +21,65 @@ use Drupal\votingapi\Entity\Vote;
@@ -16,6 +21,65 @@ use Drupal\votingapi\Entity\Vote;
*/
*/
class VotingApiDrushCommands extends DrushCommands {
class VotingApiDrushCommands extends DrushCommands {
 
/**
 
* The datetime.time service.
 
*
 
* @var \Drupal\Component\Datetime\TimeInterface
 
*/
 
protected $timeService;
 
 
/**
 
* The database connection.
 
*
 
* @var \Drupal\Core\Database\Connection
 
*/
 
protected $database;
 
 
/**
 
* The entity type manager.
 
*
 
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
 
*/
 
protected $entityTypeManager;
 
 
/**
 
* The vote result function manager.
 
*
 
* @var \Drupal\votingapi\VoteResultFunctionManager
 
*/
 
protected $voteResultFunctionManager;
 
 
/**
 
* Constructs an object.
 
*
 
* @param \Drupal\Component\Datetime\TimeInterface $time_service
 
* The datetime.time service.
 
* @param \Drupal\Core\Database\Connection $database
 
* The database connection.
 
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
 
* The entity type manager service.
 
* @param \Drupal\votingapi\VoteResultFunctionManager $vote_result_function_manager
 
* The vote result function manager.
 
*/
 
public function __construct(TimeInterface $time_service, Connection $database, EntityTypeManagerInterface $entity_type_manager, VoteResultFunctionManager $vote_result_function_manager) {
 
$this->timeService = $time_service;
 
$this->database = $database;
 
$this->entityTypeManager = $entity_type_manager;
 
$this->voteResultFunctionManager = $vote_result_function_manager;
 
}
 
 
/**
 
* {@inheritdoc}
 
*/
 
public static function create(ContainerInterface $container) {
 
return new static(
 
$container->get('datetime.time'),
 
$container->get('database'),
 
$container->get('entity_type.manager'),
 
$container->get('plugin.manager.votingapi.resultfunction')
 
);
 
}
 
/**
/**
* Creates dummy voting data.
* Creates dummy voting data.
*
*
@@ -67,7 +131,7 @@ class VotingApiDrushCommands extends DrushCommands {
@@ -67,7 +131,7 @@ class VotingApiDrushCommands extends DrushCommands {
*
*
* @param string $entity_type
* @param string $entity_type
* The type of entity to recalculate vote results for.
* The type of entity to recalculate vote results for.
* @param ?string $entity_id
* @param string $entity_id
* The ID of the entity.
* The ID of the entity.
* @param string $vote_type
* @param string $vote_type
* The type of votes to recalculate, defaults to 'percent'.
* The type of votes to recalculate, defaults to 'percent'.
@@ -84,10 +148,10 @@ class VotingApiDrushCommands extends DrushCommands {
@@ -84,10 +148,10 @@ class VotingApiDrushCommands extends DrushCommands {
#[CLI\Argument(name: 'entity_id', description: 'The ID of the entity.')]
#[CLI\Argument(name: 'entity_id', description: 'The ID of the entity.')]
#[CLI\Argument(name: 'vote_type', description: "The type of votes to recalculate, defaults to 'percent'.")]
#[CLI\Argument(name: 'vote_type', description: "The type of votes to recalculate, defaults to 'percent'.")]
#[CLI\Usage(name: 'drush voting:recalculate [entity_type]', description: "Regenerates voting results from raw vote data. Defaults to 'node'.")]
#[CLI\Usage(name: 'drush voting:recalculate [entity_type]', description: "Regenerates voting results from raw vote data. Defaults to 'node'.")]
public function recalculate(string $entity_type = 'node', ?string $entity_id, string $vote_type = 'vote'): void {
public function recalculate(string $entity_type = 'node', string $entity_id = NULL, string $vote_type = 'vote'): void {
// Prep some starter query objects.
// Prep some starter query objects.
if (empty($entity_id)) {
if (empty($entity_id)) {
$votes = \Drupal::database()->select('votingapi_vote', 'vv')
$votes = $this->database->select('votingapi_vote', 'vv')
->fields('vv', ['entity_type', 'entity_id'])
->fields('vv', ['entity_type', 'entity_id'])
->condition('entity_type', $entity_type, '=')
->condition('entity_type', $entity_type, '=')
->distinct(TRUE)
->distinct(TRUE)
@@ -102,9 +166,8 @@ class VotingApiDrushCommands extends DrushCommands {
@@ -102,9 +166,8 @@ class VotingApiDrushCommands extends DrushCommands {
]);
]);
}
}
$manager = \Drupal::service('plugin.manager.votingapi.resultfunction');
foreach ($votes as $vote) {
foreach ($votes as $vote) {
$manager->recalculateResults($vote['entity_type'], $vote['entity_id'], $vote_type);
$this->voteResultFunctionManager->recalculateResults($vote['entity_type'], $vote['entity_id'], $vote_type);
}
}
$this->logger->success($message);
$this->logger->success($message);
@@ -115,7 +178,7 @@ class VotingApiDrushCommands extends DrushCommands {
@@ -115,7 +178,7 @@ class VotingApiDrushCommands extends DrushCommands {
*
*
* @param string $entity_type
* @param string $entity_type
* The type of entity whose voting data should be flushed.
* The type of entity whose voting data should be flushed.
* @param ?string $entity_id
* @param string $entity_id
* The ID of the entity.
* The ID of the entity.
*
*
* @command voting:flush
* @command voting:flush
@@ -129,10 +192,10 @@ class VotingApiDrushCommands extends DrushCommands {
@@ -129,10 +192,10 @@ class VotingApiDrushCommands extends DrushCommands {
#[CLI\Argument(name: 'entity_type', description: 'The type of entity whose voting data should be flushed.')]
#[CLI\Argument(name: 'entity_type', description: 'The type of entity whose voting data should be flushed.')]
#[CLI\Argument(name: 'entity_id', description: 'The ID of the entity.')]
#[CLI\Argument(name: 'entity_id', description: 'The ID of the entity.')]
#[CLI\Usage(name: "drush voting:flush [entity_type | 'all']", description: 'Deletes all existing voting data for the specified entity type.')]
#[CLI\Usage(name: "drush voting:flush [entity_type | 'all']", description: 'Deletes all existing voting data for the specified entity type.')]
public function flush(string $entity_type = 'all', ?string $entity_id): void {
public function flush(string $entity_type = 'all', string $entity_id = NULL): void {
if ($this->io()->confirm(dt("Delete @type voting data?", ['@type' => $entity_type]))) {
if ($this->io()->confirm(dt("Delete @type voting data?", ['@type' => $entity_type]))) {
$cache = \Drupal::database()->delete('votingapi_result');
$cache = $this->database->delete('votingapi_result');
$votes = \Drupal::database()->delete('votingapi_vote');
$votes = $this->database->delete('votingapi_vote');
if ($entity_type !== 'all') {
if ($entity_type !== 'all') {
$cache->condition('entity_type', $entity_type);
$cache->condition('entity_type', $entity_type);
@@ -168,18 +231,21 @@ class VotingApiDrushCommands extends DrushCommands {
@@ -168,18 +231,21 @@ class VotingApiDrushCommands extends DrushCommands {
'kill_votes' => FALSE,
'kill_votes' => FALSE,
];
];
if (!empty($options['kill_votes'])) {
if (!empty($options['kill_votes'])) {
$cache = \Drupal::database()->delete('votingapi_result')
$this->database->delete('votingapi_result')
->condition('entity_type', $entity_type)
->condition('entity_type', $entity_type)
->execute();
->execute();
$votes = \Drupal::database()->delete('votingapi_vote')
$this->database->delete('votingapi_vote')
->condition('entity_type', $entity_type)
->condition('entity_type', $entity_type)
->execute();
->execute();
}
}
$uids = \Drupal::entityQuery('user')
/** @var \Drupal\Core\Entity\Query\QueryInterface $user_query */
 
$user_query = $this->entityTypeManager->getStorage('user')->getQuery();
 
$uids = $user_query
->accessCheck(TRUE)
->accessCheck(TRUE)
->condition('status', 1)
->condition('status', 1)
->execute();
->execute();
$query = \Drupal::database()->select($entity_type, 'e')
/** @var \Drupal\Core\Database\Query\SelectInterface $query */
 
$query = $this->database->select($entity_type, 'e')
->fields('e', ['nid']);
->fields('e', ['nid']);
if ($entity_type == 'node' && !empty($options['types'])) {
if ($entity_type == 'node' && !empty($options['types'])) {
$query->condition('e.type', $options['types'], 'IN');
$query->condition('e.type', $options['types'], 'IN');
@@ -195,7 +261,7 @@ class VotingApiDrushCommands extends DrushCommands {
@@ -195,7 +261,7 @@ class VotingApiDrushCommands extends DrushCommands {
*/
*/
protected function castVotes(string $entity_type, string $entity_id, int $timestamp = 0, array $uids = [], string $style = 'percent'): void {
protected function castVotes(string $entity_type, string $entity_id, int $timestamp = 0, array $uids = [], string $style = 'percent'): void {
foreach ($uids as $uid) {
foreach ($uids as $uid) {
$request_time = \Drupal::time()->getRequestTime();
$request_time = $this->timeService->getRequestTime();
$value = $style === 'points' ? rand(0, 1) ? 1 : -1 : mt_rand(1, 5) * 20;
$value = $style === 'points' ? rand(0, 1) ? 1 : -1 : mt_rand(1, 5) * 20;
$vote = Vote::create(['type' => 'vote']);
$vote = Vote::create(['type' => 'vote']);
$vote->setVotedEntityId($entity_id);
$vote->setVotedEntityId($entity_id);
Loading