Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion os2forms_digital_post.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
112 changes: 111 additions & 1 deletion src/Helper/WebformHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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);
}
}
70 changes: 70 additions & 0 deletions src/Plugin/AdvancedQueue/JobType/SendDigitalPost.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Drupal\os2forms_digital_post\Plugin\AdvancedQueue\JobType;

use Drupal\advancedqueue\Job;
use Drupal\advancedqueue\JobResult;
use Drupal\advancedqueue\Plugin\AdvancedQueue\JobType\JobTypeBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\os2forms_digital_post\Helper\WebformHelper;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Send digital post.
*
* @AdvancedQueueJobType(
* id = "Drupal\os2forms_digital_post\Plugin\AdvancedQueue\JobType\SendDigitalPost",
* label = @Translation("Send digital post"),
* )
*/
class SendDigitalPost extends JobTypeBase implements ContainerFactoryPluginInterface {
/**
* The webform helper.
*
* @var \Drupal\os2forms_digital_post\Helper\WebformHelper
*/
private WebformHelper $helper;

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->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());
}
}

}

72 changes: 10 additions & 62 deletions src/Plugin/WebformHandler/DigitalPostWebformHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down