forked from OS2Forms/os2forms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebformHelperSF1601.php
More file actions
351 lines (307 loc) · 12.7 KB
/
WebformHelperSF1601.php
File metadata and controls
351 lines (307 loc) · 12.7 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php
namespace Drupal\os2forms_digital_post\Helper;
use Drupal\os2web_datalookup\LookupResult\CompanyLookupResult;
use Drupal\os2web_datalookup\LookupResult\CprLookupResult;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\advancedqueue\Entity\QueueInterface;
use Drupal\advancedqueue\Job;
use Drupal\advancedqueue\JobResult;
use Drupal\os2forms_digital_post\EventSubscriber\Os2formsDigitalPostSubscriber;
use Drupal\os2forms_digital_post\Exception\InvalidRecipientIdentifierElementException;
use Drupal\os2forms_digital_post\Exception\RuntimeException;
use Drupal\os2forms_digital_post\Exception\SubmissionNotFoundException;
use Drupal\os2forms_digital_post\Plugin\AdvancedQueue\JobType\SendDigitalPostSF1601;
use Drupal\os2forms_digital_post\Plugin\WebformHandler\WebformHandlerSF1601;
use Drupal\os2web_datalookup\Plugin\DataLookupManager;
use Drupal\os2web_datalookup\Plugin\os2web\DataLookup\DataLookupCompanyInterface;
use Drupal\os2web_datalookup\Plugin\os2web\DataLookup\DataLookupCprInterface;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\webform\WebformSubmissionStorageInterface;
use ItkDev\Serviceplatformen\Service\SF1601\SF1601;
use Psr\Log\LoggerInterface;
use Psr\Log\LoggerTrait;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
/**
* Webform helper.
*/
final class WebformHelperSF1601 implements LoggerInterface {
use LoggerTrait;
public const RECIPIENT_IDENTIFIER_TYPE = 'recipient_identifier_type';
public const RECIPIENT_IDENTIFIER = 'recipient_identifier';
/**
* The webform submission storage.
*
* @var \Drupal\webform\WebformSubmissionStorageInterface
*/
protected WebformSubmissionStorageInterface $webformSubmissionStorage;
/**
* The queue storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected EntityStorageInterface $queueStorage;
/**
* Constructor.
*/
public function __construct(
private readonly Settings $settings,
EntityTypeManagerInterface $entityTypeManager,
#[Autowire(service: 'plugin.manager.os2web_datalookup')]
private readonly DataLookupManager $dataLookupManager,
private readonly MeMoHelper $meMoHelper,
private readonly ForsendelseHelper $forsendelseHelper,
private readonly BeskedfordelerHelper $beskedfordelerHelper,
#[Autowire(service: 'logger.channel.os2forms_digital_post')]
private readonly LoggerChannelInterface $logger,
#[Autowire(service: 'logger.channel.os2forms_digital_post_submission')]
private readonly LoggerChannelInterface $submissionLogger,
private readonly DigitalPostHelper $digitalPostHelper,
private readonly Os2formsDigitalPostSubscriber $digitalPostSubscriber,
) {
$this->webformSubmissionStorage = $entityTypeManager->getStorage('webform_submission');
$this->queueStorage = $entityTypeManager->getStorage('advancedqueue_queue');
}
/**
* Send digital post.
*
* @param \Drupal\webform\WebformSubmissionInterface $submission
* The submission.
* @param array $handlerSettings
* The Handler settings.
* @param array $submissionData
* Submission data. Only for overriding during testing and development.
*
* @return array
* [The response, The kombi post message].
*
* @phpstan-param array<string, mixed> $handlerSettings
* @phpstan-param array<string, mixed> $submissionData
* @phpstan-return array<int, mixed>
*/
public function sendDigitalPost(WebformSubmissionInterface $submission, array $handlerSettings, array $submissionData = []): array {
$submissionData = $submissionData + $submission->getData();
$handlerMessageSettings = $handlerSettings[WebformHandlerSF1601::MEMO_MESSAGE];
$recipientIdentifierKey = $handlerMessageSettings[WebformHandlerSF1601::RECIPIENT_ELEMENT] ?? NULL;
if (NULL === $recipientIdentifierKey) {
$message = 'Recipient identifier element (key: @element_key) not found in submission';
$context = [
'@element_key' => WebformHandlerSF1601::RECIPIENT_ELEMENT,
];
$this->error($message, $context);
throw new InvalidRecipientIdentifierElementException(str_replace(array_keys($context), array_values($context),
$message));
}
$recipientIdentifier = $submissionData[$recipientIdentifierKey] ?? NULL;
// Fix if os2forms_person_lookup (cpr & name validation) element is used.
if (is_array($recipientIdentifier)) {
// Example:
// [
// 'cpr_number' => 1234567890,
// 'name' => Eksempel Eksempelsen,
// ].
$recipientIdentifier = $recipientIdentifier['cpr_number'] ?? NULL;
}
if (NULL === $recipientIdentifier) {
$message = 'Recipient identifier element (key: @element_key) not found in submission';
$context = [
'@element_key' => WebformHandlerSF1601::RECIPIENT_ELEMENT,
];
$this->error($message, $context);
throw new InvalidRecipientIdentifierElementException(str_replace(array_keys($context), array_values($context),
$message));
}
// Remove all non-digits from recipient identifier.
$recipientIdentifier = preg_replace('/[^\d]+/', '', $recipientIdentifier);
/** @var \Drupal\os2web_datalookup\LookupResult\CprLookupResult|\Drupal\os2web_datalookup\LookupResult\CompanyLookupResult|null $lookupResult */
$lookupResult = NULL;
if (preg_match('/^\d{8}$/', $recipientIdentifier)) {
$instance = $this->dataLookupManager->createDefaultInstanceByGroup('cvr_lookup');
if (!($instance instanceof DataLookupCompanyInterface)) {
throw new RuntimeException('Cannot get CVR data lookup instance');
}
$lookupResult = $instance->lookup($recipientIdentifier);
if (!$lookupResult->isSuccessful()) {
throw new RuntimeException('Cannot validate recipient CVR');
}
$recipientIdentifierType = 'CVR';
}
else {
$instance = $this->dataLookupManager->createDefaultInstanceByGroup('cpr_lookup');
if (!($instance instanceof DataLookupCprInterface)) {
throw new RuntimeException('Cannot get CPR data lookup instance');
}
$lookupResult = $instance->lookup($recipientIdentifier);
if (!$lookupResult->isSuccessful()) {
throw new RuntimeException('Cannot validate recipient CPR');
}
$recipientIdentifierType = 'CPR';
}
$this->digitalPostSubscriber->setDigitalPostContext($submission, $lookupResult);
$senderSettings = $this->settings->getSender();
$messageOptions = [
self::RECIPIENT_IDENTIFIER_TYPE => $recipientIdentifierType,
self::RECIPIENT_IDENTIFIER => $recipientIdentifier,
Settings::SENDER_IDENTIFIER_TYPE => $senderSettings[Settings::SENDER_IDENTIFIER_TYPE],
Settings::SENDER_IDENTIFIER => $senderSettings[Settings::SENDER_IDENTIFIER],
WebformHandlerSF1601::SENDER_LABEL => $handlerMessageSettings[WebformHandlerSF1601::SENDER_LABEL],
WebformHandlerSF1601::MESSAGE_HEADER_LABEL => $handlerMessageSettings[WebformHandlerSF1601::MESSAGE_HEADER_LABEL],
];
$message = $this->meMoHelper->buildWebformSubmissionMessage($submission, $messageOptions, $handlerSettings, $lookupResult);
$forsendelseOptions = [
self::RECIPIENT_IDENTIFIER_TYPE => $recipientIdentifierType,
self::RECIPIENT_IDENTIFIER => $recipientIdentifier,
Settings::SENDER_IDENTIFIER_TYPE => $senderSettings[Settings::SENDER_IDENTIFIER_TYPE],
Settings::SENDER_IDENTIFIER => $senderSettings[Settings::SENDER_IDENTIFIER],
Settings::FORSENDELSES_TYPE_IDENTIFIKATOR => $senderSettings[Settings::FORSENDELSES_TYPE_IDENTIFIKATOR],
WebformHandlerSF1601::SENDER_LABEL => $handlerMessageSettings[WebformHandlerSF1601::SENDER_LABEL],
WebformHandlerSF1601::MESSAGE_HEADER_LABEL => $handlerMessageSettings[WebformHandlerSF1601::MESSAGE_HEADER_LABEL],
];
$forsendelse = $this->forsendelseHelper->buildSubmissionForsendelse($submission, $forsendelseOptions, $handlerSettings, $lookupResult);
$type = $handlerMessageSettings[WebformHandlerSF1601::TYPE] ?? SF1601::TYPE_DIGITAL_POST;
return $this->digitalPostHelper->sendDigitalPost($type, $message, $forsendelse, $submission);
}
/**
* Load webform submission by id.
*/
public function loadSubmission(int $id): ?WebformSubmissionInterface {
return $this->webformSubmissionStorage->load($id);
}
/**
* Load queue.
*/
private function loadQueue(): QueueInterface {
$processingSettings = $this->settings->getProcessing();
/** @var \Drupal\advancedqueue\Entity\QueueInterface $queue */
$queue = $this->queueStorage->load($processingSettings['queue'] ?? 'os2forms_digital_post');
return $queue;
}
/**
* {@inheritdoc}
*
* @param mixed $level
* The level.
* @param string $message
* The message.
* @param array $context
* The context.
*
* @phpstan-param array<string, mixed> $context
*/
public function log($level, $message, array $context = []): void {
$this->logger->log($level, $message, $context);
// @see https://www.drupal.org/node/3020595
if (isset($context['webform_submission']) && $context['webform_submission'] instanceof WebformSubmissionInterface) {
$this->submissionLogger->log($level, $message, $context);
}
}
/**
* Create a job.
*
* @see self::processJob()
*
* @phpstan-param array<string, mixed> $handlerConfiguration
*/
public function createJob(WebformSubmissionInterface $webformSubmission, array $handlerConfiguration): ?Job {
$context = [
'handler_id' => 'os2forms_digital_post',
'webform_submission' => $webformSubmission,
];
try {
$job = Job::create(SendDigitalPostSF1601::class, [
'formId' => $webformSubmission->getWebform()->id(),
'submissionId' => $webformSubmission->id(),
'handlerConfiguration' => $handlerConfiguration,
]);
$queue = $this->loadQueue();
$queue->enqueueJob($job);
$context['@queue'] = $queue->id();
$this->notice('Job for sending digital post add to the queue @queue.', $context + [
'operation' => 'digital post queued for sending',
]);
return $job;
}
catch (\Exception $exception) {
$this->error('Error creating job for sending digital post.', $context + [
'operation' => 'digital post failed',
]);
return NULL;
}
}
/**
* Process a job.
*
* @see self::createJob()
*/
public function processJob(Job $job): JobResult {
$payload = $job->getPayload();
$context = [
'handler_id' => 'os2forms_digital_post',
'operation' => 'digital post send',
];
try {
$submissionId = $payload['submissionId'];
$submission = $this->loadSubmission($submissionId);
if (NULL === $submission) {
$message = 'Cannot load submission @submissionId';
$context = [
'@submissionId' => $submissionId,
];
$this->error($message, $context);
throw new SubmissionNotFoundException(str_replace(array_keys($context), array_values($context),
$message));
}
$context['webform_submission'] = $submission;
$this->sendDigitalPost($submission, $payload['handlerConfiguration']);
$this->notice('Digital post sent', $context);
return JobResult::success();
}
catch (\Exception $e) {
$this->error('Error: @message', $context + [
'@message' => $e->getMessage(),
]);
return JobResult::failure($e->getMessage());
}
}
/**
* Process Beskedfordeler data.
*
* @phpstan-param array<string, mixed> $data
*/
public function processBeskedfordelerData(int $submissionId, array $data): void {
$webformSubmission = $this->loadSubmission($submissionId);
if (NULL !== $webformSubmission) {
$context = [
'webform_submission' => $webformSubmission,
'handler_id' => 'os2forms_digital_post',
];
$status = $data['TransaktionsStatusKode'];
if (!empty($data['FejlDetaljer'])) {
$this->error('@status; @error_code: @error_text', $context + [
'operation' => 'digital post failed',
'@status' => $status,
'@error_code' => $data['FejlDetaljer']['FejlKode'],
'@error_text' => $data['FejlDetaljer']['FejlTekst'],
'data' => $data,
]);
}
else {
$this->info('@status', $context + [
'operation' => 'digital post success',
'@status' => $status,
]);
}
}
}
/**
* Proxy for BeskedfordelerHelper::deleteMessages().
*
* @see BeskedfordelerHelper::deleteMessages()
*
* @phpstan-param array<int, \Drupal\webform\WebformSubmissionInterface> $webformSubmissions
*/
public function deleteMessages(array $webformSubmissions): void {
$this->beskedfordelerHelper->deleteMessages($webformSubmissions);
}
}