diff --git a/os2forms_digital_post.services.yml b/os2forms_digital_post.services.yml index 0463de8..e92beee 100644 --- a/os2forms_digital_post.services.yml +++ b/os2forms_digital_post.services.yml @@ -9,4 +9,4 @@ services: os2forms_digital_post.webform_helper: class: Drupal\os2forms_digital_post\Helper\WebformHelper - arguments: ["@entity_type.manager", "@renderer"] + arguments: ["@entity_type.manager", "@renderer", "@os2forms_cpr_lookup.service", "@os2forms_digital_post.print_service_consumer", "@os2forms_digital_post.template_manager"] diff --git a/src/Helper/WebformHelper.php b/src/Helper/WebformHelper.php index 27a776c..a63530b 100644 --- a/src/Helper/WebformHelper.php +++ b/src/Helper/WebformHelper.php @@ -3,9 +3,13 @@ namespace Drupal\os2forms_digital_post\Helper; use Drupal\os2forms_cpr_lookup\CPR\CprServiceResult; +use Drupal\os2forms_digital_post\Exception\CprElementNotFoundInSubmissionException; use Drupal\webform\WebformSubmissionInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Render\Renderer; +use Drupal\os2forms_cpr_lookup\Service\CprServiceInterface; +use Drupal\os2forms_digital_post\Consumer\PrintServiceConsumer; +use Drupal\os2forms_digital_post\Manager\TemplateManager; /** * Webform helper. @@ -26,12 +30,36 @@ final class WebformHelper { */ protected $renderer; + /** + * The cpr service. + * + * @var \Drupal\os2forms_cpr_lookup\Service\CprServiceInterface + */ + protected $cprService; + + /** + * The print service consumer. + * + * @var \Drupal\os2forms_digital_post\Consumer\PrintServiceConsumer + */ + protected $printServiceConsumer; + + /** + * The template manager. + * + * @var \Drupal\os2forms_digital_post\Manager\TemplateManager + */ + protected $templateManager; + /** * Constructor. */ - public function __construct(EntityTypeManagerInterface $entity_type_manager, Renderer $renderer) { + public function __construct(EntityTypeManagerInterface $entity_type_manager, Renderer $renderer, CprServiceInterface $cprService, PrintServiceConsumer $printServiceConsumer, TemplateManager $templateManager) { $this->entityTypeManager = $entity_type_manager; $this->renderer = $renderer; + $this->cprService = $cprService; + $this->printServiceConsumer = $printServiceConsumer; + $this->templateManager = $templateManager; } /** @@ -68,4 +96,86 @@ public function getTemplateContext(WebformSubmissionInterface $webformSubmission ]; } + /** + * Send digital post. + * + * @param string $submissionId + * The submission ID. + * @param array $handlerConfiguration + * Handler config. + * + * @throws CprElementNotFoundInSubmissionException + * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException + * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException + * @throws \ItkDev\Serviceplatformen\Service\Exception\ServiceException + */ + public function sendDigitalPost(string $submissionId, array $handlerConfiguration) { + /** @var \Drupal\webform\Entity\WebformSubmission $submission */ + $webform_submission = $this->getSubmission($submissionId); + + $submissionData = $webform_submission->getData(); + + if (!array_key_exists($handlerConfiguration['cpr_element'], $submissionData)) { + $this->getLogger()->error( + 'The chosen CPR element not found in submission!' + ); + + throw new CprElementNotFoundInSubmissionException(); + } + + /** @var \Drupal\os2forms_cpr_lookup\CPR\CprServiceResult $cprSearchResult */ + $cprSearchResult = $this->cprService->search($submissionData[$handlerConfiguration['cpr_element']]); + $context = $this->getTemplateContext($webform_submission, $cprSearchResult, $handlerConfiguration); + $result = FALSE; + + switch ($handlerConfiguration['channel']) { + case 'A': + $result = $this->printServiceConsumer->afsendBrevPerson( + $handlerConfiguration['channel'], + $handlerConfiguration['priority'], + $submissionData[$handlerConfiguration['cpr_element']], + $cprSearchResult->getName(), + NULL, + $cprSearchResult->getStreetName(), + $cprSearchResult->getHouseNumber(), + $floor, + NULL, + NULL, + $cprSearchResult->getPostalCode(), + NULL, + NULL, + 'DK', + 'PDF', + $this->templateManager->renderPdf($handlerConfiguration['template'], $context), + $handlerConfiguration['document_title'] + ); + break; + + case 'D': + $result = $this->printServiceConsumer->afsendDigitalPostPerson( + $handlerConfiguration['channel'], + $handlerConfiguration['priority'], + $submissionData[$handlerConfiguration['cpr_element']], + 'PDF', + $this->templateManager->renderPdf($handlerConfiguration['template'], $context), + $handlerConfiguration['document_title'] + ); + break; + } + + if (FALSE === $result) { + // Throw an error? + } + } + + /** + * Gets WebformSubmission from id. + * + * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException + * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException + */ + 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 new file mode 100644 index 0000000..ba1425a --- /dev/null +++ b/src/Plugin/AdvancedQueue/JobType/SendDigitalPost.php @@ -0,0 +1,70 @@ +get('os2forms_digital_post.webform_helper') + ); + } + + /** + * {@inheritdoc} + */ + public function __construct( + array $configuration, + $plugin_id, + $plugin_definition, + WebformHelper $helper + ) { + parent::__construct($configuration, $plugin_id, $plugin_definition); + $this->helper = $helper; + } + + /** + * Processes the send digital post job. + */ + public function process(Job $job): JobResult { + $payload = $job->getPayload(); + + try { + $this->helper->sendDigitalPost($payload['submissionId'], $payload['handlerConfiguration']); + + return JobResult::success(); + } + catch (\Exception $e) { + return JobResult::failure($e->getMessage()); + } + } + +} + diff --git a/src/Plugin/WebformHandler/DigitalPostWebformHandler.php b/src/Plugin/WebformHandler/DigitalPostWebformHandler.php index b76f02f..c4c6b55 100644 --- a/src/Plugin/WebformHandler/DigitalPostWebformHandler.php +++ b/src/Plugin/WebformHandler/DigitalPostWebformHandler.php @@ -2,9 +2,11 @@ namespace Drupal\os2forms_digital_post\Plugin\WebformHandler; +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; use Drupal\webform\WebformSubmissionInterface; @@ -302,68 +304,14 @@ public function preSave(WebformSubmissionInterface $webform_submission) { * {@inheritdoc} */ public function postSave(WebformSubmissionInterface $webform_submission, $update = TRUE) { - $this->debug(__FUNCTION__, $update ? 'update' : 'insert'); - - $submissionData = $webform_submission->getData(); - - if (!array_key_exists($this->configuration['cpr_element'], $submissionData)) { - $this->getLogger()->error( - 'The chosen CPR element not found in submission!' - ); - - throw new CprElementNotFoundInSubmissionException(); - } - - /** @var \Drupal\os2forms_cpr_lookup\CPR\CprServiceResult $cprSearchResult */ - $cprSearchResult = $this->cprService->search($submissionData[$this->configuration['cpr_element']]); - - $context = $this->webformHelper->getTemplateContext($webform_submission, $cprSearchResult, $this->configuration); - - if (TRUE === $this->configuration['debug']) { - $this->templateManager->renderPdf($this->configuration['template'], $context, TRUE); - return; - } - - $result = FALSE; - - switch ($this->configuration['channel']) { - case 'A': - $result = $this->printServiceConsumer->afsendBrevPerson( - $this->configuration['channel'], - $this->configuration['priority'], - $submissionData[$this->configuration['cpr_element']], - $cprSearchResult->getName(), - NULL, - $cprSearchResult->getStreetName(), - $cprSearchResult->getHouseNumber(), - $floor, - NULL, - NULL, - $cprSearchResult->getPostalCode(), - NULL, - NULL, - 'DK', - 'PDF', - $this->templateManager->renderPdf($this->configuration['template'], $context), - $this->configuration['document_title'] - ); - break; - - case 'D': - $result = $this->printServiceConsumer->afsendDigitalPostPerson( - $this->configuration['channel'], - $this->configuration['priority'], - $submissionData[$this->configuration['cpr_element']], - 'PDF', - $this->templateManager->renderPdf($this->configuration['template'], $context), - $this->configuration['document_title'] - ); - break; - } - - if (FALSE === $result) { - // Throw an error? - } + $queueStorage = $this->entityTypeManager->getStorage('advancedqueue_queue'); + /** @var \Drupal\advancedqueue\Entity\Queue $queue */ + $queue = $queueStorage->load('send_digital_post'); + $job = Job::create(SendDigitalPost::class, [ + 'submissionId' => $webform_submission->id(), + 'handlerConfiguration' => $this->configuration, + ]); + $queue->enqueueJob($job); } /**