Skip to content

Commit b7e53b9

Browse files
committed
OS-110 proper dependency injections
1 parent 6d6af9f commit b7e53b9

File tree

6 files changed

+89
-32
lines changed

6 files changed

+89
-32
lines changed

modules/os2forms_digital_signature/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ URL: `admin/os2forms_digital_signature/settings`
3535
Must match hash salt on the signature server
3636

3737

38-
- **List IP's which can download unsigned PDF submissions**
38+
- **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.
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
11
services:
2+
logger.channel.os2forms_digital_signature:
3+
parent: logger.channel_base
4+
arguments: [ 'os2forms_digital_signature' ]
5+
26
os2forms_digital_signature.signing_service:
37
class: Drupal\os2forms_digital_signature\Service\SigningService
4-
arguments: ['@config.factory']
8+
arguments:
9+
- '@http_client'
10+
- '@config.factory'
11+
- '@entity_type.manager'
12+
- '@logger.channel.os2forms_digital_signature'

modules/os2forms_digital_signature/src/Controller/DigitalSignatureController.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,17 @@
1313

1414
class DigitalSignatureController {
1515

16+
/**
17+
* Logger for channel - os2forms_digital_signature.
18+
*
19+
* @var \Drupal\Core\Logger\LoggerChannelInterface
20+
*/
21+
protected $logger;
22+
23+
public function __construct() {
24+
$this->logger = \Drupal::logger('os2forms_digital_signature');
25+
}
26+
1627
/**
1728
* Callback for the file being signed.
1829
*
@@ -71,7 +82,7 @@ public function signCallback($uuid, $hash, $fid = NULL) {
7182
$signedFilename = \Drupal::request()->get('file');
7283
$signedFileContent = $signingService->download($signedFilename);
7384
if (!$signedFileContent) {
74-
\Drupal::logger('os2forms_digital_signature')->warning('Missing file on remote server %file.', ['%file' => $signedFilename]);
85+
$this->logger->warning('Missing file on remote server %file.', ['%file' => $signedFilename]);
7586
throw new NotFoundHttpException();
7687
}
7788

@@ -88,7 +99,7 @@ public function signCallback($uuid, $hash, $fid = NULL) {
8899
$directory = dirname($expectedFileUri);
89100

90101
if (!$file_system->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY)) {
91-
\Drupal::logger('os2forms_digital_signature')->error('Failed to prepare directory %directory.', ['%directory' => $directory]);
102+
$this->logger->error('Failed to prepare directory %directory.', ['%directory' => $directory]);
92103
}
93104
}
94105

@@ -106,7 +117,7 @@ public function signCallback($uuid, $hash, $fid = NULL) {
106117
}
107118
}
108119
catch (\Exception $e) {
109-
\Drupal::logger('os2forms_digital_signature')->error('Failed to write to file %uri: @message', ['%uri' => $expectedFileUri, '@message' => $e->getMessage()]);
120+
$this->logger->error('Failed to write to file %uri: @message', ['%uri' => $expectedFileUri, '@message' => $e->getMessage()]);
110121
}
111122

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

modules/os2forms_digital_signature/src/Form/SettingsForm.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
use Drupal\Core\Form\ConfigFormBase;
66
use Drupal\Core\Form\FormStateInterface;
7+
use Drupal\Core\StringTranslation\StringTranslationTrait;
78

89
/**
910
* Digital post settings form.
1011
*/
1112
class SettingsForm extends ConfigFormBase {
13+
use StringTranslationTrait;
1214

1315
/**
1416
* Name of the config.
@@ -37,27 +39,27 @@ protected function getEditableConfigNames() {
3739
public function buildForm(array $form, FormStateInterface $form_state) {
3840
$form['os2forms_digital_signature_remove_service_url'] = [
3941
'#type' => 'textfield',
40-
'#title' => t("Signature server URL"),
42+
'#title' => $this->t('Signature server URL'),
4143
'#default_value' => $this->config(self::$configName)->get('os2forms_digital_signature_remove_service_url'),
42-
'#description' => t('E.g. https://signering.bellcom.dk/sign.php?'),
44+
'#description' => $this->t('E.g. https://signering.bellcom.dk/sign.php?'),
4345
];
4446
$form['os2forms_digital_signature_sign_hash_salt'] = [
4547
'#type' => 'textfield',
46-
'#title' => t("Hash Salt used for signature"),
48+
'#title' => $this->t('Hash Salt used for signature'),
4749
'#default_value' => $this->config(self::$configName)->get('os2forms_digital_signature_sign_hash_salt'),
48-
'#description' => t('Must match hash salt on the signature server'),
50+
'#description' => $this->t('Must match hash salt on the signature server'),
4951
];
5052
$form['os2forms_digital_signature_submission_allowed_ips'] = [
5153
'#type' => 'textfield',
52-
'#title' => t("List IP's which can download unsigned PDF submissions"),
54+
'#title' => $this->t('List IPs which can download unsigned PDF submissions'),
5355
'#default_value' => $this->config(self::$configName)->get('os2forms_digital_signature_submission_allowed_ips'),
54-
'#description' => t('Comma separated. Ex. 192.168.1.1,192.168.2.1'),
56+
'#description' => $this->t('Comma separated. e.g. 192.168.1.1,192.168.2.1'),
5557
];
5658
$form['os2forms_digital_signature_submission_retention_period'] = [
5759
'#type' => 'textfield',
58-
'#title' => t('Unsigned submission timespan (s)'),
60+
'#title' => $this->t('Unsigned submission timespan (s)'),
5961
'#default_value' => ($this->config(self::$configName)->get('os2forms_digital_signature_submission_retention_period')) ?? 300,
60-
'#description' => t('How many seconds can unsigned submission exist before being automatically deleted'),
62+
'#description' => $this->t('How many seconds can unsigned submission exist before being automatically deleted'),
6163
];
6264

6365
return parent::buildForm($form, $form_state);

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,29 @@ class DigitalSignatureWebformHandler extends WebformHandlerBase {
4040
*/
4141
protected $elementManager;
4242

43+
/**
44+
* Logger for channel - os2forms_digital_signature.
45+
*
46+
* @var \Drupal\Core\Logger\LoggerChannelInterface
47+
*/
48+
protected $logger;
49+
50+
/**
51+
* {@inheritdoc}
52+
*/
53+
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
54+
parent::__construct($configuration, $plugin_id, $plugin_definition);
55+
$this->logger = \Drupal::logger('os2forms_digital_signature');
56+
}
57+
4358
/**
4459
* {@inheritdoc}
4560
*/
4661
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
4762
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
4863
$instance->moduleHandler = $container->get('module_handler');
4964
$instance->elementManager = $container->get('plugin.manager.webform.element');
65+
5066
return $instance;
5167
}
5268

