Coverage index

src/Commands/VisitorsCommands.php

Lines: 100.00%

LineHitsSource
1
<?php
2
3
namespace Drupal\visitors\Commands;
4
5
use Drupal\Core\Database\Connection;
6
use Drupal\visitors\VisitorsTrackerInterface;
7
use Drupal\visitors\VisitorsUpgradeInterface;
8
use Drush\Commands\DrushCommands;
9
use Symfony\Component\Console\Helper\ProgressBar;
10
11
/**
12
 * Defines a Drush command.
13
 */
14
class VisitorsCommands extends DrushCommands {
15
16
  /**
17
   * The database connection.
18
   *
19
   * @var \Drupal\Core\Database\Connection
20
   */
21
  protected $database;
22
23
  /**
24
   * The visitors tracker.
25
   *
26
   * @var \Drupal\visitors\VisitorsTrackerInterface
27
   */
28
  protected $tracker;
29
30
  /**
31
   * The visitors upgrade.
32
   *
33
   * @var \Drupal\visitors\VisitorsUpgradeInterface
34
   */
35
  protected $upgrade;
36
37
  /**
38
   * Drush commands for rebuilding logs.
39
   *
40
   * @param \Drupal\Core\Database\Connection $database
41
   *   The database connection.
42
   * @param \Drupal\visitors\VisitorsTrackerInterface $tracker
43
   *   The visitors tracker.
44
   * @param \Drupal\visitors\VisitorsUpgradeInterface $upgrade
45
   *   The visitors upgrade.
46
   */
47
  public function __construct(
48
    Connection $database,
49
    VisitorsTrackerInterface $tracker,
50
    VisitorsUpgradeInterface $upgrade,
51
  ) {
522
    parent::__construct();
53
542
    $this->database = $database;
552
    $this->tracker = $tracker;
562
    $this->upgrade = $upgrade;
57
  }
58
59
  /**
60
   * Regenerates routes from path.
61
   *
62
   * @command visitors:upgrade
63
   * @aliases visitors-upgrade
64
   *
65
   * @usage drush visitors:upgrade
66
   *  Upgrade data from 8.x-2.x to 3.0.x.
67
   */
68
  public function upgrade() {
69
701
    $total = $this->getTotal();
71
72
    // Get the Symfony Console output interface.
731
    $output = $this->output();
74
751
    $progressBar = new ProgressBar($output, $total);
761
    $progressBar->setFormat('debug');
771
    $progressBar->start();
78
79
    do {
801
      $progressBar->advance();
811
      $this->doLog();
821
      $total -= 1;
831
    } while ($total > 0);
84
85
    // Finish the progress bar.
861
    $progressBar->finish();
87
    // Add a new line after the progress bar.
881
    $output->writeln('');
89
90
    // Output a completion message.
911
    $output->writeln('Task completed!');
92
  }
93
94
  /**
95
   * Migrate the page views.
96
   */
97
  protected function doLog() {
981
    $row = $this->getVisitorsRow();
991
    $id = $row['visitors_id'];
1001
    $request_time = $row['visitors_date_time'];
101
1021
    $visit = $this->upgrade->doVisitRecord($row);
1031
    $log = $this->upgrade->doLogRecord($row);
104
1051
    $raw_uid = $log['uid'] ?? 0;
1061
    $anonymous = $raw_uid === 0 || $raw_uid === '0' || $raw_uid === '';
1071
    $visit['uid'] = ($anonymous || $raw_uid === NULL) ? NULL : (string) $raw_uid;
108
1091
    $visit_id = $this->tracker->getVisitId($visit, $request_time);
1101
    $log['visit_id'] = $visit_id;
1111
    $event_id = $this->tracker->writeEvent($log);
112
1131
    $this->tracker->updateVisit($visit_id, $event_id, $request_time, $visit['uid']);
114
1151
    $this->upgrade->deleteVisitorsRow($id);
116
  }
117
118
  /**
119
   * Get the next visitors row.
120
   */
121
  protected function getVisitorsRow() {
1221
    static $ids = [];
123
1241
    if (empty($ids)) {
1251
      $ids = $this->upgrade->getVisitorsRows(10000);
126
    }
127
1281
    return (array) array_shift($ids);
129
  }
130
131
  /**
132
   * The total records to convert.
133
   */
134
  protected function getTotal() {
1351
    $select = $this->database->select('visitors', 'v');
1361
    $select->fields('v')
1371
      ->isNotNull('v.visitor_id')
1381
      ->countQuery();
139
1401
    $count = $select->execute()->fetchField();
141
1421
    return (int) $count;
143
  }
144
145
}