diff --git a/os2forms_digital_post.services.yml b/os2forms_digital_post.services.yml index e92beee..db80747 100644 --- a/os2forms_digital_post.services.yml +++ b/os2forms_digital_post.services.yml @@ -9,4 +9,10 @@ services: os2forms_digital_post.webform_helper: class: Drupal\os2forms_digital_post\Helper\WebformHelper - arguments: ["@entity_type.manager", "@renderer", "@os2forms_cpr_lookup.service", "@os2forms_digital_post.print_service_consumer", "@os2forms_digital_post.template_manager"] + arguments: + - "@entity_type.manager" + - "@renderer" + - "@os2forms_cpr_lookup.service" + - "@os2forms_digital_post.print_service_consumer" + - "@os2forms_digital_post.template_manager" + - "@logger.factory" diff --git a/src/Exception/SubmissionNotFoundException.php b/src/Exception/SubmissionNotFoundException.php new file mode 100644 index 0000000..2e605ff --- /dev/null +++ b/src/Exception/SubmissionNotFoundException.php @@ -0,0 +1,10 @@ +entityTypeManager = $entity_type_manager; $this->renderer = $renderer; $this->cprService = $cprService; $this->printServiceConsumer = $printServiceConsumer; $this->templateManager = $templateManager; + $this->logger = $loggerChannelFactory->get('os2forms_digital_post'); } /** @@ -92,7 +102,7 @@ public function getTemplateContext(WebformSubmissionInterface $webformSubmission return [ 'label' => $webform->label(), 'recipient' => $recipient, - 'submission' => $webformSubmissionRendered + 'submission' => $webformSubmissionRendered, ]; } @@ -104,7 +114,8 @@ public function getTemplateContext(WebformSubmissionInterface $webformSubmission * @param array $handlerConfiguration * Handler config. * - * @throws CprElementNotFoundInSubmissionException + * @throws \Drupal\os2forms_digital_post\Exception\CprElementNotFoundInSubmissionException + * @throws \Drupal\os2forms_digital_post\Exception\SubmissionNotFoundException * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException * @throws \ItkDev\Serviceplatformen\Service\Exception\ServiceException @@ -112,11 +123,19 @@ public function getTemplateContext(WebformSubmissionInterface $webformSubmission public function sendDigitalPost(string $submissionId, array $handlerConfiguration) { /** @var \Drupal\webform\Entity\WebformSubmission $submission */ $webform_submission = $this->getSubmission($submissionId); + if (empty($webform_submission)) { + $this->logger->error( + 'Cannot load submission @submissionId', + ['@submissionId' => $submissionId] + ); + + throw new SubmissionNotFoundException(sprintf('Submission %s not found', $submissionId)); + } $submissionData = $webform_submission->getData(); if (!array_key_exists($handlerConfiguration['cpr_element'], $submissionData)) { - $this->getLogger()->error( + $this->logger->error( 'The chosen CPR element not found in submission!' ); @@ -178,4 +197,5 @@ private function getSubmission(string $submissionId) { $storage = $this->entityTypeManager->getStorage('webform_submission'); return $storage->load($submissionId); } + } diff --git a/src/Plugin/AdvancedQueue/JobType/SendDigitalPost.php b/src/Plugin/AdvancedQueue/JobType/SendDigitalPost.php index ba1425a..bb909a3 100644 --- a/src/Plugin/AdvancedQueue/JobType/SendDigitalPost.php +++ b/src/Plugin/AdvancedQueue/JobType/SendDigitalPost.php @@ -67,4 +67,3 @@ public function process(Job $job): JobResult { } } - diff --git a/src/Plugin/WebformHandler/DigitalPostWebformHandler.php b/src/Plugin/WebformHandler/DigitalPostWebformHandler.php index c4c6b55..122eb3e 100644 --- a/src/Plugin/WebformHandler/DigitalPostWebformHandler.php +++ b/src/Plugin/WebformHandler/DigitalPostWebformHandler.php @@ -5,7 +5,6 @@ use Drupal\advancedqueue\Job; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Render\Element; -use Drupal\os2forms_digital_post\Exception\CprElementNotFoundInSubmissionException; use Drupal\os2forms_digital_post\Plugin\AdvancedQueue\JobType\SendDigitalPost; use Drupal\webform\Plugin\WebformHandlerBase; use Drupal\webform\WebformInterface; @@ -308,6 +307,7 @@ public function postSave(WebformSubmissionInterface $webform_submission, $update /** @var \Drupal\advancedqueue\Entity\Queue $queue */ $queue = $queueStorage->load('send_digital_post'); $job = Job::create(SendDigitalPost::class, [ + 'formId' => $webform_submission->getWebform()->id(), 'submissionId' => $webform_submission->id(), 'handlerConfiguration' => $this->configuration, ]);