Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 5b27ec6

Browse files
committed
DW-532: Add queue functionality
1 parent 0c98fd6 commit 5b27ec6

File tree

4 files changed

+186
-64
lines changed

4 files changed

+186
-64
lines changed

os2forms_digital_post.services.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ services:
99

1010
os2forms_digital_post.webform_helper:
1111
class: Drupal\os2forms_digital_post\Helper\WebformHelper
12-
arguments: ["@entity_type.manager", "@renderer"]
12+
arguments: ["@entity_type.manager", "@renderer", "@os2forms_cpr_lookup.service", "@os2forms_digital_post.print_service_consumer", "@os2forms_digital_post.template_manager"]

src/Helper/WebformHelper.php

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
namespace Drupal\os2forms_digital_post\Helper;
44

55
use Drupal\os2forms_cpr_lookup\CPR\CprServiceResult;
6+
use Drupal\os2forms_digital_post\Exception\CprElementNotFoundInSubmissionException;
67
use Drupal\webform\WebformSubmissionInterface;
78
use Drupal\Core\Entity\EntityTypeManagerInterface;
89
use Drupal\Core\Render\Renderer;
10+
use Drupal\os2forms_cpr_lookup\Service\CprServiceInterface;
11+
use Drupal\os2forms_digital_post\Consumer\PrintServiceConsumer;
12+
use Drupal\os2forms_digital_post\Manager\TemplateManager;
913

1014
/**
1115
* Webform helper.
@@ -26,12 +30,36 @@ final class WebformHelper {
2630
*/
2731
protected $renderer;
2832

33+
/**
34+
* The cpr service.
35+
*
36+
* @var \Drupal\os2forms_cpr_lookup\Service\CprServiceInterface
37+
*/
38+
protected $cprService;
39+
40+
/**
41+
* The print service consumer.
42+
*
43+
* @var \Drupal\os2forms_digital_post\Consumer\PrintServiceConsumer
44+
*/
45+
protected $printServiceConsumer;
46+
47+
/**
48+
* The template manager.
49+
*
50+
* @var \Drupal\os2forms_digital_post\Manager\TemplateManager
51+
*/
52+
protected $templateManager;
53+
2954
/**
3055
* Constructor.
3156
*/
32-
public function __construct(EntityTypeManagerInterface $entity_type_manager, Renderer $renderer) {
57+
public function __construct(EntityTypeManagerInterface $entity_type_manager, Renderer $renderer, CprServiceInterface $cprService, PrintServiceConsumer $printServiceConsumer, TemplateManager $templateManager) {
3358
$this->entityTypeManager = $entity_type_manager;
3459
$this->renderer = $renderer;
60+
$this->cprService = $cprService;
61+
$this->printServiceConsumer = $printServiceConsumer;
62+
$this->templateManager = $templateManager;
3563
}
3664

3765
/**
@@ -68,4 +96,80 @@ public function getTemplateContext(WebformSubmissionInterface $webformSubmission
6896
];
6997
}
7098

99+
public function sendDigitalPost(string $submissionId, array $handlerConfiguration) {
100+
/** @var \Drupal\webform\Entity\WebformSubmission $submission */
101+
$webform_submission = $this->getSubmission($submissionId);
102+
103+
$submissionData = $webform_submission->getData();
104+
105+
if (!array_key_exists($handlerConfiguration['cpr_element'], $submissionData)) {
106+
$this->getLogger()->error(
107+
'The chosen CPR element not found in submission!'
108+
);
109+
110+
throw new CprElementNotFoundInSubmissionException();
111+
}
112+
113+
/** @var \Drupal\os2forms_cpr_lookup\CPR\CprServiceResult $cprSearchResult */
114+
$cprSearchResult = $this->cprService->search($submissionData[$handlerConfiguration['cpr_element']]);
115+
116+
$context = $this->getTemplateContext($webform_submission, $cprSearchResult, $handlerConfiguration);
117+
118+
if (TRUE === $handlerConfiguration['debug']) {
119+
$this->templateManager->renderPdf($handlerConfiguration['template'], $context, TRUE);
120+
return;
121+
}
122+
123+
$result = FALSE;
124+
125+
switch ($handlerConfiguration['channel']) {
126+
case 'A':
127+
$result = $this->printServiceConsumer->afsendBrevPerson(
128+
$handlerConfiguration['channel'],
129+
$handlerConfiguration['priority'],
130+
$submissionData[$handlerConfiguration['cpr_element']],
131+
$cprSearchResult->getName(),
132+
NULL,
133+
$cprSearchResult->getStreetName(),
134+
$cprSearchResult->getHouseNumber(),
135+
$floor,
136+
NULL,
137+
NULL,
138+
$cprSearchResult->getPostalCode(),
139+
NULL,
140+
NULL,
141+
'DK',
142+
'PDF',
143+
$this->templateManager->renderPdf($handlerConfiguration['template'], $context),
144+
$handlerConfiguration['document_title']
145+
);
146+
break;
147+
148+
case 'D':
149+
$result = $this->printServiceConsumer->afsendDigitalPostPerson(
150+
$handlerConfiguration['channel'],
151+
$handlerConfiguration['priority'],
152+
$submissionData[$handlerConfiguration['cpr_element']],
153+
'PDF',
154+
$this->templateManager->renderPdf($handlerConfiguration['template'], $context),
155+
$handlerConfiguration['document_title']
156+
);
157+
break;
158+
}
159+
160+
if (FALSE === $result) {
161+
// Throw an error?
162+
}
163+
}
164+
165+
/**
166+
* Gets WebformSubmission from id.
167+
*
168+
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
169+
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
170+
*/
171+
private function getSubmission(string $submissionId) {
172+
$storage = $this->entityTypeManager->getStorage('webform_submission');
173+
return $storage->load($submissionId);
174+
}
71175
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Drupal\os2forms_digital_post\Plugin\AdvancedQueue\JobType;
4+
5+
use Drupal\advancedqueue\Job;
6+
use Drupal\advancedqueue\JobResult;
7+
use Drupal\advancedqueue\Plugin\AdvancedQueue\JobType\JobTypeBase;
8+
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
9+
use Drupal\os2forms_digital_post\Helper\WebformHelper;
10+
use Symfony\Component\DependencyInjection\ContainerInterface;
11+
12+
/**
13+
* Archive document job.
14+
*
15+
* @AdvancedQueueJobType(
16+
* id = "Drupal\os2forms_digital_post\Plugin\AdvancedQueue\JobType\SendDigitalPost",
17+
* label = @Translation("Send digital post"),
18+
* )
19+
*/
20+
class SendDigitalPost extends JobTypeBase implements ContainerFactoryPluginInterface {
21+
/**
22+
* The archiving helper.
23+
*
24+
* @var \Drupal\os2forms_digital_post\Helper\WebformHelper
25+
*/
26+
private WebformHelper $helper;
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
32+
return new static(
33+
$configuration,
34+
$plugin_id,
35+
$plugin_definition,
36+
$container->get('os2forms_digital_post.webform_helper')
37+
);
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function __construct(
44+
array $configuration,
45+
$plugin_id,
46+
$plugin_definition,
47+
WebformHelper $helper
48+
) {
49+
parent::__construct($configuration, $plugin_id, $plugin_definition);
50+
$this->helper = $helper;
51+
}
52+
53+
/**
54+
* Processes the ArchiveDocument job.
55+
*/
56+
public function process(Job $job): JobResult {
57+
$payload = $job->getPayload();
58+
59+
try {
60+
$this->helper->sendDigitalPost($payload['submissionId'], $payload['handlerConfiguration']);
61+
62+
return JobResult::success();
63+
}
64+
catch (\Exception $e) {
65+
return JobResult::failure($e->getMessage());
66+
}
67+
}
68+
69+
}
70+

