Skip to content
Snippets Groups Projects
Commit 70b4c0e3 authored by Liam Morland's avatar Liam Morland
Browse files

Issue #3471412: Use dependency injection

parent 297038e8
No related branches found
No related tags found
No related merge requests found
...@@ -2,7 +2,10 @@ ...@@ -2,7 +2,10 @@
namespace Drupal\fillpdf\Plugin\FillPdfActionPlugin; namespace Drupal\fillpdf\Plugin\FillPdfActionPlugin;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\fillpdf\OutputHandler;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
/** /**
...@@ -17,6 +20,43 @@ use Symfony\Component\HttpFoundation\RedirectResponse; ...@@ -17,6 +20,43 @@ use Symfony\Component\HttpFoundation\RedirectResponse;
*/ */
class FillPdfRedirectAction extends FillPdfSaveAction { 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. * Executes this plugin.
* *
...@@ -28,7 +68,7 @@ class FillPdfRedirectAction extends FillPdfSaveAction { ...@@ -28,7 +68,7 @@ class FillPdfRedirectAction extends FillPdfSaveAction {
*/ */
public function execute() { public function execute() {
$saved_file = $this->savePdf(); $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); return new RedirectResponse($url);
} }
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Drupal\fillpdf\Plugin\PdfBackend; namespace Drupal\fillpdf\Plugin\PdfBackend;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Utility\Error; use Drupal\Core\Utility\Error;
use Drupal\file\FileInterface; use Drupal\file\FileInterface;
...@@ -43,12 +44,15 @@ class LocalServerPdfBackend extends PdfBackendBase implements ContainerFactoryPl ...@@ -43,12 +44,15 @@ class LocalServerPdfBackend extends PdfBackendBase implements ContainerFactoryPl
* The plugin implementation definition. * The plugin implementation definition.
* @param \GuzzleHttp\Client $httpClient * @param \GuzzleHttp\Client $httpClient
* The Guzzle http client. * The Guzzle http client.
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $loggerFactory
* The logger.factory service.
*/ */
public function __construct( public function __construct(
array $configuration, array $configuration,
$plugin_id, $plugin_id,
array $plugin_definition, array $plugin_definition,
protected Client $httpClient, protected Client $httpClient,
protected LoggerChannelFactoryInterface $loggerFactory,
) { ) {
parent::__construct($configuration, $plugin_id, $plugin_definition); parent::__construct($configuration, $plugin_id, $plugin_definition);
} }
...@@ -61,7 +65,8 @@ class LocalServerPdfBackend extends PdfBackendBase implements ContainerFactoryPl ...@@ -61,7 +65,8 @@ class LocalServerPdfBackend extends PdfBackendBase implements ContainerFactoryPl
$configuration, $configuration,
$plugin_id, $plugin_id,
$plugin_definition, $plugin_definition,
$container->get('http_client') $container->get('http_client'),
$container->get('logger.factory'),
); );
} }
...@@ -93,13 +98,13 @@ class LocalServerPdfBackend extends PdfBackendBase implements ContainerFactoryPl ...@@ -93,13 +98,13 @@ class LocalServerPdfBackend extends PdfBackendBase implements ContainerFactoryPl
} }
catch (RequestException $request_exception) { catch (RequestException $request_exception) {
if ($response = $request_exception->getResponse()) { 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(), '%code' => $response->getStatusCode(),
'%reason' => $response->getReasonPhrase(), '%reason' => $response->getReasonPhrase(),
])); ]));
} }
else { 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 ...@@ -170,7 +175,7 @@ class LocalServerPdfBackend extends PdfBackendBase implements ContainerFactoryPl
return base64_decode($decoded['pdf']); return base64_decode($decoded['pdf']);
} }
catch (RequestException $e) { catch (RequestException $e) {
Error::logException(\Drupal::logger('fillpdf'), $e); Error::logException($this->loggerFactory->get('fillpdf'), $e);
return NULL; return NULL;
} }
} }
......
...@@ -7,6 +7,7 @@ use Drupal\Core\File\FileExists; ...@@ -7,6 +7,7 @@ use Drupal\Core\File\FileExists;
use Drupal\Core\File\FileSystemInterface; use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\file\FileInterface; use Drupal\file\FileInterface;
use Drupal\file\FileRepositoryInterface;
use Drupal\fillpdf\Component\Utility\FillPdf; use Drupal\fillpdf\Component\Utility\FillPdf;
use Drupal\fillpdf\Component\Utility\Xfdf; use Drupal\fillpdf\Component\Utility\Xfdf;
use Drupal\fillpdf\FieldMapping\TextFieldMapping; use Drupal\fillpdf\FieldMapping\TextFieldMapping;
...@@ -43,6 +44,8 @@ class PdftkPdfBackend extends PdfBackendBase implements ContainerFactoryPluginIn ...@@ -43,6 +44,8 @@ class PdftkPdfBackend extends PdfBackendBase implements ContainerFactoryPluginIn
* The plugin implementation definition. * The plugin implementation definition.
* @param \Drupal\Core\File\FileSystemInterface $fileSystem * @param \Drupal\Core\File\FileSystemInterface $fileSystem
* The file system. * The file system.
* @param \Drupal\file\FileRepositoryInterface $fileRepository
* The file.repository service.
* @param \Drupal\fillpdf\ShellManager $shellManager * @param \Drupal\fillpdf\ShellManager $shellManager
* The FillPDF shell manager. * The FillPDF shell manager.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
...@@ -53,6 +56,7 @@ class PdftkPdfBackend extends PdfBackendBase implements ContainerFactoryPluginIn ...@@ -53,6 +56,7 @@ class PdftkPdfBackend extends PdfBackendBase implements ContainerFactoryPluginIn
$plugin_id, $plugin_id,
array $plugin_definition, array $plugin_definition,
protected FileSystemInterface $fileSystem, protected FileSystemInterface $fileSystem,
protected FileRepositoryInterface $fileRepository,
protected ShellManager $shellManager, protected ShellManager $shellManager,
protected EntityTypeManagerInterface $entityTypeManager, protected EntityTypeManagerInterface $entityTypeManager,
) { ) {
...@@ -68,8 +72,9 @@ class PdftkPdfBackend extends PdfBackendBase implements ContainerFactoryPluginIn ...@@ -68,8 +72,9 @@ class PdftkPdfBackend extends PdfBackendBase implements ContainerFactoryPluginIn
$plugin_id, $plugin_id,
$plugin_definition, $plugin_definition,
$container->get('file_system'), $container->get('file_system'),
$container->get('file.repository'),
$container->get('fillpdf.shell_manager'), $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 ...@@ -77,7 +82,7 @@ class PdftkPdfBackend extends PdfBackendBase implements ContainerFactoryPluginIn
* {@inheritdoc} * {@inheritdoc}
*/ */
public function parseStream($pdf_content) { 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); return $this->parseFile($template_file);
} }
...@@ -150,7 +155,7 @@ class PdftkPdfBackend extends PdfBackendBase implements ContainerFactoryPluginIn ...@@ -150,7 +155,7 @@ class PdftkPdfBackend extends PdfBackendBase implements ContainerFactoryPluginIn
* {@inheritdoc} * {@inheritdoc}
*/ */
public function mergeStream($pdf_content, array $field_mappings, array $context) { 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); return $this->mergeFile($template_file, $field_mappings, $context);
} }
...@@ -169,7 +174,7 @@ class PdftkPdfBackend extends PdfBackendBase implements ContainerFactoryPluginIn ...@@ -169,7 +174,7 @@ class PdftkPdfBackend extends PdfBackendBase implements ContainerFactoryPluginIn
$xfdf_name = $template_uri . '.xfdf'; $xfdf_name = $template_uri . '.xfdf';
$xfdf = Xfdf::createString($fields, basename($xfdf_name)); $xfdf = Xfdf::createString($fields, basename($xfdf_name));
// Generate the file. // 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. // @todo Improve this approach when we turn $context into a value object.
if (!isset($context['fid'])) { if (!isset($context['fid'])) {
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Drupal\fillpdf_test\Plugin\PdfBackend; namespace Drupal\fillpdf_test\Plugin\PdfBackend;
use Drupal\Core\Extension\ModuleExtensionList;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\State\StateInterface; use Drupal\Core\State\StateInterface;
use Drupal\file\FileInterface; use Drupal\file\FileInterface;
...@@ -34,6 +35,8 @@ class TestPdfBackend extends PdfBackendBase implements ContainerFactoryPluginInt ...@@ -34,6 +35,8 @@ class TestPdfBackend extends PdfBackendBase implements ContainerFactoryPluginInt
* The plugin_id for the plugin instance. * The plugin_id for the plugin instance.
* @param array $plugin_definition * @param array $plugin_definition
* The plugin implementation definition. * The plugin implementation definition.
* @param \Drupal\Core\Extension\ModuleExtensionList $extensionListModule
* The extension.list.module service.
* @param \Drupal\Core\State\StateInterface $state * @param \Drupal\Core\State\StateInterface $state
* The state. * The state.
*/ */
...@@ -41,6 +44,7 @@ class TestPdfBackend extends PdfBackendBase implements ContainerFactoryPluginInt ...@@ -41,6 +44,7 @@ class TestPdfBackend extends PdfBackendBase implements ContainerFactoryPluginInt
array $configuration, array $configuration,
$plugin_id, $plugin_id,
array $plugin_definition, array $plugin_definition,
protected ModuleExtensionList $extensionListModule,
protected StateInterface $state, protected StateInterface $state,
) { ) {
parent::__construct($configuration, $plugin_id, $plugin_definition); parent::__construct($configuration, $plugin_id, $plugin_definition);
...@@ -54,7 +58,8 @@ class TestPdfBackend extends PdfBackendBase implements ContainerFactoryPluginInt ...@@ -54,7 +58,8 @@ class TestPdfBackend extends PdfBackendBase implements ContainerFactoryPluginInt
$configuration, $configuration,
$plugin_id, $plugin_id,
$plugin_definition, $plugin_definition,
$container->get('state') $container->get('extension.list.module'),
$container->get('state'),
); );
} }
...@@ -84,7 +89,7 @@ class TestPdfBackend extends PdfBackendBase implements ContainerFactoryPluginInt ...@@ -84,7 +89,7 @@ class TestPdfBackend extends PdfBackendBase implements ContainerFactoryPluginInt
*/ */
public function mergeStream($pdf_content, array $field_mappings, array $context) { public function mergeStream($pdf_content, array $field_mappings, array $context) {
// Not really populated, but that isn't our job. // 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', [ $this->state->set('fillpdf_test.last_populated_metadata', [
'field_mapping' => $field_mappings, 'field_mapping' => $field_mappings,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment