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
RebuildIpAddressForm
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
3declare(strict_types=1);
4
5namespace Drupal\visitors\Form;
6
7use Drupal\Core\Form\ConfirmFormBase;
8use Drupal\Core\Form\FormStateInterface;
9use Drupal\Core\Url;
10use Drupal\visitors\VisitorsRebuildIpAddressInterface;
11use Symfony\Component\DependencyInjection\ContainerInterface;
12
13/**
14 * Form to convert IP address with bath.
15 */
16final class RebuildIpAddressForm extends ConfirmFormBase {
17  /**
18   * The IP address rebuild service.
19   *
20   * @var \Drupal\visitors\VisitorsRebuildIpAddressInterface
21   */
22  protected $service;
23
24  /**
25   * Constructs a new RebuildIpAddressForm.
26   *
27   * @param \Drupal\visitors\VisitorsRebuildIpAddressInterface $service
28   *   The route rebuild service.
29   */
30  public function __construct(VisitorsRebuildIpAddressInterface $service) {
31    $this->service = $service;
32  }
33
34  /**
35   * {@inheritdoc}
36   */
37  public static function create(ContainerInterface $container) {
38    return new self(
39      $container->get('visitors.rebuild.ip_address')
40    );
41  }
42
43  /**
44   * {@inheritdoc}
45   */
46  public function buildForm(array $form, FormStateInterface $form_state) {
47    $form['slow'] = [
48      '#prefix' => '<div class="container">',
49      '#markup' => $this->t('The visitors log may have some addresses in the legacy, IPv4 only, format. You can convert the IP address to the new format supporting IPv4 and IPv6.'),
50      '#suffix' => '</div>',
51    ];
52    $form['drush'] = [
53      '#prefix' => '<div class="container">',
54      '#markup' => $this->t('Available drush command: <code>drush visitors:rebuild:ip-address</code>'),
55      '#suffix' => '</div>',
56    ];
57
58    return parent::buildForm($form, $form_state);
59  }
60
61  /**
62   * {@inheritdoc}
63   */
64  public function submitForm(array &$form, FormStateInterface $form_state) {
65    $records = $this->service->getIpAddresses();
66    $operations = [];
67    foreach ($records as $record) {
68      $operations[] = [
69        'Drupal\visitors\Form\RebuildIpAddressForm::batch',
70        [$record->visitors_ip],
71      ];
72    }
73    $batch = [
74      'operations' => $operations,
75      'finished' => 'Drupal\visitors\Form\RebuildIpAddressForm::batchFinished',
76      'title' => $this->t('Rebuilding visitors IP addresses.'),
77      'init_message' => $this->t('Fetching ip addresses.'),
78      'progress_message' => $this->t('Processed @current out of @total.'),
79      'error_message' => $this->t('IP address 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.rebuild.ip_address');
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('@success items updates processed.', [
117        '@success' => count($results),
118      ]);
119      \Drupal::messenger()->addMessage($message);
120      \Drupal::state()->delete('visitors.rebuild.ip_address');
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_ip_address_form";
139  }
140
141  /**
142   * {@inheritdoc}
143   */
144  public function getCancelUrl() {
145    return new Url('visitors.settings');
146  }
147
148  /**
149   * {@inheritdoc}
150   */
151  public function getQuestion() {
152    return $this->t('Do you want to rebuild the IP Address?');
153  }
154
155}