-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidateXmlCommand.php
More file actions
180 lines (157 loc) · 5.87 KB
/
Copy pathValidateXmlCommand.php
File metadata and controls
180 lines (157 loc) · 5.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
declare(strict_types=1);
namespace Drupal\os2forms_fordelingskomponent\Drush\Commands;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Url;
use Drupal\os2forms_fordelingskomponent\Helper\FordelingskomponentHelper;
use Drupal\os2forms_fordelingskomponent\Helper\WebformHelperSF2900;
use Drupal\os2forms_fordelingskomponent\Plugin\WebformHandler\WebformHandlerSF2900;
use Drupal\os2forms_fordelingskomponent\Settings;
use Drupal\webform\WebformEntityStorageInterface;
use Drupal\webform\WebformInterface;
use Drupal\webform\WebformSubmissionStorageInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
// phpcs:disable Drupal.Commenting.ClassComment.Missing
#[AsCommand(
name: 'os2forms-fordelingskomponent:validate-xml',
description: 'Validate generated XML on submissions',
)]
final class ValidateXmlCommand extends AbstractCommand {
/**
* The webform storage.
*/
private readonly WebformEntityStorageInterface $webformStorage;
/**
* The submission starage.
*/
private readonly WebformSubmissionStorageInterface $submissionStorage;
public function __construct(
FordelingskomponentHelper $helper,
Settings $settings,
EntityTypeManagerInterface $entityTypeManager,
private readonly WebformHelperSF2900 $webformHelper,
) {
parent::__construct($helper, $settings);
$this->webformStorage = $entityTypeManager->getStorage('webform');
$this->submissionStorage = $entityTypeManager->getStorage('webform_submission');
}
/**
* {@inheritdoc}
*
* @see https://www.drush.org/13.x/commands/
*/
protected function configure(): void {
$this
->addArgument('webform-id', InputArgument::REQUIRED, 'Webform ID')
->addArgument('handler-id', InputArgument::REQUIRED, 'Handler ID')
->addOption('show-xml', NULL, InputOption::VALUE_NONE, 'Show XML')
->addOption('show-render-context', NULL, InputOption::VALUE_NONE, 'Show render context')
->addOption('break-on-error', NULL, InputOption::VALUE_OPTIONAL, 'Break on error. If set, terminate after first error.', FALSE);
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
// I long for "invokable commands"
// (https://symfony.com/doc/7.4/console.html#creating-a-command) ...
$webformId = $input->getArgument('webform-id');
$handlerId = $input->getArgument('handler-id');
$showXml = (bool) $input->getOption('show-xml');
$showRenderContext = (bool) $input->getOption('show-render-context');
$breakOnError = filter_var($input->getOption('break-on-error') ?? TRUE, FILTER_VALIDATE_BOOLEAN);
$io = new SymfonyStyle($input, $output);
$webform = $this->loadWebform($webformId);
$handler = $this->getHandler($handlerId, $webform);
$submissions = $this->loadSubmissions($webform);
if (0 === count($submissions)) {
$io->warning(sprintf('No submissions on form %s', $webform->label()));
}
else {
foreach ($submissions as $submission) {
$preview = $this->webformHelper->renderPreview($handler, $submission);
$previewUrl = Url::fromRoute('os2forms_fordelingskomponent.fordelingskomponent_distribution_object.preview', [
'webform' => $webform->id(),
'webform_handler' => $handler->getHandlerId(),
'webform_submission' => $submission->id(),
])
->setAbsolute()
->toString(TRUE)->getGeneratedUrl();
$hasErrors = count($preview['exceptions']) > 0;
if ($hasErrors) {
$io->error([
$submission->label(),
$previewUrl,
...array_map(static fn (\Exception $exception) => $exception->getMessage(), $preview['exceptions']),
]);
}
else {
$io->success([
$submission->label(),
$previewUrl,
]);
}
if ($showXml) {
if (NULL === $preview['xml']->rendered) {
$io->warning('Cannot render XML');
}
else {
$io->writeln($preview['xml']->rendered);
}
}
if ($showRenderContext) {
$io->writeln(json_encode($preview['xml']->context, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
}
if ($hasErrors) {
if ($breakOnError) {
return self::FAILURE;
}
continue;
}
}
}
return self::SUCCESS;
}
/**
* Load webform.
*/
private function loadWebform(string $id): WebformInterface {
/** @var ?WebformInterface $webform */
$webform = $this->webformStorage->load($id);
if (NULL === $webform) {
throw new InvalidArgumentException(sprintf('Cannot load webform %s', $id));
}
return $webform;
}
/**
* Get webform handler.
*/
private function getHandler(string $handlerId, WebformInterface $webform): WebformHandlerSF2900 {
try {
$handler = $webform->getHandler($handlerId);
}
catch (\Exception $e) {
throw new InvalidArgumentException(sprintf('Cannot load handler: %s', $handlerId), previous: $e);
}
if (!$handler instanceof WebformHandlerSF2900) {
throw new InvalidArgumentException(sprintf('Handler must be an instance of %s; found %s', WebformHandlerSF2900::class, $handler::class));
}
return $handler;
}
/**
* Load webform submissions.
*
* @return \Drupal\webform\WebformSubmissionInterface[]
* The submissions.
*/
private function loadSubmissions(WebformInterface $webform): array {
return $this->submissionStorage->loadByProperties([
'webform_id' => $webform->id(),
]);
}
}