Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
CrmCommands
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 3
12
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 create
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 recalculateStatistics
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Drupal\crm\Drush\Commands;
6
7use Drupal\crm\Service\RelationshipStatisticsInterface;
8use Drush\Attributes as CLI;
9use Drush\Commands\DrushCommands;
10use Symfony\Component\DependencyInjection\ContainerInterface;
11
12/**
13 * CRM Drush commands.
14 */
15class CrmCommands extends DrushCommands {
16
17  /**
18   * The relationship statistics service.
19   *
20   * @var \Drupal\crm\Service\RelationshipStatisticsInterface
21   */
22  protected RelationshipStatisticsInterface $statisticsService;
23
24  /**
25   * Constructs a CrmCommands object.
26   *
27   * @param \Drupal\crm\Service\RelationshipStatisticsInterface $statistics_service
28   *   The relationship statistics service.
29   */
30  public function __construct(RelationshipStatisticsInterface $statistics_service) {
31    parent::__construct();
32    $this->statisticsService = $statistics_service;
33  }
34
35  /**
36   * {@inheritdoc}
37   */
38  public static function create(ContainerInterface $container): self {
39    return new static(
40      $container->get('crm.relationship_statistics'),
41    );
42  }
43
44  /**
45   * Recalculates relationship statistics for all contacts.
46   */
47  #[CLI\Command(name: 'crm:recalculate-statistics', aliases: ['crm-rs'])]
48  #[CLI\Option(name: 'batch-size', description: 'Number of contacts to process per batch.')]
49  #[CLI\Usage(name: 'crm:recalculate-statistics', description: 'Recalculate all contact relationship statistics.')]
50  #[CLI\Usage(name: 'crm:recalculate-statistics --batch-size=50', description: 'Recalculate with smaller batch size.')]
51  public function recalculateStatistics(array $options = ['batch-size' => 100]): void {
52    $batch_size = (int) $options['batch-size'];
53
54    $this->logger()->notice('Starting relationship statistics recalculation...');
55
56    $start_time = microtime(TRUE);
57    $total_processed = $this->statisticsService->recalculateAll($batch_size);
58    $elapsed_time = round(microtime(TRUE) - $start_time, 2);
59
60    $this->logger()->success(dt('Recalculated statistics for @count contacts in @time seconds.', [
61      '@count' => $total_processed,
62      '@time' => $elapsed_time,
63    ]));
64  }
65
66}