Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
UpgradeForm
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
6 / 6
11
100.00% covered (success)
100.00%
1 / 1
 getFormId
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
 getDescription
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
 submitForm
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 upgrade
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
5
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\StringTranslation\TranslatableMarkup;
10use Drupal\Core\Url;
11
12/**
13 * Upgrade form.
14 *
15 * From 8.x-2.x to 3.0.x.
16 */
17final class UpgradeForm extends ConfirmFormBase {
18
19  /**
20   * {@inheritdoc}
21   */
22  public function getFormId(): string {
23    return 'visitors_upgrade';
24  }
25
26  /**
27   * {@inheritdoc}
28   */
29  public function getQuestion(): TranslatableMarkup {
30    return $this->t('Migrate Visitors 8.x-2.x to 3.0.x');
31  }
32
33  /**
34   * {@inheritdoc}
35   */
36  public function getDescription(): TranslatableMarkup {
37    return $this->t('This action will upgrade the data.');
38  }
39
40  /**
41   * {@inheritdoc}
42   */
43  public function getCancelUrl(): Url {
44    return new Url('visitors.settings');
45  }
46
47  /**
48   * {@inheritdoc}
49   */
50  public function submitForm(array &$form, FormStateInterface $form_state) {
51
52    $operations = [
53      [['\Drupal\visitors\Form\UpgradeForm', 'upgrade'], []],
54    ];
55    $batch = [
56      'title' => $this->t('Upgrading data'),
57      'operations' => $operations,
58      'finished' => '\Drupal\visitors\Form\UpgradeForm::finished',
59    ];
60    if (function_exists('batch_set')) {
61      \batch_set($batch);
62    }
63
64    return 1;
65  }
66
67  /**
68   * Batch method for upgrading.
69   *
70   * @param array $context
71   *   The batch context.
72   */
73  public static function upgrade(&$context) {
74
75    $service = \Drupal::service('visitors.upgrade');
76    if (!isset($context['sandbox']['max'])) {
77      $context['sandbox']['max'] = $service->getTotal();
78      $context['sandbox']['current'] = 0;
79      $context['finished'] = ($context['sandbox']['max'] == $context['sandbox']['current']) ? 1 : $context['sandbox']['current'] / $context['sandbox']['max'];
80
81      return;
82    }
83
84    $batch_size = 50;
85    $rows = $service->getVisitorsRows($batch_size);
86
87    foreach ($rows as $row) {
88      $service->upgradeRow((array) $row);
89      $context['sandbox']['current'] += 1;
90    }
91
92    $context['finished'] = ($context['sandbox']['max'] == $context['sandbox']['current']) ? 1 : $context['sandbox']['current'] / $context['sandbox']['max'];
93  }
94
95}