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