Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
51 / 51
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
RebuildLocationForm
100.00% covered (success)
100.00%
51 / 51
100.00% covered (success)
100.00%
9 / 9
12
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 create
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 buildForm
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
1
 submitForm
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
1 / 1
2
 batch
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 batchFinished
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
2
 getFormId
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCancelUrl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getQuestion
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Drupal\visitors_geoip\Form;
4
5use Drupal\Core\Form\ConfirmFormBase;
6use Drupal\Core\Form\FormStateInterface;
7use Drupal\Core\Url;
8use Drupal\visitors_geoip\VisitorsGeoIpRebuildLocationInterface;
9use Symfony\Component\DependencyInjection\ContainerInterface;
10
11/**
12 * Form to rebuild routes with batch.
13 */
14final class RebuildLocationForm extends ConfirmFormBase {
15
16  /**
17   * The route rebuild service.
18   *
19   * @var \Drupal\visitors_geoip\VisitorsGeoIpRebuildLocationInterface
20   */
21  protected $service;
22
23  /**
24   * Constructs a new RebuildLocationForm.
25   *
26   * @param \Drupal\visitors_geoip\VisitorsGeoIpRebuildLocationInterface $service
27   *   The route rebuild service.
28   */
29  public function __construct(VisitorsGeoIpRebuildLocationInterface $service) {
30    $this->service = $service;
31  }
32
33  /**
34   * {@inheritdoc}
35   */
36  public static function create(ContainerInterface $container) {
37    return new self(
38      $container->get('visitors_geoip.rebuild.location')
39    );
40  }
41
42  /**
43   * {@inheritdoc}
44   */
45  public function buildForm(array $form, FormStateInterface $form_state) {
46    $form['slow'] = [
47      '#prefix' => '<div class="container">',
48      '#markup' => $this->t('The visitors log is missing location for some visits. You can rebuild the location with the ip address.'),
49      '#suffix' => '</div>',
50    ];
51    $form['drush'] = [
52      '#prefix' => '<div class="container">',
53      '#markup' => $this->t('Available drush command: <code>drush visitors:rebuild:location</code>'),
54      '#suffix' => '</div>',
55    ];
56
57    return parent::buildForm($form, $form_state);
58  }
59
60  /**
61   * {@inheritdoc}
62   */
63  public function submitForm(array &$form, FormStateInterface $form_state) {
64    $records = $this->service->getLocations();
65
66    $operations = [];
67    foreach ($records as $record) {
68      $operations[] = [
69        'Drupal\visitors_geoip\Form\RebuildLocationForm::batch',
70        [$record],
71      ];
72    }
73    $batch = [
74      'operations' => $operations,
75      'finished' => 'Drupal\visitors_geoip\Form\RebuildLocationForm::batchFinished',
76      'title' => $this->t('Rebuilding visitors locations.'),
77      'init_message' => $this->t('Fetching locations.'),
78      'progress_message' => $this->t('Processed @current out of @total.'),
79      'error_message' => $this->t('Location rebuild has encountered an error.'),
80    ];
81
82    \batch_set($batch);
83  }
84
85  /**
86   * Batch callback.
87   *
88   * @param string $ip_address
89   *   The ip_address to rebuild.
90   * @param array $context
91   *   The batch context.
92   */
93  public static function batch(string $ip_address, array &$context) {
94    $service = \Drupal::service('visitors_geoip.rebuild.location');
95    $result = $service->rebuild($ip_address);
96    if ($result) {
97      $context['results'][] = $ip_address;
98    }
99
100    $context['finished'] = 1;
101  }
102
103  /**
104   * Batch finished callback.
105   *
106   * @param bool $success
107   *   If the batch was successful.
108   * @param array $results
109   *   The results of the batch.
110   * @param array $operations
111   *   The operations of the batch.
112   */
113  public static function batchFinished(bool $success, array $results, array $operations) {
114    if ($success) {
115      // Here we do something meaningful with the results.
116      $message = t('@count items successfully processed:', [
117        '@count' => count($results),
118      ]);
119      \Drupal::messenger()->addMessage($message);
120      \Drupal::state()->delete('visitors_geoip.rebuild.location');
121    }
122    else {
123      // An error occurred.
124      // $operations contains the operations that remained unprocessed.
125      $error_operation = reset($operations);
126      $message = t('An error occurred while processing %error_operation with arguments: @arguments', [
127        '%error_operation' => $error_operation[0],
128        '@arguments' => print_r($error_operation[1], TRUE),
129      ]);
130      \Drupal::messenger()->addMessage($message, 'error');
131    }
132  }
133
134  /**
135   * {@inheritdoc}
136   */
137  public function getFormId() : string {
138    return "confirm_visitor_rebuild_location_form";
139  }
140
141  /**
142   * {@inheritdoc}
143   */
144  public function getCancelUrl() {
145    return new Url('visitors_geoip.settings');
146  }
147
148  /**
149   * {@inheritdoc}
150   */
151  public function getQuestion() {
152    return $this->t('Do you want to rebuild missing locations?');
153  }
154
155}