Skip to content

Commit 23fa488

Browse files
committed
OS-110 refactoring, service injections
1 parent e01f0c1 commit 23fa488

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

modules/os2forms_digital_signature/src/Controller/DigitalSignatureController.php

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,44 @@
33
namespace Drupal\os2forms_digital_signature\Controller;
44

55
use Drupal\Component\Utility\Crypt;
6+
use Drupal\Core\Controller\ControllerBase;
7+
use Drupal\Core\File\FileExists;
68
use Drupal\Core\File\FileSystemInterface;
9+
use Drupal\Core\Site\Settings;
710
use Drupal\Core\Url;
811
use Drupal\file\Entity\File;
12+
use Drupal\os2forms_digital_signature\Service\SigningService;
13+
use Psr\Log\LoggerInterface;
14+
use Symfony\Component\DependencyInjection\ContainerInterface;
915
use Symfony\Component\HttpFoundation\RedirectResponse;
1016
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1117

1218
/**
1319
* Digital Signature Controller.
1420
*/
15-
class DigitalSignatureController {
21+
class DigitalSignatureController extends ControllerBase {
1622

1723
/**
18-
* Logger for channel - os2forms_digital_signature.
19-
*
20-
* @var \Drupal\Core\Logger\LoggerChannelInterface
24+
* Constructor.
2125
*/
22-
protected $logger;
26+
public function __construct(
27+
private readonly LoggerInterface $logger,
28+
private readonly Settings $settings,
29+
private readonly SigningService $signingService,
30+
private readonly FileSystemInterface $fileSystem,
31+
) {
32+
}
2333

24-
public function __construct() {
25-
$this->logger = \Drupal::logger('os2forms_digital_signature');
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public static function create(ContainerInterface $container) {
38+
return new static(
39+
$container->get('logger.channel.os2forms_digital_signature'),
40+
$container->get('settings'),
41+
$container->get('os2forms_digital_signature.signing_service'),
42+
$container->get('file_system'),
43+
);
2644
}
2745

2846
/**
@@ -70,26 +88,20 @@ public function signCallback($uuid, $hash, $fid = NULL) {
7088
}
7189

7290
// Checking hash.
73-
$salt = \Drupal::service('settings')->get('hash_salt');
91+
$salt = $this->settings->get('hash_salt');
7492
$tmpHash = Crypt::hashBase64($uuid . $webformId . $salt);
7593
if ($hash !== $tmpHash) {
7694
// Submission exist, but the provided hash is incorrect.
7795
throw new NotFoundHttpException();
7896
}
7997

80-
/** @var \Drupal\os2forms_digital_signature\Service\SigningService $signingService */
81-
$signingService = \Drupal::service('os2forms_digital_signature.signing_service');
82-
8398
$signedFilename = \Drupal::request()->get('file');
84-
$signedFileContent = $signingService->download($signedFilename);
99+
$signedFileContent = $this->signingService->download($signedFilename);
85100
if (!$signedFileContent) {
86101
$this->logger->warning('Missing file on remote server %file.', ['%file' => $signedFilename]);
87102
throw new NotFoundHttpException();
88103
}
89104

90-
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
91-
$file_system = \Drupal::service('file_system');
92-
93105
// If $fid is present - we are replacing uploaded/managed file, otherwise
94106
// creating a new one.
95107
if ($fid) {
@@ -101,14 +113,14 @@ public function signCallback($uuid, $hash, $fid = NULL) {
101113
$expectedFileUri = "private://webform/$webformId/digital_signature/$uuid.pdf";
102114
$directory = dirname($expectedFileUri);
103115

104-
if (!$file_system->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY)) {
116+
if (!$this->fileSystem->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY)) {
105117
$this->logger->error('Failed to prepare directory %directory.', ['%directory' => $directory]);
106118
}
107119
}
108120

109121
// Write the data to the file using Drupal's file system service.
110122
try {
111-
$file_system->saveData($signedFileContent, $expectedFileUri, FileSystemInterface::EXISTS_REPLACE);
123+
$this->fileSystem->saveData($signedFileContent, $expectedFileUri, FileExists::Replace);
112124

113125
// Updating webform submission.
114126
$webformSubmission->setLocked(TRUE);

modules/os2forms_digital_signature/src/Service/SigningService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public function deleteStalledSubmissions() : void {
240240
}
241241

242242
/**
243-
* Returns Remove service URL.
243+
* Returns Remote signature service URL.
244244
*
245245
* @return string
246246
* Remote Service URL, if missing '?' or '&', '?' will be added

0 commit comments

Comments
 (0)