Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
56 / 56
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
RebuildCommands
100.00% covered (success)
100.00%
56 / 56
100.00% covered (success)
100.00%
4 / 4
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 routes
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
2
 addresses
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
2
 device
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace Drupal\visitors\Commands;
4
5use Drupal\Core\State\StateInterface;
6use Drupal\visitors\VisitorsDeviceInterface;
7use Drupal\visitors\VisitorsRebuildIpAddressInterface;
8use Drupal\visitors\VisitorsRebuildRouteInterface;
9use Drush\Commands\DrushCommands;
10use Symfony\Component\Console\Helper\ProgressBar;
11
12/**
13 * Defines a Drush command.
14 */
15class RebuildCommands extends DrushCommands {
16
17  /**
18   * The state service.
19   *
20   * @var \Drupal\Core\State\StateInterface
21   */
22  protected $state;
23
24  /**
25   * The visitors rebuild route service.
26   *
27   * @var \Drupal\visitors\VisitorsRebuildRouteInterface
28   */
29  protected $route;
30
31  /**
32   * The visitors rebuild ip address service.
33   *
34   * @var \Drupal\visitors\VisitorsRebuildIpAddressInterface
35   */
36  protected $address;
37
38  /**
39   * The visitors device service.
40   *
41   * @var \Drupal\visitors\VisitorsDeviceInterface
42   */
43  protected $device;
44
45  /**
46   * Drush commands for rebuilding logs.
47   *
48   * @param \Drupal\Core\State\StateInterface $state
49   *   The state service.
50   * @param \Drupal\visitors\VisitorsRebuildRouteInterface $route
51   *   The visitors rebuild route service.
52   * @param \Drupal\visitors\VisitorsRebuildIpAddressInterface $ip_address
53   *   The visitors rebuild ip address service.
54   * @param \Drupal\visitors\VisitorsDeviceInterface $device
55   *   The visitors device service.
56   */
57  public function __construct(
58    StateInterface $state,
59    VisitorsRebuildRouteInterface $route,
60    VisitorsRebuildIpAddressInterface $ip_address,
61    VisitorsDeviceInterface $device,
62  ) {
63    parent::__construct();
64
65    $this->state = $state;
66    $this->route = $route;
67    $this->address = $ip_address;
68    $this->device = $device;
69  }
70
71  /**
72   * Regenerates routes from path.
73   *
74   * @command visitors:rebuild:route
75   * @aliases visitors-rebuild-route
76   *
77   * @usage drush visitors:rebuild:route
78   *  Generates routes from the visitors_path.
79   */
80  public function routes() {
81    // Get the Symfony Console output interface.
82    $output = $this->output();
83
84    $records = $this->route->getPaths();
85    $total = count($records);
86
87    $output->writeLn("There are $total paths to process.");
88    $progressBar = new ProgressBar($output, $total);
89    $progressBar->setFormat('debug');
90    $progressBar->start();
91
92    do {
93      $progressBar->advance();
94      $record = array_pop($records);
95      if (empty($record)) {
96        continue;
97      }
98
99      $this->route->rebuild($record->visitors_path);
100
101    } while (count($records));
102
103    // Finish the progress bar.
104    $progressBar->finish();
105    // Add a new line after the progress bar.
106    $output->writeln('');
107
108    $this->state->delete('visitors.rebuild.route');
109
110    // Output a completion message.
111    $output->writeln('Task completed!');
112  }
113
114  /**
115   * Converts IP Address to support IPv6.
116   *
117   * @command visitors:rebuild:ip-address
118   * @aliases visitors-rebuild-ip-address
119   *
120   * @usage drush visitors:rebuild:ip-address
121   *  Converts integers IP addresses to strings.
122   */
123  public function addresses() {
124
125    $records = $this->address->getIpAddresses();
126    $total = count($records);
127
128    // Get the Symfony Console output interface.
129    $output = $this->output();
130    $output->writeLn("There are $total ip addresses to process.");
131    $progressBar = new ProgressBar($output, $total);
132    $progressBar->setFormat('debug');
133    $progressBar->start();
134
135    do {
136      $progressBar->advance();
137      $record = array_pop($records);
138      if (empty($record)) {
139        continue;
140      }
141
142      $this->address->rebuild($record->visitors_ip);
143
144    } while (count($records));
145
146    // Finish the progress bar.
147    $progressBar->finish();
148    // Add a new line after the progress bar.
149    $output->writeln('');
150
151    $this->state->delete('visitors.rebuild.ip_address');
152
153    // Output a completion message.
154    $output->writeln('Task completed!');
155  }
156
157  /**
158   * Rebuilds the device information.
159   *
160   * @command visitors:rebuild:device
161   * @aliases visitors-rebuild-device
162   *
163   * @usage drush visitors:rebuild:device
164   *  Rebuilds the device information.
165   */
166  public function device() {
167    // Get the Symfony Console output interface.
168    $output = $this->output();
169    if (!$this->device->hasLibrary()) {
170      $output->writeln('DeviceDetector is not installed. Did you use composer to install Visitors?');
171      return NULL;
172    }
173
174    $records = $this->device->getUniqueUserAgents();
175    $total = count($records);
176
177    $output->writeLn("There are $total user agents to process.");
178    $progressBar = new ProgressBar($output, $total);
179    $progressBar->setFormat('debug');
180    $progressBar->start();
181
182    do {
183      $progressBar->advance();
184      $record = array_pop($records);
185      if (empty($record)) {
186        continue;
187      }
188
189      $this->device->bulkUpdate($record);
190
191    } while (count($records));
192
193    // Finish the progress bar.
194    $progressBar->finish();
195    // Add a new line after the progress bar.
196    $output->writeln('');
197
198    $this->state->delete('visitors.rebuild.device');
199
200    // Output a completion message.
201    $output->writeln('Task completed!');
202  }
203
204}