forked from OS2Forms/os2forms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractMessageHelper.php
More file actions
93 lines (80 loc) · 3.53 KB
/
AbstractMessageHelper.php
File metadata and controls
93 lines (80 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
namespace Drupal\os2forms_digital_post\Helper;
use DigitalPost\MeMo\Message;
use Drupal\Core\Render\ElementInfoManager;
use Drupal\os2forms_digital_post\EventSubscriber\Os2formsDigitalPostSubscriber;
use Drupal\os2forms_digital_post\Exception\InvalidAttachmentElementException;
use Drupal\os2forms_digital_post\Model\Document;
use Drupal\os2forms_digital_post\Plugin\WebformHandler\WebformHandlerSF1601;
use Drupal\os2web_datalookup\LookupResult\CompanyLookupResult;
use Drupal\os2web_datalookup\LookupResult\CprLookupResult;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\webform\WebformTokenManagerInterface;
use Drupal\webform_attachment\Element\WebformAttachmentBase;
use ItkDev\Serviceplatformen\Service\SF1601\Serializer;
use Oio\Fjernprint\ForsendelseI;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
/**
* Abstract message helper.
*/
abstract class AbstractMessageHelper {
/**
* Constructor.
*/
public function __construct(
readonly protected Settings $settings,
#[Autowire(service: 'plugin.manager.element_info')]
readonly protected ElementInfoManager $elementInfoManager,
#[Autowire(service: 'webform.token_manager')]
readonly protected WebformTokenManagerInterface $webformTokenManager,
readonly protected Os2formsDigitalPostSubscriber $digitalPostSubscriber,
) {
}
/**
* Get main document.
*
* @see WebformAttachmentController::download()
*
* @phpstan-param array<string, mixed> $handlerSettings
*/
protected function getMainDocument(WebformSubmissionInterface $submission, array $handlerSettings, CprLookupResult|CompanyLookupResult $recipientData): Document {
// Lifted from Drupal\webform_attachment\Controller\WebformAttachmentController::download.
$element = $handlerSettings[WebformHandlerSF1601::MEMO_MESSAGE][WebformHandlerSF1601::ATTACHMENT_ELEMENT];
$element = $submission->getWebform()->getElement($element) ?: [];
[$type] = explode(':', $element['#type']);
$instance = $this->elementInfoManager->createInstance($type);
if (!$instance instanceof WebformAttachmentBase) {
throw new InvalidAttachmentElementException(sprintf('Attachment element must be an instance of %s. Found %s.', WebformAttachmentBase::class, get_class($instance)));
}
$fileName = $instance::getFileName($element, $submission);
$mimeType = $instance::getFileMimeType($element, $submission);
// The way to alter html generated from entities is through the
// PrintEvents::POST_RENDER event. See:
// @Drupal\entity_print\Renderer::generateHtml,
// To indicate digital post context and get the necessary information,
// we add a flag to the session.
$senderAddress = $handlerSettings[WebformHandlerSF1601::MEMO_MESSAGE][WebformHandlerSF1601::SENDER_ADDRESS] ?? '';
$this->digitalPostSubscriber->setDigitalPostContext($submission, $recipientData, $senderAddress);
$content = $instance::getFileContent($element, $submission);
$this->digitalPostSubscriber->deleteDigitalPostContext($submission);
return new Document(
$content,
$mimeType,
$fileName
);
}
/**
* Replace tokens.
*/
protected function replaceTokens(string $text, WebformSubmissionInterface $submission): string {
return $this->webformTokenManager->replace($text, $submission);
}
/**
* Convert MeMo message to DOM document.
*/
public function message2dom(Message|ForsendelseI $message): \DOMDocument {
$document = new \DOMDocument();
$document->loadXML((new Serializer())->serialize($message));
return $document;
}
}