33namespace Drupal \os2forms_digital_signature \Plugin \WebformHandler ;
44
55use Drupal \Component \Utility \Crypt ;
6+ use Drupal \Core \Extension \ModuleHandlerInterface ;
67use Drupal \Core \File \FileExists ;
78use Drupal \Core \File \FileSystemInterface ;
9+ use Drupal \Core \File \FileUrlGeneratorInterface ;
10+ use Drupal \Core \Site \Settings ;
811use Drupal \Core \Url ;
12+ use Drupal \file \FileRepositoryInterface ;
13+ use Drupal \os2forms_digital_signature \Service \SigningService ;
14+ use Drupal \webform \Plugin \WebformElementManagerInterface ;
915use Drupal \webform \Plugin \WebformHandlerBase ;
1016use Drupal \webform \WebformSubmissionInterface ;
17+ use Psr \Log \LoggerInterface ;
1118use 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 /**
0 commit comments