Skip to content

Commit 9abfcbd

Browse files
committed
OS-110 phpcs formatting
1 parent 51706b5 commit 9abfcbd

9 files changed

Lines changed: 121 additions & 69 deletions

File tree

modules/os2forms_digital_signature/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@ This module provides functionality for adding digital signature to the webform P
1111
1. Add the OS2forms attachment element to the form.
1212
2. Indicate that the OS2Forms attachment requires a digital signature.
1313
3. Add the Digital Signature Handler to the webform.
14-
4. If the form requires an email handler, ensure the trigger is set to **...when submission is locked** in the handler’s *Additional settings*.
14+
4. If the form requires an email handler, ensure the trigger is set to **...when submission is locked** in the handler’s
15+
*Additional settings*.
1516

1617
### Flow Explained
1718

1819
1. Upon form submission, a PDF is generated, saved in the private directory, and sent to the signature service via URL.
1920
2. The user is redirected to the signature service to provide their signature.
2021
3. After signing, the user is redirected back to the webform solution.
2122
4. The signed PDF is downloaded and stored in Drupal’s private directory.
22-
5. When a submission PDF is requested (e.g., via download link or email), the signed PDF is served instead of generating a new one on the fly.
23+
5. When a submission PDF is requested (e.g., via download link or email), the signed PDF is served instead of generating
24+
a new one on the fly.
2325

2426
## Settings page
2527

@@ -29,12 +31,10 @@ URL: `admin/os2forms_digital_signature/settings`
2931

3032
The URL of the service providing digital signature. This is the example of a known service https://signering.bellcom.dk/sign.php?
3133

32-
3334
- **Hash Salt used for signature**
3435

3536
Must match hash salt on the signature server
3637

37-
3838
- **List IPs which can download unsigned PDF submissions**
3939

4040
Only requests from this IP will be able to download PDF which are to be signed.

modules/os2forms_digital_signature/os2forms_digital_signature.module

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<?php
22

3+
/**
4+
* @file
5+
* This module enables Digital Signature functionality for Webforms.
6+
*/
7+
38
use Drupal\Core\Form\FormStateInterface;
49
use Drupal\Core\StreamWrapper\StreamWrapperManager;
510
use Drupal\os2forms_digital_signature\Form\SettingsForm;
@@ -33,7 +38,7 @@ function os2forms_digital_signature_webform_submission_form_alter(array &$form,
3338
$settings = $config->get('settings');
3439

3540
// Checking if the title has not been overridden.
36-
if ($settings['default_submit_button_label'] == $form['actions']['submit']['#value']){
41+
if ($settings['default_submit_button_label'] == $form['actions']['submit']['#value']) {
3742
$form['actions']['submit']['#value'] = t('Sign and submit');
3843
}
3944
}

modules/os2forms_digital_signature/os2forms_digital_signature.routing.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,3 @@ os2forms_digital_signature.settings:
1313
_title: 'Digital signature settings'
1414
requirements:
1515
_permission: 'administer site configuration'
16-

modules/os2forms_digital_signature/src/Controller/DigitalSignatureController.php

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
use Drupal\Core\File\FileSystemInterface;
77
use Drupal\Core\Url;
88
use Drupal\file\Entity\File;
9-
use Drupal\os2forms_digital_signature\Service\SigningService;
10-
use Drupal\webform\WebformSubmissionInterface;
119
use Symfony\Component\HttpFoundation\RedirectResponse;
1210
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1311

12+
/**
13+
* Digital Signature Controller.
14+
*/
1415
class DigitalSignatureController {
1516

1617
/**
@@ -29,14 +30,14 @@ public function __construct() {
2930
*
3031
* Expecting the file name to be coming as GET parameter.
3132
*
32-
* @param $uuid
33+
* @param string $uuid
3334
* Webform submission UUID.
34-
* @param $hash
35-
* Hash to check if the request is authentic.
36-
* @param $fid
37-
* File to replace (optional).
35+
* @param string $hash
36+
* Hash to check if the request is authentic.
37+
* @param int|null $fid
38+
* File to replace (optional).
3839
*
39-
* @return RedirectResponse
40+
* @return \Symfony\Component\HttpFoundation\RedirectResponse
4041
* Redirect response to form submission confirmation.
4142
*
4243
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
@@ -49,7 +50,7 @@ public function signCallback($uuid, $hash, $fid = NULL) {
4950
->loadByProperties(['uuid' => $uuid]);
5051

5152
// Since loadByProperties returns an array, we need to fetch the first item.
52-
/** @var WebformSubmissionInterface $webformSubmission */
53+
/** @var \Drupal\webform\WebformSubmissionInterface $webformSubmission */
5354
$webformSubmission = $submissions ? reset($submissions) : NULL;
5455
if (!$webformSubmission) {
5556
// Submission does not exist.
@@ -58,7 +59,7 @@ public function signCallback($uuid, $hash, $fid = NULL) {
5859

5960
$webformId = $webformSubmission->getWebform()->id();
6061

61-
// Checking the action
62+
// Checking the action.
6263
$action = \Drupal::request()->query->get('action');
6364
if ($action == 'cancel') {
6465
$cancelUrl = $webformSubmission->getWebform()->toUrl()->toString();
@@ -76,7 +77,7 @@ public function signCallback($uuid, $hash, $fid = NULL) {
7677
throw new NotFoundHttpException();
7778
}
7879

79-
/** @var SigningService $signingService */
80+
/** @var \Drupal\os2forms_digital_signature\Service\SigningService $signingService */
8081
$signingService = \Drupal::service('os2forms_digital_signature.signing_service');
8182

8283
$signedFilename = \Drupal::request()->get('file');
@@ -86,14 +87,16 @@ public function signCallback($uuid, $hash, $fid = NULL) {
8687
throw new NotFoundHttpException();
8788
}
8889

89-
/** @var FileSystemInterface $file_system */
90+
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
9091
$file_system = \Drupal::service('file_system');
9192

92-
// If $fid is present - we are replacing uploaded/managed file, otherwise creating a new one.
93+
// If $fid is present - we are replacing uploaded/managed file, otherwise
94+
// creating a new one.
9395
if ($fid) {
9496
$file = File::load($fid);
9597
$expectedFileUri = $file->getFileUri();
96-
} else {
98+
}
99+
else {
97100
// Prepare the directory to ensure it exists and is writable.
98101
$expectedFileUri = "private://webform/$webformId/digital_signature/$uuid.pdf";
99102
$directory = dirname($expectedFileUri);
@@ -105,7 +108,7 @@ public function signCallback($uuid, $hash, $fid = NULL) {
105108

106109
// Write the data to the file using Drupal's file system service.
107110
try {
108-
$file_system->saveData($signedFileContent, $expectedFileUri , FileSystemInterface::EXISTS_REPLACE);
111+
$file_system->saveData($signedFileContent, $expectedFileUri, FileSystemInterface::EXISTS_REPLACE);
109112

110113
// Updating webform submission.
111114
$webformSubmission->setLocked(TRUE);
@@ -117,7 +120,11 @@ public function signCallback($uuid, $hash, $fid = NULL) {
117120
}
118121
}
119122
catch (\Exception $e) {
120-
$this->logger->error('Failed to write to file %uri: @message', ['%uri' => $expectedFileUri, '@message' => $e->getMessage()]);
123+
$this->logger->error('Failed to write to file %uri: @message',
124+
[
125+
'%uri' => $expectedFileUri,
126+
'@message' => $e->getMessage(),
127+
]);
121128
}
122129

123130
// Build the URL for the webform submission confirmation page.

modules/os2forms_digital_signature/src/Element/DigitalSignatureDocument.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
namespace Drupal\os2forms_digital_signature\Element;
44

5-
65
use Drupal\webform\Element\WebformManagedFileBase;
76

87
/**
9-
* Provides a webform element for an 'os2forms_digital_signature_document' element.
8+
* Provides a element for an 'os2forms_digital_signature_document' element.
109
*
1110
* @FormElement("os2forms_digital_signature_document")
1211
*/

modules/os2forms_digital_signature/src/Form/SettingsForm.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,5 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
7979

8080
parent::submitForm($form, $form_state);
8181
}
82+
8283
}

modules/os2forms_digital_signature/src/Plugin/WebformElement/DigitalSignatureDocument.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,10 @@ public function getItemFormats() {
3333
/**
3434
* {@inheritdoc}
3535
*/
36-
protected function getFileExtensions(array $element = NULL) {
36+
protected function getFileExtensions(?array $element = NULL) {
3737
return 'pdf';
3838
}
3939

40-
4140
/**
4241
* {@inheritdoc}
4342
*/

modules/os2forms_digital_signature/src/Plugin/WebformHandler/DigitalSignatureWebformHandler.php

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@
66
use Drupal\Core\File\FileExists;
77
use Drupal\Core\File\FileSystemInterface;
88
use Drupal\Core\Url;
9-
use Drupal\file\FileInterface;
10-
use Drupal\os2forms_digital_signature\Service\SigningService;
119
use Drupal\webform\Plugin\WebformHandlerBase;
1210
use Drupal\webform\WebformSubmissionInterface;
1311
use 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

Comments
 (0)