Skip to content

Commit e01f0c1

Browse files
committed
OS-110 refactoring
1 parent 9ae977f commit e01f0c1

File tree

3 files changed

+79
-26
lines changed

3 files changed

+79
-26
lines changed

modules/os2forms_digital_signature/src/Form/SettingsForm.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ protected function getEditableConfigNames() {
3737
* {@inheritdoc}
3838
*/
3939
public function buildForm(array $form, FormStateInterface $form_state) {
40-
$form['os2forms_digital_signature_remove_service_url'] = [
40+
$form['os2forms_digital_signature_remote_service_url'] = [
4141
'#type' => 'textfield',
4242
'#title' => $this->t('Signature server URL'),
43-
'#default_value' => $this->config(self::$configName)->get('os2forms_digital_signature_remove_service_url'),
43+
'#default_value' => $this->config(self::$configName)->get('os2forms_digital_signature_remote_service_url'),
4444
'#description' => $this->t('E.g. https://signering.bellcom.dk/sign.php?'),
4545
];
4646
$form['os2forms_digital_signature_sign_hash_salt'] = [

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

Lines changed: 55 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,18 @@
33
namespace Drupal\os2forms_digital_signature\Plugin\WebformHandler;
44

55
use Drupal\Component\Utility\Crypt;
6+
use Drupal\Core\Extension\ModuleHandlerInterface;
67
use Drupal\Core\File\FileExists;
78
use Drupal\Core\File\FileSystemInterface;
9+
use Drupal\Core\File\FileUrlGeneratorInterface;
10+
use Drupal\Core\Site\Settings;
811
use Drupal\Core\Url;
12+
use Drupal\file\FileRepositoryInterface;
13+
use Drupal\os2forms_digital_signature\Service\SigningService;
14+
use Drupal\webform\Plugin\WebformElementManagerInterface;
915
use Drupal\webform\Plugin\WebformHandlerBase;
1016
use Drupal\webform\WebformSubmissionInterface;
17+
use Psr\Log\LoggerInterface;
1118
use Symfony\Component\DependencyInjection\ContainerInterface;
1219

1320
/**
@@ -30,29 +37,56 @@ class DigitalSignatureWebformHandler extends WebformHandlerBase {
3037
*
3138
* @var \Drupal\Core\Extension\ModuleHandlerInterface
3239
*/
33-
protected $moduleHandler;
40+
private readonly ModuleHandlerInterface $moduleHandler;
3441

3542
/**
3643
* The webform element plugin manager.
3744
*
3845
* @var \Drupal\webform\Plugin\WebformElementManagerInterface
3946
*/
40-
protected $elementManager;
47+
private readonly WebformElementManagerInterface $elementManager;
4148

4249
/**
4350
* Logger for channel - os2forms_digital_signature.
4451
*
45-
* @var \Drupal\Core\Logger\LoggerChannelInterface
52+
* @var \Psr\Log\LoggerInterface
4653
*/
47-
protected $logger;
54+
private readonly LoggerInterface $logger;
4855

4956
/**
50-
* {@inheritdoc}
57+
* File system interface.
58+
*
59+
* @var \Drupal\Core\File\FileSystemInterface
5160
*/
52-
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
53-
parent::__construct($configuration, $plugin_id, $plugin_definition);
54-
$this->logger = \Drupal::logger('os2forms_digital_signature');
55-
}
61+
private readonly FileSystemInterface $fileSystem;
62+
63+
/**
64+
* File repository.
65+
*
66+
* @var \Drupal\file\FileRepositoryInterface
67+
*/
68+
private readonly FileRepositoryInterface $fileRepository;
69+
70+
/**
71+
* File URL generator.
72+
*
73+
* @var \Drupal\Core\File\FileUrlGeneratorInterface
74+
*/
75+
private readonly FileUrlGeneratorInterface $fileUrlGenerator;
76+
77+
/**
78+
* OS2Forms signing service.
79+
*
80+
* @var \Drupal\os2forms_digital_signature\Service\SigningService
81+
*/
82+
private readonly SigningService $signingService;
83+
84+
/**
85+
* Settings service.
86+
*
87+
* @var \Drupal\Core\Site\Settings
88+
*/
89+
private readonly Settings $settings;
5690

5791
/**
5892
* {@inheritdoc}
@@ -61,6 +95,12 @@ public static function create(ContainerInterface $container, array $configuratio
6195
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
6296
$instance->moduleHandler = $container->get('module_handler');
6397
$instance->elementManager = $container->get('plugin.manager.webform.element');
98+
$instance->logger = $container->get('logger.channel.os2forms_digital_signature');
99+
$instance->fileSystem = $container->get('file_system');
100+
$instance->fileRepository = $container->get('file.repository');
101+
$instance->fileUrlGenerator = $container->get('file_url_generator');
102+
$instance->signingService = $container->get('os2forms_digital_signature.signing_service');
103+
$instance->settings = $container->get('settings');
64104

65105
return $instance;
66106
}
@@ -87,7 +127,7 @@ public function preSave(WebformSubmissionInterface $webform_submission) {
87127
}
88128

89129
$destinationDir = 'private://signing';
90-
if (!\Drupal::service('file_system')->prepareDirectory($destinationDir, FileSystemInterface::CREATE_DIRECTORY)) {
130+
if (!$this->fileSystem->prepareDirectory($destinationDir, FileSystemInterface::CREATE_DIRECTORY)) {
91131
$this->logger->error('File directory cannot be created: %filedirectory', ['%filedirectory' => $destinationDir]);
92132
return;
93133
}
@@ -96,8 +136,7 @@ public function preSave(WebformSubmissionInterface $webform_submission) {
96136

97137
// Save the file data.
98138
try {
99-
/** @var \Drupal\file\FileInterface $fileToSign */
100-
$fileToSign = \Drupal::service('file.repository')->writeData($attachment['filecontent'], $fileUri, FileExists::Replace);
139+
$fileToSign = $this->fileRepository->writeData($attachment['filecontent'], $fileUri, FileExists::Replace);
101140
}
102141
catch (\Exception $e) {
103142
$this->logger->error('File cannot be saved: %fileUri, error: %error',
@@ -109,19 +148,16 @@ public function preSave(WebformSubmissionInterface $webform_submission) {
109148
}
110149

111150
$fileToSign->save();
112-
$fileToSignPublicUrl = \Drupal::service('file_url_generator')->generateAbsoluteString($fileToSign->getFileUri());
113-
114-
/** @var \Drupal\os2forms_digital_signature\Service\SigningService $signingService */
115-
$signingService = \Drupal::service('os2forms_digital_signature.signing_service');
151+
$fileToSignPublicUrl = $this->fileUrlGenerator->generateAbsoluteString($fileToSign->getFileUri());
116152

117-
$cid = $signingService->getCid();
153+
$cid = $this->signingService->getCid();
118154
if (empty($cid)) {
119155
$this->logger->error('Failed to obtain cid. Is server running?');
120156
return;
121157
}
122158

123159
// Creating hash.
124-
$salt = \Drupal::service('settings')->get('hash_salt');
160+
$salt = $this->settings->get('hash_salt');
125161
$hash = Crypt::hashBase64($webform_submission->uuid() . $webform->id() . $salt);
126162

127163
$attachmentFid = $attachment['fid'] ?? NULL;
@@ -135,7 +171,7 @@ public function preSave(WebformSubmissionInterface $webform_submission) {
135171

136172
// Starting signing, if everything is correct - this funcition will start
137173
// redirect.
138-
$signingService->sign($fileToSignPublicUrl, $cid, $signatureCallbackUrl->setAbsolute()->toString());
174+
$this->signingService->sign($fileToSignPublicUrl, $cid, $signatureCallbackUrl->setAbsolute()->toString());
139175
}
140176

141177
/**

modules/os2forms_digital_signature/src/Service/SigningService.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ class SigningService {
2929
*
3030
* @var \Drupal\Core\Entity\EntityStorageInterface
3131
*/
32-
protected EntityStorageInterface $webformStorage;
32+
private readonly EntityStorageInterface $webformStorage;
3333

3434
/**
3535
* WebformSubmission storage.
3636
*
3737
* @var \Drupal\Core\Entity\EntityStorageInterface
3838
*/
39-
protected EntityStorageInterface $webformSubmissionStorage;
39+
private readonly EntityStorageInterface $webformSubmissionStorage;
4040

4141
public function __construct(
4242
private readonly ClientInterface $httpClient,
@@ -57,7 +57,7 @@ public function __construct(
5757
* The correlation id.
5858
*/
5959
public function getCid() : ?string {
60-
$url = $this->config->get('os2forms_digital_signature_remove_service_url') . 'action=getcid';
60+
$url = $this->getServiceUrl() . http_build_query(['action' => 'getcid']);
6161
$response = $this->httpClient->request('GET', $url);
6262
$result = $response->getBody()->getContents();
6363

@@ -104,7 +104,7 @@ public function sign(string $document_uri, string $cid, string $forward_url):voi
104104
'uri' => base64_encode($document_uri),
105105
'forward_url' => base64_encode($forward_url),
106106
];
107-
$url = $this->config->get('os2forms_digital_signature_remove_service_url') . http_build_query($params);
107+
$url = $this->getServiceUrl() . http_build_query($params);
108108

109109
$response = new RedirectResponse($url);
110110
$response->send();
@@ -145,7 +145,7 @@ public function download(string $filename, $leave = FALSE, $annotate = FALSE, $a
145145
'annotate' => $annotate,
146146
'attributes' => $attributes,
147147
];
148-
$url = $this->config->get('os2forms_digital_signature_remove_service_url') . http_build_query($params);
148+
$url = $this->getServiceUrl() . http_build_query($params);
149149

150150
$response = $this->httpClient->request('GET', $url);
151151
$return = $response->getBody()->getContents();
@@ -239,4 +239,21 @@ public function deleteStalledSubmissions() : void {
239239
}
240240
}
241241

242+
/**
243+
* Returns Remove service URL.
244+
*
245+
* @return string
246+
* Remote Service URL, if missing '?' or '&', '?' will be added
247+
* automatically.
248+
*/
249+
public function getServiceUrl() : string {
250+
$url = $this->config->get('os2forms_digital_signature_remote_service_url');
251+
// Handling URL, if it does not end with '?' or '&'.
252+
if (!str_ends_with($url, '?') && !str_ends_with($url, '&')) {
253+
return $url . '?';
254+
}
255+
256+
return $url;
257+
}
258+
242259
}

0 commit comments

Comments
 (0)