@@ -62,13 +78,13 @@ public function preSave(WebformSubmissionInterface $webform_submission) {
6278

6379
$attachment = $this->getSubmissionAttachment($webform_submission);
6480
if (!$attachment) {
65-
\Drupal::logger('os2forms_digital_signature')->error('Attachment cannot be created webform: %webform, webform_submission: %webform_submission', ['%webform' => $webform->id(), '%webform_submission' => $webform_submission->uuid()]);
81+
$this->logger->error('Attachment cannot be created webform: %webform, webform_submission: %webform_submission', ['%webform' => $webform->id(), '%webform_submission' => $webform_submission->uuid()]);
6682
return;
6783
}
6884

6985
$destinationDir = 'private://signing';
7086
if (!\Drupal::service('file_system')->prepareDirectory($destinationDir, FileSystemInterface::CREATE_DIRECTORY)) {
71-
\Drupal::logger('os2forms_digital_signature')->error('File directory cannot be created: %filedirectory', ['%filedirectory' => $destinationDir]);
87+
$this->logger->error('File directory cannot be created: %filedirectory', ['%filedirectory' => $destinationDir]);
7288
return;
7389
}
7490

@@ -80,7 +96,7 @@ public function preSave(WebformSubmissionInterface $webform_submission) {
8096
$fileToSign = \Drupal::service('file.repository')->writeData($attachment['filecontent'], $fileUri, FileSystemInterface::EXISTS_REPLACE);
8197
}
8298
catch (\Exception $e) {
83-
\Drupal::logger('os2forms_digital_signature')->error('File cannot be saved: %fileUri, error: %error', ['%fileUri' => $fileUri, '%error' => $e->getMessage()]);
99+
$this->logger->error('File cannot be saved: %fileUri, error: %error', ['%fileUri' => $fileUri, '%error' => $e->getMessage()]);
84100
return;
85101
}
86102

@@ -94,16 +110,16 @@ public function preSave(WebformSubmissionInterface $webform_submission) {
94110

95111
$cid = $signingService->get_cid();
96112
if (empty($cid)) {
97-
\Drupal::logger('os2forms_digital_signature')->error('Failed to obtain cid. Is server running?');
113+
$this->logger->error('Failed to obtain cid. Is server running?');
98114
return;
99115
}
100116

101117
// Creating hash.
102118
$salt = \Drupal::service('settings')->get('hash_salt');
103119
$hash = Crypt::hashBase64($webform_submission->uuid() . $webform->id() . $salt);
104120

105-
$attahchmentFid = $attachment['fid'] ?? NULL;
106-
$signatureCallbackUrl = Url::fromRoute('os2forms_digital_signature.sign_callback', ['uuid' => $webform_submission->uuid(), 'hash' => $hash, 'fid' => $attahchmentFid]);
121+
$attachmentFid = $attachment['fid'] ?? NULL;
122+
$signatureCallbackUrl = Url::fromRoute('os2forms_digital_signature.sign_callback', ['uuid' => $webform_submission->uuid(), 'hash' => $hash, 'fid' => $attachmentFid]);
107123

108124
// Starting signing, if everything is correct - this funcition will start redirect.
109125
$signingService->sign($fileToSignPublicUrl, $cid, $signatureCallbackUrl->setAbsolute()->toString());

modules/os2forms_digital_signature/src/Service/SigningService.php

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
use Drupal\Core\Config\ConfigFactoryInterface;
66
use Drupal\Core\Config\ImmutableConfig;
7+
use Drupal\Core\Entity\EntityStorageInterface;
8+
use Drupal\Core\Entity\EntityTypeManager;
9+
use Drupal\Core\Logger\LoggerChannelInterface;
710
use Drupal\os2forms_digital_signature\Form\SettingsForm;
8-
use Drupal\os2forms_digital_signature\Plugin\WebformHandler\DigitalSignatureWebformHandler;
11+
use GuzzleHttp\ClientInterface;
912
use Symfony\Component\HttpFoundation\RedirectResponse;
10-
use Drupal\webform\Entity\Webform;
1113
use Drupal\webform\Entity\WebformSubmission;
1214

1315
class SigningService {
@@ -19,8 +21,28 @@ class SigningService {
1921
*/
2022
private readonly ImmutableConfig $config;
2123

22-
public function __construct(ConfigFactoryInterface $configFactory) {
24+
/**
25+
* Webform storage.
26+
*
27+
* @var \Drupal\Core\Entity\EntityStorageInterface
28+
*/
29+
protected EntityStorageInterface $webformStorage;
30+
31+
/**
32+
* WebformSubmission storage.
33+
*
34+
* @var \Drupal\Core\Entity\EntityStorageInterface
35+
*/
36+
protected EntityStorageInterface $webformSubmissionStorage;
37+
38+
public function __construct(
39+
private readonly ClientInterface $httpClient,
40+
ConfigFactoryInterface $configFactory,
41+
EntityTypeManager $entityTypeManager,
42+
private readonly LoggerChannelInterface $logger) {
2343
$this->config = $configFactory->get(SettingsForm::$configName);
44+
$this->webformStorage = $entityTypeManager->getStorage('webform');
45+
$this->webformSubmissionStorage = $entityTypeManager->getStorage('webform_submission');
2446
}
2547

2648
/**
@@ -31,9 +53,8 @@ public function __construct(ConfigFactoryInterface $configFactory) {
3153
*/
3254
public function get_cid() : ?string {
3355
$url = $this->config->get('os2forms_digital_signature_remove_service_url') . 'action=getcid';
34-
$curl = curl_init($url);
35-
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
36-
$result = curl_exec($curl);
56+
$response = $this->httpClient->request('GET', $url);
57+
$result = $response->getBody()->getContents();
3758

3859
$reply = json_decode($result, JSON_OBJECT_AS_ARRAY);
3960

@@ -60,7 +81,7 @@ public function get_cid() : ?string {
6081
*/
6182
public function sign(string $document_uri, string $cid, string $forward_url):void {
6283
if (empty($document_uri) || empty($cid) || empty($forward_url)) {
63-
\Drupal::logger('os2forms_digital_signature')->error('Cannot initiate signing process, check params: document_uri: %document_uri, cid: %cid, forward_url: %forward_url', ['%document_uri' => $document_uri, '%cid' => $cid, '%forward_url' => $forward_url]);
84+
$this->logger->error('Cannot initiate signing process, check params: document_uri: %document_uri, cid: %cid, forward_url: %forward_url', ['%document_uri' => $document_uri, '%cid' => $cid, '%forward_url' => $forward_url]);
6485
return;
6586
}
6687

@@ -98,9 +119,8 @@ public function download(string $filename, $leave = FALSE, $annotate = FALSE, $a
98119
$params = ['action' => 'download', 'file' => $filename, 'leave' => $leave, 'annotate' => $annotate, 'attributes' => $attributes];
99120
$url = $this->config->get('os2forms_digital_signature_remove_service_url') . http_build_query($params);
100121

101-
$curl = curl_init($url);
102-
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
103-
$return = curl_exec($curl);
122+
$response = $this->httpClient->request('GET', $url);
123+
$return = $response->getBody()->getContents();
104124

105125
if (empty($return)) {
106126
return FALSE;
@@ -149,7 +169,7 @@ public function deleteStalledSubmissions() : void {
149169

150170
// Find all with os2forms_digital_signature handlers enabled.
151171
foreach ($handler_webform_ids as $webform_id) {
152-
$webform = Webform::load($webform_id);
172+
$webform = $this->webformStorage->load($webform_id);
153173
if (!$webform) {
154174
continue;
155175
}
@@ -172,7 +192,7 @@ public function deleteStalledSubmissions() : void {
172192
// Find all stalled webform submissions of digital signature forms.
173193
$retention_period = ($this->config->get('os2forms_digital_signature_submission_retention_period')) ?? 300;
174194
$timestamp_threshold = \Drupal::time()->getRequestTime() - $retention_period;
175-
$query = \Drupal::entityQuery('webform_submission')
195+
$query = $this->webformSubmissionStorage->getQuery()
176196
->accessCheck(FALSE)
177197
->condition('webform_id', $digitalSignatureWebforms, 'IN')
178198
->condition('locked', 0)
@@ -186,7 +206,7 @@ public function deleteStalledSubmissions() : void {
186206

187207
// Deleting all stalled webform submissions.
188208
foreach ($submission_ids as $submission_id) {
189-
$submission = WebformSubmission::load($submission_id);
209+
$submission = $this->webformSubmissionStorage->load($submission_id);
190210
$submission->delete();
191211
}
192212
}

0 commit comments

Comments
 (0)