Skip to content

Commit

Permalink
feat: kind of working UNHCR importer
Browse files Browse the repository at this point in the history
  • Loading branch information
orakili committed Dec 6, 2024
1 parent 1ffd1cb commit 4a49784
Show file tree
Hide file tree
Showing 11 changed files with 1,270 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,31 @@ reliefweb_import.plugin.importer:
type: mapping
label: 'ReliefWeb importer plugin base settings.'
mapping:
enabled:
type: bool
label: 'Whether the plugin is enabled or not.'

# UNHCR Data importer plugin settings.
reliefweb_import.plugin.importer.unhcr_data:
type: reliefweb_import.plugin.importer
label: 'UNHCR Data importer plugin base settings.'
mapping:
api_url:
type: string
label: 'API URL.'
api_key:
type: string
label: 'API key.'
list_endpoint:
type: string
label: 'Endpoint path to get a list of documents.'
document_endpoint:
type: string
label: 'Endpoint path to get a document.'
timeout:
type: int
label: 'Connection and request timeout.'
provider_uuid:
type: string
label: 'UUID of the Post API provider used associated with this imported plugin.'

2 changes: 1 addition & 1 deletion html/modules/custom/reliefweb_import/drush.services.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
reliefweb_import.commands:
class: \Drupal\reliefweb_import\Drush\Commands\ReliefwebImport
arguments: ['@reliefweb_import.job_feeds_importer']
arguments: ['@config.factory', '@reliefweb_import.job_feeds_importer', '@plugin.manager.reliefweb_import.reliefweb_importer']
tags:
- { name: drush.command }
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ core_version_requirement: ^10
dependencies:
- drupal:reliefweb_api
- drupal:reliefweb_entities
- drupal:reliefweb_post_api
- drupal:reliefweb_utility
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ services:
class: \Drupal\reliefweb_import\Service\JobFeedsImporter
arguments: ['@database', '@entity_type.manager', '@account_switcher', '@http_client', '@logger.factory', '@state']

plugin.manager.reliefweb_import.importer:
class: Drupal\reliefweb_import\Plugin\ImporterPluginManager
plugin.manager.reliefweb_import.reliefweb_importer:
class: Drupal\reliefweb_import\Plugin\ReliefWebImporterPluginManager
parent: default_plugin_manager
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Consolidation\SiteAlias\SiteAliasManagerAwareInterface;
use Consolidation\SiteAlias\SiteAliasManagerAwareTrait;
use Consolidation\SiteProcess\ProcessManagerAwareTrait;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\reliefweb_import\Plugin\ReliefwebImporterPluginManagerInterface;
use Drupal\reliefweb_import\Service\JobFeedsImporterInterface;
use Drush\Commands\DrushCommands;

Expand All @@ -21,23 +23,83 @@ class ReliefwebImport extends DrushCommands implements SiteAliasManagerAwareInte
* {@inheritdoc}
*/
public function __construct(
protected ConfigFactoryInterface $configFactory,
protected JobFeedsImporterInterface $jobImporter,
protected ReliefwebImporterPluginManagerInterface $importerPluginManager,
) {}

/**
* Import jobs.
*
* @param int $limit
* Max number of items to send.
* Max number of items to import.
*
* @command reliefweb_import:jobs
*
* @usage reliefweb_import:jobs
* Send emails.
* Import jobs.
*
* @validate-module-enabled reliefweb_import
* @aliases reliefweb-import-jobs
*/
public function jobs(int $limit = 50): void {
public function importJobs(int $limit = 50): void {
$this->jobImporter->importJobs($limit);
}

/**
* Import content.
*
* @param string $plugin_id
* ID of the importer plugin to use.
* @param int $limit
* Max number of items to import.
*
* @command reliefweb_import:content
*
* @usage reliefweb_import:content test 10
* Import 10 documents from the 'test' importer plugin.
*
* @validate-module-enabled reliefweb_import
* @aliases reliefweb-import-content
*
* @todo allow passing 'all' to import content from all the enabled plugins.
*/
public function import(string $plugin_id, int $limit = 50): bool {
if (!$this->importerPluginManager->hasDefinition($plugin_id)) {
$this->logger()->error(strtr('Unknown importer plugin: @plugin_id.', [
'@plugin_id' => $plugin_id,
]));
return FALSE;
}

$settings = $this->configFactory
->get('reliefweb_import')
->get('plugin.reliefweb_import.importer.' . $plugin_id);

if (empty($settings)) {
$this->logger()->error(strtr('No settings for importer plugin: @plugin_id.', [
'plugin_id' => $plugin_id,
]));
return FALSE;
}

/** @var \Drupal\reliefweb_import\Plugin\ImporterPluginInterface|null $plugin */
$plugin = $this->importerPluginManager->createInstance($plugin_id, $settings);
if (empty($plugin)) {
$this->logger()->error(strtr('Unable to create importer plugin: @plugin_id.', [
'plugin_id' => $plugin_id,
]));
return FALSE;
}

if (!$plugin->enabled()) {
$this->logger()->notice(strtr('Importer plugin: @plugin_id not enabled.', [
'plugin_id' => $plugin_id,
]));
return TRUE;
}

return $plugin->importContent($limit);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Drupal\reliefweb_import\Exception;

/**
* ReliefWeb Import exception interface.
*/
interface ExceptionInterface {}
Loading

0 comments on commit 4a49784

Please sign in to comment.