Skip to content

Commit

Permalink
ITKDev: Updated code style and doc blocks in os2forms_digital_post
Browse files Browse the repository at this point in the history
  • Loading branch information
cableman committed Aug 21, 2024
1 parent f7ff39e commit 158ea5f
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ public function __construct(
*
* @command os2forms-digital-post:test:send
* @usage os2forms-digital-post:test:send --help
*
* @throws \Drupal\Component\Plugin\Exception\PluginException
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
*/
public function send(
array $recipients,
Expand Down Expand Up @@ -138,6 +144,8 @@ public function send(

/**
* Dump digital post settings.
*
* @throws \Drupal\os2forms_digital_post\Exception\InvalidSettingException
*/
private function dumpDigitalPostSettings(SymfonyStyle $io): void {
$io->section('Digital post settings');
Expand Down
32 changes: 17 additions & 15 deletions modules/os2forms_digital_post/src/Form/SettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public function __construct(
/**
* {@inheritdoc}
*
* @phpstan-return self
* @phpstan-return static
*/
public static function create(ContainerInterface $container) {
public static function create(ContainerInterface $container): SettingsForm {
return new static(
$container->get(Settings::class),
$container->get(CertificateLocatorHelper::class),
Expand All @@ -53,7 +53,7 @@ public static function create(ContainerInterface $container) {
/**
* {@inheritdoc}
*/
public function getFormId() {
public function getFormId(): string {
return 'os2forms_digital_post_settings';
}

Expand All @@ -62,8 +62,10 @@ public function getFormId() {
*
* @phpstan-param array<string, mixed> $form
* @phpstan-return array<string, mixed>
*
* @throws \Drupal\os2forms_digital_post\Exception\InvalidSettingException
*/
public function buildForm(array $form, FormStateInterface $form_state) {
public function buildForm(array $form, FormStateInterface $form_state): array {
$form['test_mode'] = [
'#type' => 'checkbox',
'#title' => $this->t('Test mode'),
Expand Down Expand Up @@ -212,17 +214,17 @@ public function buildForm(array $form, FormStateInterface $form_state) {
*
* @phpstan-param array<string, mixed> $form
*/
public function validateForm(array &$form, FormStateInterface $formState): void {
$triggeringElement = $formState->getTriggeringElement();
public function validateForm(array &$form, FormStateInterface $form_state): void {
$triggeringElement = $form_state->getTriggeringElement();
if ('testCertificate' === ($triggeringElement['#name'] ?? NULL)) {
return;
}

$values = $formState->getValues();
$values = $form_state->getValues();
if (CertificateLocatorHelper::LOCATOR_TYPE_FILE_SYSTEM === $values['certificate']['locator_type']) {
$path = $values['certificate'][CertificateLocatorHelper::LOCATOR_TYPE_FILE_SYSTEM]['path'] ?? NULL;
if (!file_exists($path)) {
$formState->setErrorByName('certificate][file_system][path', $this->t('Invalid certificate path: %path', ['%path' => $path]));
$form_state->setErrorByName('certificate][file_system][path', $this->t('Invalid certificate path: %path', ['%path' => $path]));
}
}
}
Expand All @@ -232,18 +234,18 @@ public function validateForm(array &$form, FormStateInterface $formState): void
*
* @phpstan-param array<string, mixed> $form
*/
public function submitForm(array &$form, FormStateInterface $formState): void {
$triggeringElement = $formState->getTriggeringElement();
public function submitForm(array &$form, FormStateInterface $form_state): void {
$triggeringElement = $form_state->getTriggeringElement();
if ('testCertificate' === ($triggeringElement['#name'] ?? NULL)) {
$this->testCertificate();
return;
}

try {
$settings['test_mode'] = (bool) $formState->getValue('test_mode');
$settings['sender'] = $formState->getValue('sender');
$settings['certificate'] = $formState->getValue('certificate');
$settings['processing'] = $formState->getValue('processing');
$settings['test_mode'] = (bool) $form_state->getValue('test_mode');
$settings['sender'] = $form_state->getValue('sender');
$settings['certificate'] = $form_state->getValue('certificate');
$settings['processing'] = $form_state->getValue('processing');
$this->settings->setSettings($settings);
$this->messenger()->addStatus($this->t('Settings saved'));
}
Expand All @@ -259,7 +261,7 @@ private function testCertificate(): void {
try {
$certificateLocator = $this->certificateLocatorHelper->getCertificateLocator();
$certificateLocator->getCertificates();
$this->messenger()->addStatus($this->t('Certificate succesfully tested'));
$this->messenger()->addStatus($this->t('Certificate successfully tested'));
}
catch (\Throwable $throwable) {
$message = $this->t('Error testing certificate: %message', ['%message' => $throwable->getMessage()]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ public function __construct(
}

/**
* Get main document.
* Get the main document.
*
* @throws \Drupal\Component\Plugin\Exception\PluginException
* @throws \Drupal\os2forms_digital_post\Exception\InvalidAttachmentElementException
*
* @see WebformAttachmentController::download()
*
Expand Down
12 changes: 10 additions & 2 deletions modules/os2forms_digital_post/src/Helper/BeskedfordelerHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public function __construct(
}

/**
* Save MeMo message in database.
* Save MeMo message to the database.
*
* @throws \Exception
*/
public function createMessage(int $submissionId, MeMoMessage $message, string $receipt): int {
$messageUUID = $message->getMessageHeader()->getMessageUUID();
Expand All @@ -51,11 +53,13 @@ public function createMessage(int $submissionId, MeMoMessage $message, string $r
* Load message.
*
* @param string $messageUUID
* The message UUID.
* The message's UUID.
*
* @return \Drupal\os2forms_digital_post\Model\Message|null
* The message.
*
* @throws \Exception
*
* @see Message::__set()
*/
public function loadMessage(string $messageUUID): ?Message {
Expand All @@ -70,6 +74,8 @@ public function loadMessage(string $messageUUID): ?Message {

/**
* Add Beskedfordeler message to message.
*
* @throws \Exception
*/
public function addBeskedfordelerMessage(string $messageUUID, string $beskedfordelerMessage): bool {
$message = $this->loadMessage($messageUUID);
Expand All @@ -92,6 +98,8 @@ public function addBeskedfordelerMessage(string $messageUUID, string $beskedford
*
* @param array|WebformSubmissionInterface[] $submissions
* The submissions.
*
* @throws \Exception
*/
public function deleteMessages(array $submissions): void {
$submissionIds = array_map(static function (WebformSubmissionInterface $submission) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ class CertificateLocatorHelper {
public const LOCATOR_TYPE_FILE_SYSTEM = 'file_system';

/**
* {@inheritdoc}
* Default constructor.
*
* @param Settings $settings
* Modul configuration.
*/
public function __construct(
private readonly Settings $settings,
Expand All @@ -29,6 +32,9 @@ public function __construct(

/**
* Get certificate locator.
*
* @throws \ItkDev\AzureKeyVault\Exception\TokenException
* @throws \Drupal\os2forms_digital_post\Exception\CertificateLocatorException
*/
public function getCertificateLocator(): CertificateLocatorInterface {
$certificateSettings = $this->settings->getCertificate();
Expand Down
15 changes: 15 additions & 0 deletions modules/os2forms_digital_post/src/Helper/DigitalPostHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ public function __construct(
* @return array
* [The response, The kombi post message].
*
* @throws \Drupal\os2forms_digital_post\Exception\CertificateLocatorException
* @throws \ItkDev\AzureKeyVault\Exception\TokenException
* @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
*
* @phpstan-return array<int, mixed>
*/
public function sendDigitalPost(string $type, Message $message, ?ForsendelseI $forsendelse, WebformSubmissionInterface $submission = NULL): array {
Expand Down Expand Up @@ -87,6 +94,9 @@ public function log($level, $message, array $context = []): void {

/**
* Look up CPR.
*
* @throws \Drupal\Component\Plugin\Exception\PluginException
* @throws \Drupal\os2forms_digital_post\Exception\RuntimeException
*/
public function lookupCpr(string $cpr): CprLookupResult {
$instance = $this->dataLookupManager->createDefaultInstanceByGroup('cpr_lookup');
Expand All @@ -103,6 +113,9 @@ public function lookupCpr(string $cpr): CprLookupResult {

/**
* Look up CVR.
*
* @throws \Drupal\Component\Plugin\Exception\PluginException
* @throws \Drupal\os2forms_digital_post\Exception\RuntimeException
*/
public function lookupCvr(string $cvr): CompanyLookupResult {
$instance = $this->dataLookupManager->createDefaultInstanceByGroup('cvr_lookup');
Expand All @@ -119,6 +132,8 @@ public function lookupCvr(string $cvr): CompanyLookupResult {

/**
* Look up recipient.
*
* @throws \Drupal\os2forms_digital_post\Exception\RuntimeException
*/
public function lookupRecipient(string $recipient): CprLookupResult|CompanyLookupResult {
try {
Expand Down
13 changes: 11 additions & 2 deletions modules/os2forms_digital_post/src/Helper/ForsendelseHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class ForsendelseHelper extends AbstractMessageHelper {

/**
* Build forsendelse.
*
* @throws \Drupal\os2forms_digital_post\Exception\InvalidForsendelseException
*/
public function buildForsendelse(CprLookupResult|CompanyLookupResult $recipientData, string $messageLabel, Document $document): ForsendelseI {
$forsendelse = new ForsendelseI();
Expand Down Expand Up @@ -56,8 +58,15 @@ public function buildForsendelse(CprLookupResult|CompanyLookupResult $recipientD
/**
* Build forsendelse form a webform submission.
*
* @phpstan-param array<string, mixed> $options
* @phpstan-param array<string, mixed> $handlerSettings
* @param WebformSubmissionInterface $submission
* @param array<string, mixed> $options
* @param array<string, mixed> $handlerSettings
* @param CprLookupResult|CompanyLookupResult $recipientData
*
* @return \Oio\Fjernprint\ForsendelseI
* @throws \Drupal\Component\Plugin\Exception\PluginException
* @throws \Drupal\os2forms_digital_post\Exception\InvalidAttachmentElementException
* @throws \Drupal\os2forms_digital_post\Exception\InvalidForsendelseException
*/
public function buildSubmissionForsendelse(WebformSubmissionInterface $submission, array $options, array $handlerSettings, CprLookupResult|CompanyLookupResult $recipientData): ForsendelseI {
$label = $this->replaceTokens($options[WebformHandlerSF1601::MESSAGE_HEADER_LABEL], $submission);
Expand Down
2 changes: 1 addition & 1 deletion modules/os2forms_digital_post/src/Helper/MeMoHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public function buildWebformSubmissionMessage(WebformSubmissionInterface $submis
}

/**
* Enrich recipient with additional data from a lookup.
* Enrich the recipient with additional data from a lookup.
*/
private function enrichRecipient(Recipient $recipient, CprLookupResult|CompanyLookupResult|null $recipientData): Recipient {
if ($recipientData instanceof CprLookupResult) {
Expand Down
12 changes: 10 additions & 2 deletions modules/os2forms_digital_post/src/Helper/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ final class Settings {
*
* @var string
*/
private $collection = 'os2forms_digital_post.';
private string $collection = 'os2forms_digital_post.';

/**
* Constructor.
Expand All @@ -38,6 +38,8 @@ public function __construct(KeyValueFactoryInterface $keyValueFactory) {

/**
* Get test mode.
*
* @throws \Drupal\os2forms_digital_post\Exception\InvalidSettingException
*/
public function getTestMode(): bool {
return (bool) $this->get('test_mode', TRUE);
Expand All @@ -47,6 +49,8 @@ public function getTestMode(): bool {
* Get sender.
*
* @phpstan-return array<string, mixed>
*
* @throws \Drupal\os2forms_digital_post\Exception\InvalidSettingException
*/
public function getSender(): array {
$value = $this->get('sender');
Expand All @@ -57,6 +61,8 @@ public function getSender(): array {
* Get certificate.
*
* @phpstan-return array<string, mixed>
*
* @throws \Drupal\os2forms_digital_post\Exception\InvalidSettingException
*/
public function getCertificate(): array {
$value = $this->get('certificate');
Expand All @@ -83,8 +89,10 @@ public function getProcessing(): array {
*
* @return mixed
* The setting value.
*
* @throws \Drupal\os2forms_digital_post\Exception\InvalidSettingException
*/
private function get(string $key, $default = NULL) {
private function get(string $key, mixed $default = NULL) {
$resolver = $this->getSettingsResolver();
if (!$resolver->isDefined($key)) {
throw new InvalidSettingException(sprintf('Setting %s is not defined', $key));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ public function __construct(
* @phpstan-param array<string, mixed> $handlerSettings
* @phpstan-param array<string, mixed> $submissionData
* @phpstan-return array<int, mixed>
*
* @throws \Drupal\os2forms_digital_post\Exception\InvalidRecipientIdentifierElementException
* @throws \Drupal\Component\Plugin\Exception\PluginException
* @throws \Drupal\os2forms_digital_post\Exception\RuntimeException
*/
public function sendDigitalPost(WebformSubmissionInterface $submission, array $handlerSettings, array $submissionData = []): array {
$submissionData = $submissionData + $submission->getData();
Expand Down
Loading

0 comments on commit 158ea5f

Please sign in to comment.