Skip to content

Commit

Permalink
Refactored code to satisfy code climate
Browse files Browse the repository at this point in the history
  • Loading branch information
kaise-lafrai committed Dec 24, 2024
1 parent 1921854 commit 34e4851
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}

Expand Down
123 changes: 63 additions & 60 deletions modules/datastore/src/Service/PostImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 34e4851

Please sign in to comment.