-
Notifications
You must be signed in to change notification settings - Fork 3
Support for MeMo 1.2 #189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Support for MeMo 1.2 #189
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0943c5c
5189: Sanitized MeMo filename
rimi-itk 3191624
5189: Added check for absolute and secure URLs in MeMo actions
rimi-itk fdb5ee6
5189: Improved digital post test command
rimi-itk c7762ba
5189: Refactored action URL validation. Added runtime check.
rimi-itk 7533b14
5189: Cleaned up
rimi-itk 7bd7041
5189: Updated itk-dev/serviceplatformen
rimi-itk a802c2f
5189: Updated example forms
rimi-itk 62652e1
5189: Added clarifying comment
rimi-itk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,9 @@ | |
|
|
||
| namespace Drupal\os2forms_digital_post\Drush\Commands; | ||
|
|
||
| use DigitalPost\MeMo\Action; | ||
| use DigitalPost\MeMo\EntryPoint; | ||
| use DigitalPost\MeMo\Reservation; | ||
| use Drupal\Component\Serialization\Yaml; | ||
| use Drupal\Core\DependencyInjection\AutowireTrait; | ||
| use Drupal\Core\Utility\Token; | ||
|
|
@@ -10,10 +13,16 @@ | |
| use Drupal\os2forms_digital_post\Helper\Settings; | ||
| use Drupal\os2forms_digital_post\Model\Document; | ||
| use Drush\Commands\DrushCommands; | ||
| use ItkDev\Serviceplatformen\Service\SF1601\Serializer; | ||
| use ItkDev\Serviceplatformen\Service\SF1601\SF1601; | ||
| use Symfony\Component\Console\Exception\InvalidArgumentException; | ||
| use Symfony\Component\Console\Exception\InvalidOptionException; | ||
| use Symfony\Component\Console\Style\SymfonyStyle; | ||
| use Symfony\Component\DependencyInjection\Attribute\Autowire; | ||
| use Symfony\Component\OptionsResolver\Exception\ExceptionInterface; | ||
| use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; | ||
| use Symfony\Component\OptionsResolver\Options; | ||
| use Symfony\Component\OptionsResolver\OptionsResolver; | ||
| use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; | ||
|
|
||
| /** | ||
|
|
@@ -44,14 +53,20 @@ public function __construct( | |
| * @param array $options | ||
| * The options. | ||
| * | ||
| * @option string subject | ||
| * The subject. Can contain HTML. | ||
| * @option string message | ||
| * The message to send. Can contain HTML. | ||
| * @option string digital-post-type | ||
| * The digital post type to use. | ||
| * @option bool dump-digital-post-settings | ||
| * Dump digital post settings. | ||
| * @option subject | ||
| * The subject. Can contain HTML. | ||
| * @option message | ||
| * The message to send. Can contain HTML. | ||
| * @option digital-post-type | ||
| * The digital post type to use. | ||
| * @option dump-digital-post-settings | ||
| * Dump digital post settings. | ||
| * @option memo-version | ||
| * MeMo version (1.1 or 1.2). If not set, a proper default will be used. | ||
| * @option action | ||
| * MeMo actions, e.g. 'action=INFORMATION&label=Vigtig%20information&entrypoint=https://example.com' | ||
| * @option filename | ||
| * The main document filename (used to test invalid filenames (cf. https://digitaliser.dk/digital-post/nyhedsarkiv/2024/nov/oeget-validering-i-digital-post)) | ||
| * | ||
| * @phpstan-param array<string> $recipients | ||
| * @phpstan-param array<string, mixed> $options | ||
|
|
@@ -66,6 +81,9 @@ public function send( | |
| 'message' => 'This is a test message from os2forms_digital_post sent on [current-date:html_datetime].', | ||
| 'digital-post-type' => SF1601::TYPE_AUTOMATISK_VALG, | ||
| 'dump-digital-post-settings' => FALSE, | ||
| 'memo-version' => NULL, | ||
| 'action' => [], | ||
| 'filename' => 'os2forms_digital_post', | ||
| ], | ||
| ): void { | ||
| $io = new SymfonyStyle($this->input(), $this->output()); | ||
|
|
@@ -89,7 +107,7 @@ public function send( | |
| $document = new Document( | ||
| $content, | ||
| Document::MIME_TYPE_PDF, | ||
| 'os2forms_digital_post.pdf' | ||
| $options['filename'] . '.pdf', | ||
| ); | ||
|
|
||
| $type = $options['digital-post-type']; | ||
|
|
@@ -98,21 +116,40 @@ public function send( | |
| throw new InvalidArgumentException(sprintf('Invalid type: %s. Must be one of %s.', $quote($type), implode(', ', array_map($quote, SF1601::TYPES)))); | ||
| } | ||
|
|
||
| $meMoVersion = $options['memo-version']; | ||
| if ($meMoVersion) { | ||
| $meMoVersion = (float) $meMoVersion; | ||
| $allowedValues = [SF1601::MEMO_1_1, SF1601::MEMO_1_2]; | ||
| if (!in_array($meMoVersion, $allowedValues, TRUE)) { | ||
| $quote = static fn($value) => var_export($value, TRUE); | ||
| throw new InvalidArgumentException(sprintf( | ||
| 'Invalid MeMo version: %s. Must be one of %s.', | ||
| $quote($meMoVersion), | ||
| implode(', ', array_map($quote, $allowedValues)) | ||
| )); | ||
| } | ||
| } | ||
|
|
||
| $io->section('Digital post'); | ||
| $io->definitionList( | ||
| ['Type' => $type], | ||
| ['Subject' => $subject], | ||
| ['Message' => $message] | ||
| ['Message' => $message], | ||
| ['Document' => sprintf('%s (%s)', $document->filename, $document->mimeType)], | ||
| ['MeMo version' => $meMoVersion ?? '–'], | ||
| ); | ||
|
|
||
| $actions = array_map($this->buildAction(...), $options['action']); | ||
|
|
||
| foreach ($recipients as $recipient) { | ||
| try { | ||
| $io->writeln(sprintf('Recipient: %s', $recipient)); | ||
| $recipientLookupResult = $this->digitalPostHelper->lookupRecipient($recipient); | ||
| $actions = []; | ||
|
|
||
| $meMoMessage = $this->digitalPostHelper->getMeMoHelper()->buildMessage($recipientLookupResult, $senderLabel, | ||
| $messageLabel, $document, $actions); | ||
| if ($meMoVersion) { | ||
| $meMoMessage->setMemoVersion($meMoVersion); | ||
| } | ||
stankut marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $forsendelse = $this->digitalPostHelper->getForsendelseHelper()->buildForsendelse($recipientLookupResult, | ||
| $messageLabel, $document); | ||
|
|
||
|
|
@@ -122,7 +159,13 @@ public function send( | |
| $forsendelse | ||
| ); | ||
|
|
||
| $io->success(sprintf('Digital post sent to %s', $recipient)); | ||
| $io->definitionList( | ||
| ['Recipient' => $recipient], | ||
| ['Document' => sprintf('%s (%s)', $document->filename, $document->mimeType)], | ||
| ['MeMo version' => $meMoMessage->getMemoVersion()], | ||
| ); | ||
|
|
||
| $io->success(sprintf('Digital post sent to %s (MeMo %s)', $recipient, $meMoMessage->getMemoVersion())); | ||
| } | ||
| catch (\Throwable $throwable) { | ||
| $io->error(sprintf('Error sending digital post to %s:', $recipient)); | ||
|
|
@@ -158,4 +201,91 @@ private function dumpDigitalPostSettings(SymfonyStyle $io): void { | |
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * Build MeMo action. | ||
| * | ||
| * Lifted from KombiPostAfsendCommand::buildAction(). | ||
| * | ||
| * @see KombiPostAfsendCommand::buildAction() | ||
| */ | ||
| private function buildAction(string $spec): Action { | ||
| parse_str($spec, $options); | ||
| $resolver = $this->getActionOptionsResolver(); | ||
| try { | ||
| $options = $resolver->resolve($options); | ||
| } | ||
| catch (ExceptionInterface $exception) { | ||
| throw new InvalidOptionException(sprintf( | ||
| 'Invalid action %s: %s', | ||
| json_encode($spec), | ||
| $exception->getMessage() | ||
| )); | ||
| } | ||
|
|
||
| $action = (new Action()) | ||
| ->setActionCode($options['action']) | ||
| ->setLabel($options['label']); | ||
| if (SF1601::ACTION_AFTALE === $options['action']) { | ||
| $reservation = (new Reservation()) | ||
| ->setStartDateTime(new \DateTime('+2 days')) | ||
| ->setEndDateTime(new \DateTime('+2 days 1 hour')) | ||
| ->setLocation('Meeting room 1') | ||
| ->setAbstract('Abstract') | ||
| ->setDescription('Description') | ||
| ->setOrganizerName('Organizer') | ||
| ->setOrganizerMail('[email protected]') | ||
| ->setReservationUUID(Serializer::createUuid()); | ||
| $action->setReservation($reservation); | ||
| } | ||
| elseif ($options['entrypoint']) { | ||
| $action->setEntryPoint( | ||
| (new EntryPoint()) | ||
| ->setUrl($options['entrypoint']) | ||
| ); | ||
| } | ||
|
|
||
| if ($options['endDateTime']) { | ||
| $action->setEndDateTime(new \DateTime($options['endDateTime'])); | ||
| } | ||
|
|
||
| return $action; | ||
| } | ||
|
|
||
| /** | ||
| * Get actions options resolver. | ||
| * | ||
| * @see KombiPostAfsendCommand::getActionOptionsResolver() | ||
| */ | ||
| private function getActionOptionsResolver(): OptionsResolver { | ||
| $resolver = new OptionsResolver(); | ||
| $resolver | ||
| ->setRequired([ | ||
| 'action', | ||
| 'label', | ||
| ]) | ||
| ->setDefaults([ | ||
| 'endDateTime' => NULL, | ||
| 'entrypoint' => NULL, | ||
| ]) | ||
| ->setInfo('action', sprintf('The action name (one of %s)', implode(', ', SF1601::ACTIONS))) | ||
| ->setInfo('label', 'The action label') | ||
| ->setInfo('endDateTime', 'The end time e.g. "2022-12-02" or "14 days"') | ||
| ->setInfo('entrypoint', 'The entry point (an URL)') | ||
| ->setAllowedValues('action', static function ($value) { | ||
| return in_array($value, SF1601::ACTIONS, TRUE); | ||
| }) | ||
| ->setNormalizer('entrypoint', static function (Options $options, $value) { | ||
| if (NULL === $value && SF1601::ACTION_AFTALE !== $options['action']) { | ||
| throw new InvalidOptionsException(sprintf( | ||
| 'Action entrypoint is required for all actions but %s', | ||
| SF1601::ACTION_AFTALE | ||
| )); | ||
| } | ||
|
|
||
| return $value; | ||
| }); | ||
|
|
||
| return $resolver; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we leave a comment explaining that if no memo-version is provided, 1.2 is the default?