From 70b4c0e3abb30eb971422933d9e2cf21ad96352e Mon Sep 17 00:00:00 2001 From: Liam Morland <liam@openplus.ca> Date: Sat, 31 Aug 2024 12:22:52 -0400 Subject: [PATCH] Issue #3471412: Use dependency injection --- .../FillPdfRedirectAction.php | 42 ++++++++++++++++++- .../PdfBackend/LocalServerPdfBackend.php | 13 ++++-- src/Plugin/PdfBackend/PdftkPdfBackend.php | 13 ++++-- .../src/Plugin/PdfBackend/TestPdfBackend.php | 9 +++- 4 files changed, 66 insertions(+), 11 deletions(-) diff --git a/src/Plugin/FillPdfActionPlugin/FillPdfRedirectAction.php b/src/Plugin/FillPdfActionPlugin/FillPdfRedirectAction.php index c47f56a..8dabd33 100644 --- a/src/Plugin/FillPdfActionPlugin/FillPdfRedirectAction.php +++ b/src/Plugin/FillPdfActionPlugin/FillPdfRedirectAction.php @@ -2,7 +2,10 @@ namespace Drupal\fillpdf\Plugin\FillPdfActionPlugin; +use Drupal\Core\File\FileUrlGeneratorInterface; use Drupal\Core\Url; +use Drupal\fillpdf\OutputHandler; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\RedirectResponse; /** @@ -17,6 +20,43 @@ use Symfony\Component\HttpFoundation\RedirectResponse; */ class FillPdfRedirectAction extends FillPdfSaveAction { + /** + * Constructs a \Drupal\Component\Plugin\PluginBase object. + * + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin_id for the plugin instance. + * @param mixed $plugin_definition + * The plugin implementation definition. + * @param \Drupal\Core\File\FileUrlGeneratorInterface $fileUrlGenerator + * The file_url_generator service. + * @param \Drupal\fillpdf\OutputHandler $outputHandler + * The fillpdf.output_handler service. + */ + public function __construct( + array $configuration, + $plugin_id, + $plugin_definition, + protected FileUrlGeneratorInterface $fileUrlGenerator, + OutputHandler $outputHandler, + ) { + parent::__construct($configuration, $plugin_id, $plugin_definition, $outputHandler); + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('file_url_generator'), + $container->get('fillpdf.output_handler'), + ); + } + /** * Executes this plugin. * @@ -28,7 +68,7 @@ class FillPdfRedirectAction extends FillPdfSaveAction { */ public function execute() { $saved_file = $this->savePdf(); - $url = ($saved_file !== FALSE) ? \Drupal::service('file_url_generator')->generateAbsoluteString($saved_file->getFileUri()) : Url::fromRoute('<front>')->toString(); + $url = ($saved_file !== FALSE) ? $this->fileUrlGenerator->generateAbsoluteString($saved_file->getFileUri()) : Url::fromRoute('<front>')->toString(); return new RedirectResponse($url); } diff --git a/src/Plugin/PdfBackend/LocalServerPdfBackend.php b/src/Plugin/PdfBackend/LocalServerPdfBackend.php index f166dd7..67556ff 100644 --- a/src/Plugin/PdfBackend/LocalServerPdfBackend.php +++ b/src/Plugin/PdfBackend/LocalServerPdfBackend.php @@ -2,6 +2,7 @@ namespace Drupal\fillpdf\Plugin\PdfBackend; +use Drupal\Core\Logger\LoggerChannelFactoryInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Utility\Error; use Drupal\file\FileInterface; @@ -43,12 +44,15 @@ class LocalServerPdfBackend extends PdfBackendBase implements ContainerFactoryPl * The plugin implementation definition. * @param \GuzzleHttp\Client $httpClient * The Guzzle http client. + * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $loggerFactory + * The logger.factory service. */ public function __construct( array $configuration, $plugin_id, array $plugin_definition, protected Client $httpClient, + protected LoggerChannelFactoryInterface $loggerFactory, ) { parent::__construct($configuration, $plugin_id, $plugin_definition); } @@ -61,7 +65,8 @@ class LocalServerPdfBackend extends PdfBackendBase implements ContainerFactoryPl $configuration, $plugin_id, $plugin_definition, - $container->get('http_client') + $container->get('http_client'), + $container->get('logger.factory'), ); } @@ -93,13 +98,13 @@ class LocalServerPdfBackend extends PdfBackendBase implements ContainerFactoryPl } catch (RequestException $request_exception) { if ($response = $request_exception->getResponse()) { - \Drupal::messenger()->addError($this->t('Error %code. Reason: %reason.', [ + $this->messenger()->addError($this->t('Error %code. Reason: %reason.', [ '%code' => $response->getStatusCode(), '%reason' => $response->getReasonPhrase(), ])); } else { - \Drupal::messenger()->addError($this->t('Unknown error occurred parsing PDF.')); + $this->messenger()->addError($this->t('Unknown error occurred parsing PDF.')); } } @@ -170,7 +175,7 @@ class LocalServerPdfBackend extends PdfBackendBase implements ContainerFactoryPl return base64_decode($decoded['pdf']); } catch (RequestException $e) { - Error::logException(\Drupal::logger('fillpdf'), $e); + Error::logException($this->loggerFactory->get('fillpdf'), $e); return NULL; } } diff --git a/src/Plugin/PdfBackend/PdftkPdfBackend.php b/src/Plugin/PdfBackend/PdftkPdfBackend.php index b28f251..78cb63b 100644 --- a/src/Plugin/PdfBackend/PdftkPdfBackend.php +++ b/src/Plugin/PdfBackend/PdftkPdfBackend.php @@ -7,6 +7,7 @@ use Drupal\Core\File\FileExists; use Drupal\Core\File\FileSystemInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\file\FileInterface; +use Drupal\file\FileRepositoryInterface; use Drupal\fillpdf\Component\Utility\FillPdf; use Drupal\fillpdf\Component\Utility\Xfdf; use Drupal\fillpdf\FieldMapping\TextFieldMapping; @@ -43,6 +44,8 @@ class PdftkPdfBackend extends PdfBackendBase implements ContainerFactoryPluginIn * The plugin implementation definition. * @param \Drupal\Core\File\FileSystemInterface $fileSystem * The file system. + * @param \Drupal\file\FileRepositoryInterface $fileRepository + * The file.repository service. * @param \Drupal\fillpdf\ShellManager $shellManager * The FillPDF shell manager. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager @@ -53,6 +56,7 @@ class PdftkPdfBackend extends PdfBackendBase implements ContainerFactoryPluginIn $plugin_id, array $plugin_definition, protected FileSystemInterface $fileSystem, + protected FileRepositoryInterface $fileRepository, protected ShellManager $shellManager, protected EntityTypeManagerInterface $entityTypeManager, ) { @@ -68,8 +72,9 @@ class PdftkPdfBackend extends PdfBackendBase implements ContainerFactoryPluginIn $plugin_id, $plugin_definition, $container->get('file_system'), + $container->get('file.repository'), $container->get('fillpdf.shell_manager'), - $container->get('entity_type.manager') + $container->get('entity_type.manager'), ); } @@ -77,7 +82,7 @@ class PdftkPdfBackend extends PdfBackendBase implements ContainerFactoryPluginIn * {@inheritdoc} */ public function parseStream($pdf_content) { - $template_file = \Drupal::service('file.repository')->writeData($pdf_content); + $template_file = $this->fileRepository->writeData($pdf_content); return $this->parseFile($template_file); } @@ -150,7 +155,7 @@ class PdftkPdfBackend extends PdfBackendBase implements ContainerFactoryPluginIn * {@inheritdoc} */ public function mergeStream($pdf_content, array $field_mappings, array $context) { - $template_file = \Drupal::service('file.repository')->writeData($pdf_content); + $template_file = $this->fileRepository->writeData($pdf_content); return $this->mergeFile($template_file, $field_mappings, $context); } @@ -169,7 +174,7 @@ class PdftkPdfBackend extends PdfBackendBase implements ContainerFactoryPluginIn $xfdf_name = $template_uri . '.xfdf'; $xfdf = Xfdf::createString($fields, basename($xfdf_name)); // Generate the file. - $xfdf_file = \Drupal::service('file.repository')->writeData($xfdf, $xfdf_name, FileExists::Rename); + $xfdf_file = $this->fileRepository->writeData($xfdf, $xfdf_name, FileExists::Rename); // @todo Improve this approach when we turn $context into a value object. if (!isset($context['fid'])) { diff --git a/tests/modules/fillpdf_test/src/Plugin/PdfBackend/TestPdfBackend.php b/tests/modules/fillpdf_test/src/Plugin/PdfBackend/TestPdfBackend.php index 4080623..e098fcd 100644 --- a/tests/modules/fillpdf_test/src/Plugin/PdfBackend/TestPdfBackend.php +++ b/tests/modules/fillpdf_test/src/Plugin/PdfBackend/TestPdfBackend.php @@ -2,6 +2,7 @@ namespace Drupal\fillpdf_test\Plugin\PdfBackend; +use Drupal\Core\Extension\ModuleExtensionList; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\State\StateInterface; use Drupal\file\FileInterface; @@ -34,6 +35,8 @@ class TestPdfBackend extends PdfBackendBase implements ContainerFactoryPluginInt * The plugin_id for the plugin instance. * @param array $plugin_definition * The plugin implementation definition. + * @param \Drupal\Core\Extension\ModuleExtensionList $extensionListModule + * The extension.list.module service. * @param \Drupal\Core\State\StateInterface $state * The state. */ @@ -41,6 +44,7 @@ class TestPdfBackend extends PdfBackendBase implements ContainerFactoryPluginInt array $configuration, $plugin_id, array $plugin_definition, + protected ModuleExtensionList $extensionListModule, protected StateInterface $state, ) { parent::__construct($configuration, $plugin_id, $plugin_definition); @@ -54,7 +58,8 @@ class TestPdfBackend extends PdfBackendBase implements ContainerFactoryPluginInt $configuration, $plugin_id, $plugin_definition, - $container->get('state') + $container->get('extension.list.module'), + $container->get('state'), ); } @@ -84,7 +89,7 @@ class TestPdfBackend extends PdfBackendBase implements ContainerFactoryPluginInt */ public function mergeStream($pdf_content, array $field_mappings, array $context) { // Not really populated, but that isn't our job. - $populated_pdf = file_get_contents(\Drupal::service('extension.list.module')->getPath('fillpdf_test') . '/files/fillpdf_test_v3.pdf'); + $populated_pdf = file_get_contents($this->extensionListModule->getPath('fillpdf_test') . '/files/fillpdf_test_v3.pdf'); $this->state->set('fillpdf_test.last_populated_metadata', [ 'field_mapping' => $field_mappings, -- GitLab