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

Commit a023e6a

Browse files
authored
Merge pull request #20 from itk-dev/hotfix/DW-553
DW-553: Handle missing submission
2 parents ce9fa90 + 287d9ef commit a023e6a

File tree

5 files changed

+42
-7
lines changed

5 files changed

+42
-7
lines changed

os2forms_digital_post.services.yml

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

1010
os2forms_digital_post.webform_helper:
1111
class: Drupal\os2forms_digital_post\Helper\WebformHelper
12-
arguments: ["@entity_type.manager", "@renderer", "@os2forms_cpr_lookup.service", "@os2forms_digital_post.print_service_consumer", "@os2forms_digital_post.template_manager"]
12+
arguments:
13+
- "@entity_type.manager"
14+
- "@renderer"
15+
- "@os2forms_cpr_lookup.service"
16+
- "@os2forms_digital_post.print_service_consumer"
17+
- "@os2forms_digital_post.template_manager"
18+
- "@logger.factory"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Drupal\os2forms_digital_post\Exception;
4+
5+
/**
6+
* Submission not found exception.
7+
*/
8+
class SubmissionNotFoundException extends \Exception {
9+
10+
}

src/Helper/WebformHelper.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace Drupal\os2forms_digital_post\Helper;
44

5+
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
56
use Drupal\os2forms_cpr_lookup\CPR\CprServiceResult;
67
use Drupal\os2forms_digital_post\Exception\CprElementNotFoundInSubmissionException;
8+
use Drupal\os2forms_digital_post\Exception\SubmissionNotFoundException;
79
use Drupal\webform\WebformSubmissionInterface;
810
use Drupal\Core\Entity\EntityTypeManagerInterface;
911
use Drupal\Core\Render\Renderer;
@@ -51,15 +53,23 @@ final class WebformHelper {
5153
*/
5254
protected $templateManager;
5355

56+
/**
57+
* The logger.
58+
*
59+
* @var \Drupal\Core\Logger\LoggerChannelInterface
60+
*/
61+
protected $logger;
62+
5463
/**
5564
* Constructor.
5665
*/
57-
public function __construct(EntityTypeManagerInterface $entity_type_manager, Renderer $renderer, CprServiceInterface $cprService, PrintServiceConsumer $printServiceConsumer, TemplateManager $templateManager) {
66+
public function __construct(EntityTypeManagerInterface $entity_type_manager, Renderer $renderer, CprServiceInterface $cprService, PrintServiceConsumer $printServiceConsumer, TemplateManager $templateManager, LoggerChannelFactoryInterface $loggerChannelFactory) {
5867
$this->entityTypeManager = $entity_type_manager;
5968
$this->renderer = $renderer;
6069
$this->cprService = $cprService;
6170
$this->printServiceConsumer = $printServiceConsumer;
6271
$this->templateManager = $templateManager;
72+
$this->logger = $loggerChannelFactory->get('os2forms_digital_post');
6373
}
6474

6575
/**
@@ -92,7 +102,7 @@ public function getTemplateContext(WebformSubmissionInterface $webformSubmission
92102
return [
93103
'label' => $webform->label(),
94104
'recipient' => $recipient,
95-
'submission' => $webformSubmissionRendered
105+
'submission' => $webformSubmissionRendered,
96106
];
97107
}
98108

@@ -104,19 +114,28 @@ public function getTemplateContext(WebformSubmissionInterface $webformSubmission
104114
* @param array $handlerConfiguration
105115
* Handler config.
106116
*
107-
* @throws CprElementNotFoundInSubmissionException
117+
* @throws \Drupal\os2forms_digital_post\Exception\CprElementNotFoundInSubmissionException
118+
* @throws \Drupal\os2forms_digital_post\Exception\SubmissionNotFoundException
108119
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
109120
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
110121
* @throws \ItkDev\Serviceplatformen\Service\Exception\ServiceException
111122
*/
112123
public function sendDigitalPost(string $submissionId, array $handlerConfiguration) {
113124
/** @var \Drupal\webform\Entity\WebformSubmission $submission */
114125
$webform_submission = $this->getSubmission($submissionId);
126+
if (empty($webform_submission)) {
127+
$this->logger->error(
128+
'Cannot load submission @submissionId',
129+
['@submissionId' => $submissionId]
130+
);
131+
132+
throw new SubmissionNotFoundException(sprintf('Submission %s not found', $submissionId));
133+
}
115134

116135
$submissionData = $webform_submission->getData();
117136

118137
if (!array_key_exists($handlerConfiguration['cpr_element'], $submissionData)) {
119-
$this->getLogger()->error(
138+
$this->logger->error(
120139
'The chosen CPR element not found in submission!'
121140
);
122141

@@ -178,4 +197,5 @@ private function getSubmission(string $submissionId) {
178197
$storage = $this->entityTypeManager->getStorage('webform_submission');
179198
return $storage->load($submissionId);
180199
}
200+
181201
}

src/Plugin/AdvancedQueue/JobType/SendDigitalPost.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,3 @@ public function process(Job $job): JobResult {
6767
}
6868

6969
}
70-

src/Plugin/WebformHandler/DigitalPostWebformHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Drupal\advancedqueue\Job;
66
use Drupal\Core\Form\FormStateInterface;
77
use Drupal\Core\Render\Element;
8-
use Drupal\os2forms_digital_post\Exception\CprElementNotFoundInSubmissionException;
98
use Drupal\os2forms_digital_post\Plugin\AdvancedQueue\JobType\SendDigitalPost;
109
use Drupal\webform\Plugin\WebformHandlerBase;
1110
use Drupal\webform\WebformInterface;
@@ -308,6 +307,7 @@ public function postSave(WebformSubmissionInterface $webform_submission, $update
308307
/** @var \Drupal\advancedqueue\Entity\Queue $queue */
309308
$queue = $queueStorage->load('send_digital_post');
310309
$job = Job::create(SendDigitalPost::class, [
310+
'formId' => $webform_submission->getWebform()->id(),
311311
'submissionId' => $webform_submission->id(),
312312
'handlerConfiguration' => $this->configuration,
313313
]);

0 commit comments

Comments
 (0)