src/Plugin/WebformHandler/DigitalPostWebformHandler.php

Lines changed: 10 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Drupal\os2forms_digital_post\Plugin\WebformHandler;
44

5+
use Drupal\advancedqueue\Job;
56
use Drupal\Core\Form\FormStateInterface;
67
use Drupal\Core\Render\Element;
78
use Drupal\os2forms_digital_post\Exception\CprElementNotFoundInSubmissionException;
9+
use Drupal\os2forms_digital_post\Plugin\AdvancedQueue\JobType\SendDigitalPost;
810
use Drupal\webform\Plugin\WebformHandlerBase;
911
use Drupal\webform\WebformInterface;
1012
use Drupal\webform\WebformSubmissionInterface;
@@ -302,68 +304,14 @@ public function preSave(WebformSubmissionInterface $webform_submission) {
302304
* {@inheritdoc}
303305
*/
304306
public function postSave(WebformSubmissionInterface $webform_submission, $update = TRUE) {
305-
$this->debug(__FUNCTION__, $update ? 'update' : 'insert');
306-
307-
$submissionData = $webform_submission->getData();
308-
309-
if (!array_key_exists($this->configuration['cpr_element'], $submissionData)) {
310-
$this->getLogger()->error(
311-
'The chosen CPR element not found in submission!'
312-
);
313-
314-
throw new CprElementNotFoundInSubmissionException();
315-
}
316-
317-
/** @var \Drupal\os2forms_cpr_lookup\CPR\CprServiceResult $cprSearchResult */
318-
$cprSearchResult = $this->cprService->search($submissionData[$this->configuration['cpr_element']]);
319-
320-
$context = $this->webformHelper->getTemplateContext($webform_submission, $cprSearchResult, $this->configuration);
321-
322-
if (TRUE === $this->configuration['debug']) {
323-
$this->templateManager->renderPdf($this->configuration['template'], $context, TRUE);
324-
return;
325-
}
326-
327-
$result = FALSE;
328-
329-
switch ($this->configuration['channel']) {
330-
case 'A':
331-
$result = $this->printServiceConsumer->afsendBrevPerson(
332-
$this->configuration['channel'],
333-
$this->configuration['priority'],
334-
$submissionData[$this->configuration['cpr_element']],
335-
$cprSearchResult->getName(),
336-
NULL,
337-
$cprSearchResult->getStreetName(),
338-
$cprSearchResult->getHouseNumber(),
339-
$floor,
340-
NULL,
341-
NULL,
342-
$cprSearchResult->getPostalCode(),
343-
NULL,
344-
NULL,
345-
'DK',
346-
'PDF',
347-
$this->templateManager->renderPdf($this->configuration['template'], $context),
348-
$this->configuration['document_title']
349-
);
350-
break;
351-
352-
case 'D':
353-
$result = $this->printServiceConsumer->afsendDigitalPostPerson(
354-
$this->configuration['channel'],
355-
$this->configuration['priority'],
356-
$submissionData[$this->configuration['cpr_element']],
357-
'PDF',
358-
$this->templateManager->renderPdf($this->configuration['template'], $context),
359-
$this->configuration['document_title']
360-
);
361-
break;
362-
}
363-
364-
if (FALSE === $result) {
365-
// Throw an error?
366-
}
307+
$queueStorage = $this->entityTypeManager->getStorage('advancedqueue_queue');
308+
/** @var \Drupal\advancedqueue\Entity\Queue $queue */
309+
$queue = $queueStorage->load('send_digital_post');
310+
$job = Job::create(SendDigitalPost::class, [
311+
'submissionId' => $webform_submission->id(),
312+
'handlerConfiguration' => $this->configuration,
313+
]);
314+
$queue->enqueueJob($job);
367315
}
368316

369317
/**

0 commit comments

Comments
 (0)