Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
37 / 37 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
1 / 1 |
VisitorsCommands | |
100.00% |
37 / 37 |
|
100.00% |
5 / 5 |
7 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
upgrade | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
1 | |||
doLog | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
2 | |||
getVisitorsRow | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getTotal | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace Drupal\visitors\Commands; |
4 | |
5 | use Drush\Commands\DrushCommands; |
6 | use Drupal\Core\Database\Connection; |
7 | use Drupal\visitors\VisitorsTrackerInterface; |
8 | use Symfony\Component\Console\Helper\ProgressBar; |
9 | use Drupal\visitors\VisitorsUpgradeInterface; |
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 | ) { |
52 | parent::__construct(); |
53 | |
54 | $this->database = $database; |
55 | $this->tracker = $tracker; |
56 | $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 | |
70 | $total = $this->getTotal(); |
71 | |
72 | // Get the Symfony Console output interface. |
73 | $output = $this->output(); |
74 | |
75 | $progressBar = new ProgressBar($output, $total); |
76 | $progressBar->setFormat('debug'); |
77 | $progressBar->start(); |
78 | |
79 | do { |
80 | $progressBar->advance(); |
81 | $this->doLog(); |
82 | $total -= 1; |
83 | } while ($total > 0); |
84 | |
85 | // Finish the progress bar. |
86 | $progressBar->finish(); |
87 | // Add a new line after the progress bar. |
88 | $output->writeln(''); |
89 | |
90 | // Output a completion message. |
91 | $output->writeln('Task completed!'); |
92 | } |
93 | |
94 | /** |
95 | * Migrate the page views. |
96 | */ |
97 | protected function doLog() { |
98 | $row = $this->getVisitorsRow(); |
99 | $id = $row['visitors_id']; |
100 | $request_time = $row['visitors_date_time']; |
101 | |
102 | $visit = $this->upgrade->doVisitRecord($row); |
103 | $log = $this->upgrade->doLogRecord($row); |
104 | |
105 | $visit['uid'] = $log['uid'] == 0 ? NULL : $log['uid']; |
106 | |
107 | $visit_id = $this->tracker->getVisitId($visit, $request_time); |
108 | $log['visit_id'] = $visit_id; |
109 | $log_id = $this->tracker->writeLog($log); |
110 | |
111 | $this->tracker->updateVisit($visit_id, $log_id, $request_time, $visit['uid']); |
112 | |
113 | $this->upgrade->deleteVisitorsRow($id); |
114 | } |
115 | |
116 | /** |
117 | * Get the next visitors row. |
118 | */ |
119 | protected function getVisitorsRow() { |
120 | static $ids = []; |
121 | |
122 | if (empty($ids)) { |
123 | $ids = $this->upgrade->getVisitorsRows(10000); |
124 | } |
125 | |
126 | return (array) array_shift($ids); |
127 | } |
128 | |
129 | /** |
130 | * The total records to convert. |
131 | */ |
132 | protected function getTotal() { |
133 | $select = $this->database->select('visitors', 'v'); |
134 | $select->fields('v') |
135 | ->isNotNull('v.visitor_id') |
136 | ->countQuery(); |
137 | |
138 | $count = $select->execute()->fetchField(); |
139 | |
140 | return (int) $count; |
141 | } |
142 | |
143 | } |