33namespace Drupal \os2forms_digital_post \Helper ;
44
55use Drupal \os2forms_cpr_lookup \CPR \CprServiceResult ;
6+ use Drupal \os2forms_digital_post \Exception \CprElementNotFoundInSubmissionException ;
67use Drupal \webform \WebformSubmissionInterface ;
78use Drupal \Core \Entity \EntityTypeManagerInterface ;
89use Drupal \Core \Render \Renderer ;
10+ use Drupal \os2forms_cpr_lookup \Service \CprServiceInterface ;
11+ use Drupal \os2forms_digital_post \Consumer \PrintServiceConsumer ;
12+ use Drupal \os2forms_digital_post \Manager \TemplateManager ;
913
1014/**
1115 * Webform helper.
@@ -26,12 +30,36 @@ final class WebformHelper {
2630 */
2731 protected $ renderer ;
2832
33+ /**
34+ * The cpr service.
35+ *
36+ * @var \Drupal\os2forms_cpr_lookup\Service\CprServiceInterface
37+ */
38+ protected $ cprService ;
39+
40+ /**
41+ * The print service consumer.
42+ *
43+ * @var \Drupal\os2forms_digital_post\Consumer\PrintServiceConsumer
44+ */
45+ protected $ printServiceConsumer ;
46+
47+ /**
48+ * The template manager.
49+ *
50+ * @var \Drupal\os2forms_digital_post\Manager\TemplateManager
51+ */
52+ protected $ templateManager ;
53+
2954 /**
3055 * Constructor.
3156 */
32- public function __construct (EntityTypeManagerInterface $ entity_type_manager , Renderer $ renderer ) {
57+ public function __construct (EntityTypeManagerInterface $ entity_type_manager , Renderer $ renderer, CprServiceInterface $ cprService , PrintServiceConsumer $ printServiceConsumer , TemplateManager $ templateManager ) {
3358 $ this ->entityTypeManager = $ entity_type_manager ;
3459 $ this ->renderer = $ renderer ;
60+ $ this ->cprService = $ cprService ;
61+ $ this ->printServiceConsumer = $ printServiceConsumer ;
62+ $ this ->templateManager = $ templateManager ;
3563 }
3664
3765 /**
@@ -68,4 +96,80 @@ public function getTemplateContext(WebformSubmissionInterface $webformSubmission
6896 ];
6997 }
7098
99+ public function sendDigitalPost (string $ submissionId , array $ handlerConfiguration ) {
100+ /** @var \Drupal\webform\Entity\WebformSubmission $submission */
101+ $ webform_submission = $ this ->getSubmission ($ submissionId );
102+
103+ $ submissionData = $ webform_submission ->getData ();
104+
105+ if (!array_key_exists ($ handlerConfiguration ['cpr_element ' ], $ submissionData )) {
106+ $ this ->getLogger ()->error (
107+ 'The chosen CPR element not found in submission! '
108+ );
109+
110+ throw new CprElementNotFoundInSubmissionException ();
111+ }
112+
113+ /** @var \Drupal\os2forms_cpr_lookup\CPR\CprServiceResult $cprSearchResult */
114+ $ cprSearchResult = $ this ->cprService ->search ($ submissionData [$ handlerConfiguration ['cpr_element ' ]]);
115+
116+ $ context = $ this ->getTemplateContext ($ webform_submission , $ cprSearchResult , $ handlerConfiguration );
117+
118+ if (TRUE === $ handlerConfiguration ['debug ' ]) {
119+ $ this ->templateManager ->renderPdf ($ handlerConfiguration ['template ' ], $ context , TRUE );
120+ return ;
121+ }
122+
123+ $ result = FALSE ;
124+
125+ switch ($ handlerConfiguration ['channel ' ]) {
126+ case 'A ' :
127+ $ result = $ this ->printServiceConsumer ->afsendBrevPerson (
128+ $ handlerConfiguration ['channel ' ],
129+ $ handlerConfiguration ['priority ' ],
130+ $ submissionData [$ handlerConfiguration ['cpr_element ' ]],
131+ $ cprSearchResult ->getName (),
132+ NULL ,
133+ $ cprSearchResult ->getStreetName (),
134+ $ cprSearchResult ->getHouseNumber (),
135+ $ floor ,
136+ NULL ,
137+ NULL ,
138+ $ cprSearchResult ->getPostalCode (),
139+ NULL ,
140+ NULL ,
141+ 'DK ' ,
142+ 'PDF ' ,
143+ $ this ->templateManager ->renderPdf ($ handlerConfiguration ['template ' ], $ context ),
144+ $ handlerConfiguration ['document_title ' ]
145+ );
146+ break ;
147+
148+ case 'D ' :
149+ $ result = $ this ->printServiceConsumer ->afsendDigitalPostPerson (
150+ $ handlerConfiguration ['channel ' ],
151+ $ handlerConfiguration ['priority ' ],
152+ $ submissionData [$ handlerConfiguration ['cpr_element ' ]],
153+ 'PDF ' ,
154+ $ this ->templateManager ->renderPdf ($ handlerConfiguration ['template ' ], $ context ),
155+ $ handlerConfiguration ['document_title ' ]
156+ );
157+ break ;
158+ }
159+
160+ if (FALSE === $ result ) {
161+ // Throw an error?
162+ }
163+ }
164+
165+ /**
166+ * Gets WebformSubmission from id.
167+ *
168+ * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
169+ * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
170+ */
171+ private function getSubmission (string $ submissionId ) {
172+ $ storage = $ this ->entityTypeManager ->getStorage ('webform_submission ' );
173+ return $ storage ->load ($ submissionId );
174+ }
71175}
0 commit comments