From 34e4851241b7de784e6000fc6fef1a4a615ec1db Mon Sep 17 00:00:00 2001 From: Kaise Lafrai Date: Mon, 23 Dec 2024 21:11:40 -0500 Subject: [PATCH] Refactored code to satisfy code climate --- .../PostImportResourceProcessor.php | 12 +- modules/datastore/src/Service/PostImport.php | 123 +++++++++--------- 2 files changed, 69 insertions(+), 66 deletions(-) diff --git a/modules/datastore/src/Plugin/QueueWorker/PostImportResourceProcessor.php b/modules/datastore/src/Plugin/QueueWorker/PostImportResourceProcessor.php index ad34a83cab..a2e33ff575 100644 --- a/modules/datastore/src/Plugin/QueueWorker/PostImportResourceProcessor.php +++ b/modules/datastore/src/Plugin/QueueWorker/PostImportResourceProcessor.php @@ -38,14 +38,14 @@ class PostImportResourceProcessor extends QueueWorkerBase implements ContainerFa /** * Constructor for PostImportResourceProcessor. - * + * * @param array $configuration * A configuration array containing information about the plugin instance. - * @param string $pluginId + * @param string $plugin_id * The plugin_id for the plugin instance. - * @param mixed $pluginDefinition + * @param mixed $plugin_definition * The plugin implementation definition. - * @param \Drupal\datastore\Service\PostImport $postImport + * @param \Drupal\datastore\Service\PostImport $post_import * The PostImport service. * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * The config.factory service. @@ -54,11 +54,11 @@ public function __construct( array $configuration, $plugin_id, $plugin_definition, - PostImport $postImport, + PostImport $post_import, ConfigFactoryInterface $config_factory, ) { parent::__construct($configuration, $plugin_id, $plugin_definition); - $this->postImport = $postImport; + $this->postImport = $post_import; $this->configFactory = $config_factory; } diff --git a/modules/datastore/src/Service/PostImport.php b/modules/datastore/src/Service/PostImport.php index 862e38dd76..3247974ad7 100644 --- a/modules/datastore/src/Service/PostImport.php +++ b/modules/datastore/src/Service/PostImport.php @@ -145,85 +145,88 @@ public function processResource(DataResource $resource): PostImportResult { $this->logger->notice('Post import job for resource @id completed.', ['@id' => $resource->getIdentifier()]); $this->invalidateCacheTags($resource->getIdentifier()); return $this->createPostImportResult('done', NULL, $resource); - } catch (ResourceDoesNotHaveDictionary $e) { + } + catch (ResourceDoesNotHaveDictionary $e) { $this->logger->notice($e->getMessage()); return $this->createPostImportResult('done', 'Resource does not have a data dictionary.', $resource); - } catch (\Exception $e) { + } + catch (\Exception $e) { $this->handleProcessingError($resource, $e); return $this->createPostImportResult('error', $e->getMessage(), $resource); } } -/** - * Handle errors during resource processing. - * - * @param \Drupal\common\DataResource $resource - * DKAN Resource. - * @param \Exception $exception - * The caught exception. - */ -private function handleProcessingError(DataResource $resource, \Exception $exception): void { - $identifier = $resource->getIdentifier(); - - if ($this->configFactory->get('datastore.settings')->get('drop_datastore_on_post_import_error')) { - try { - $this->drop($identifier, NULL, FALSE); - $this->logger->notice('Successfully dropped the datastore for resource @identifier due to a post import error. Visit the Datastore Import Status dashboard for details.', [ - '@identifier' => $identifier, - ]); - } catch (\Exception $dropException) { - $this->logger->error($dropException->getMessage()); + /** + * Handle errors during resource processing. + * + * @param \Drupal\common\DataResource $resource + * DKAN Resource. + * @param \Exception $exception + * The caught exception. + */ + private function handleProcessingError(DataResource $resource, \Exception $exception): void { + $identifier = $resource->getIdentifier(); + + if ($this->configFactory->get('datastore.settings')->get('drop_datastore_on_post_import_error')) { + try { + $this->drop($identifier, NULL, FALSE); + $this->logger->notice('Successfully dropped the datastore for resource @identifier due to a post import error. Visit the Datastore Import Status dashboard for details.', [ + '@identifier' => $identifier, + ]); + } + catch (\Exception $dropException) { + $this->logger->error($dropException->getMessage()); + } } + + $this->logger->error($exception->getMessage()); } - $this->logger->error($exception->getMessage()); -} + /** + * Process resource. + * + * @param \Drupal\common\DataResource $resource + * DKAN Resource. + * + * @throws \Exception + */ + private function processResourceProcessors(DataResource $resource): void { + $processors = $this->resourceProcessorCollector->getResourceProcessors(); + array_map(fn($processor) => $processor->process($resource), $processors); + } -/** - * Process resource. - * - * @param \Drupal\common\DataResource $resource - * DKAN Resource. - * - * @throws \Exception - */ -private function processResourceProcessors(DataResource $resource): void { - $processors = $this->resourceProcessorCollector->getResourceProcessors(); - array_map(fn($processor) => $processor->process($resource), $processors); -} + /** + * Validation checks before processing resource. + * + * @param \Drupal\common\DataResource $resource + * DKAN Resource. + * + * @return \Drupal\datastore\PostImportResult|null + * Post import result if validation fails, or NULL if validation passes. + */ + private function validateResource(DataResource $resource): ?PostImportResult { + $latestResource = $this->resourceMapper->get($resource->getIdentifier()); -/** - * Validation checks before processing resource. - * - * @param \Drupal\common\DataResource $resource - * DKAN Resource. - * - * @return \Drupal\datastore\PostImportResult|null - * Post import result if validation fails, or NULL if validation passes. - */ -private function validateResource(DataResource $resource): ?PostImportResult { - $latestResource = $this->resourceMapper->get($resource->getIdentifier()); + if (!$latestResource) { + $this->logger->notice('Cancelling resource processing; resource no longer exists.'); + return $this->createPostImportResult('error', 'Cancelling resource processing; resource no longer exists.', $resource); + } - if (!$latestResource) { - $this->logger->notice('Cancelling resource processing; resource no longer exists.'); - return $this->createPostImportResult('error', 'Cancelling resource processing; resource no longer exists.', $resource); - } + if ($resource->getVersion() !== $latestResource->getVersion()) { + $this->logger->notice('Cancelling resource processing; resource has changed.'); + return $this->createPostImportResult('error', 'Cancelling resource processing; resource has changed.', $resource); + } - if ($resource->getVersion() !== $latestResource->getVersion()) { - $this->logger->notice('Cancelling resource processing; resource has changed.'); - return $this->createPostImportResult('error', 'Cancelling resource processing; resource has changed.', $resource); - } + if ($this->dataDictionaryDiscovery->getDataDictionaryMode() === DataDictionaryDiscoveryInterface::MODE_NONE) { + $this->logger->notice('Data-Dictionary Disabled'); + return $this->createPostImportResult('N/A', 'Data-Dictionary Disabled', $resource); + } - if ($this->dataDictionaryDiscovery->getDataDictionaryMode() === DataDictionaryDiscoveryInterface::MODE_NONE) { - $this->logger->notice('Data-Dictionary Disabled'); - return $this->createPostImportResult('N/A', 'Data-Dictionary Disabled', $resource); + return NULL; } - return NULL; -} - /** * Create the PostImportResult object. *