66use Drupal \Core \File \FileExists ;
77use Drupal \Core \File \FileSystemInterface ;
88use Drupal \Core \Url ;
9- use Drupal \file \FileInterface ;
10- use Drupal \os2forms_digital_signature \Service \SigningService ;
119use Drupal \webform \Plugin \WebformHandlerBase ;
1210use Drupal \webform \WebformSubmissionInterface ;
1311use Symfony \Component \DependencyInjection \ContainerInterface ;
1412
1513/**
16- * Webform submission debug handler.
14+ * Digital signature webform handler.
1715 *
1816 * @WebformHandler(
1917 * id = "os2forms_digital_signature",
@@ -68,8 +66,8 @@ public static function create(ContainerInterface $container, array $configuratio
6866 }
6967
7068 /**
71- * {@inheritdoc}
72- */
69+ * {@inheritdoc}
70+ */
7371 public function preSave (WebformSubmissionInterface $ webform_submission ) {
7472 $ webform = $ webform_submission ->getWebform ();
7573
@@ -79,7 +77,12 @@ public function preSave(WebformSubmissionInterface $webform_submission) {
7977
8078 $ attachment = $ this ->getSubmissionAttachment ($ webform_submission );
8179 if (!$ attachment ) {
82- $ this ->logger ->error ('Attachment cannot be created webform: %webform, webform_submission: %webform_submission ' , ['%webform ' => $ webform ->id (), '%webform_submission ' => $ webform_submission ->uuid ()]);
80+ $ this ->logger ->error ('Attachment cannot be created webform: %webform, webform_submission: %webform_submission ' ,
81+ [
82+ '%webform ' => $ webform ->id (),
83+ '%webform_submission ' => $ webform_submission ->uuid (),
84+ ]
85+ );
8386 return ;
8487 }
8588
@@ -89,25 +92,29 @@ public function preSave(WebformSubmissionInterface $webform_submission) {
8992 return ;
9093 }
9194
92- $ fileUri = $ destinationDir . '/ ' . $ webform_submission ->uuid () .'.pdf ' ;
95+ $ fileUri = $ destinationDir . '/ ' . $ webform_submission ->uuid () . '.pdf ' ;
9396
9497 // Save the file data.
9598 try {
96- /** @var FileInterface $fileToSign */
99+ /** @var \Drupal\file\ FileInterface $fileToSign */
97100 $ fileToSign = \Drupal::service ('file.repository ' )->writeData ($ attachment ['filecontent ' ], $ fileUri , FileExists::Replace);
98101 }
99102 catch (\Exception $ e ) {
100- $ this ->logger ->error ('File cannot be saved: %fileUri, error: %error ' , ['%fileUri ' => $ fileUri , '%error ' => $ e ->getMessage ()]);
103+ $ this ->logger ->error ('File cannot be saved: %fileUri, error: %error ' ,
104+ [
105+ '%fileUri ' => $ fileUri ,
106+ '%error ' => $ e ->getMessage (),
107+ ]);
101108 return ;
102109 }
103110
104111 $ fileToSign ->save ();
105112 $ fileToSignPublicUrl = \Drupal::service ('file_url_generator ' )->generateAbsoluteString ($ fileToSign ->getFileUri ());
106113
107- /** @var SigningService $signingService */
114+ /** @var \Drupal\os2forms_digital_signature\Service\ SigningService $signingService */
108115 $ signingService = \Drupal::service ('os2forms_digital_signature.signing_service ' );
109116
110- $ cid = $ signingService ->get_cid ();
117+ $ cid = $ signingService ->getCid ();
111118 if (empty ($ cid )) {
112119 $ this ->logger ->error ('Failed to obtain cid. Is server running? ' );
113120 return ;
@@ -118,20 +125,28 @@ public function preSave(WebformSubmissionInterface $webform_submission) {
118125 $ hash = Crypt::hashBase64 ($ webform_submission ->uuid () . $ webform ->id () . $ salt );
119126
120127 $ attachmentFid = $ attachment ['fid ' ] ?? NULL ;
121- $ signatureCallbackUrl = Url::fromRoute ('os2forms_digital_signature.sign_callback ' , ['uuid ' => $ webform_submission ->uuid (), 'hash ' => $ hash , 'fid ' => $ attachmentFid ]);
122-
123- // Starting signing, if everything is correct - this funcition will start redirect.
128+ $ signatureCallbackUrl = Url::fromRoute ('os2forms_digital_signature.sign_callback ' ,
129+ [
130+ 'uuid ' => $ webform_submission ->uuid (),
131+ 'hash ' => $ hash ,
132+ 'fid ' => $ attachmentFid ,
133+ ]
134+ );
135+
136+ // Starting signing, if everything is correct - this funcition will start
137+ // redirect.
124138 $ signingService ->sign ($ fileToSignPublicUrl , $ cid , $ signatureCallbackUrl ->setAbsolute ()->toString ());
125139 }
126140
127141 /**
128142 * Get OS2forms file attachment.
129143 *
130144 * @param \Drupal\webform\WebformSubmissionInterface $webform_submission
131- * A webform submission.
145+ * A webform submission.
132146 *
133147 * @return array|null
134- * Array of attachment data.
148+ * Array of attachment data.
149+ *
135150 * @throws \Exception
136151 */
137152 protected function getSubmissionAttachment (WebformSubmissionInterface $ webform_submission ) {
@@ -140,8 +155,8 @@ protected function getSubmissionAttachment(WebformSubmissionInterface $webform_s
140155
141156 // Getting all element types that are added to the webform.
142157 //
143- // Priority is the following: check for os2forms_digital_signature_document, is not found try serving
144- // os2forms_attachment
158+ // Priority is the following: check for os2forms_digital_signature_document,
159+ // is not found try serving os2forms_attachment.
145160 $ elementTypes = array_column ($ this ->getWebform ()->getElementsDecodedAndFlattened (), '#type ' );
146161 $ attachmentType = '' ;
147162 if (in_array ('os2forms_digital_signature_document ' , $ elementTypes )) {
@@ -154,7 +169,8 @@ protected function getSubmissionAttachment(WebformSubmissionInterface $webform_s
154169 $ elements = $ this ->getWebform ()->getElementsInitializedAndFlattened ();
155170 $ element_attachments = $ this ->getWebform ()->getElementsAttachments ();
156171 foreach ($ element_attachments as $ element_attachment ) {
157- // Check if the element attachment key is excluded and should not attach any files.
172+ // Check if the element attachment key is excluded and should not attach
173+ // any files.
158174 if (isset ($ this ->configuration ['excluded_elements ' ][$ element_attachment ])) {
159175 continue ;
160176 }
0 commit comments