ImportConfigManipulator.php in Entity Share 8.3
File
modules/entity_share_client/src/Service/ImportConfigManipulator.php
View source
<?php
declare (strict_types=1);
namespace Drupal\entity_share_client\Service;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\entity_share_client\Entity\ImportConfigInterface;
use Drupal\entity_share_client\ImportProcessor\ImportProcessorPluginManager;
class ImportConfigManipulator implements ImportConfigManipulatorInterface {
protected $importProcessorPluginManager;
public function __construct(ImportProcessorPluginManager $import_processor_plugin_manager) {
$this->importProcessorPluginManager = $import_processor_plugin_manager;
}
protected function createImportProcessorPlugins(ImportConfigInterface $import_config, array $plugin_ids = NULL, array $configurations = []) {
if ($plugin_ids === NULL) {
$plugin_ids = array_keys($this->importProcessorPluginManager
->getDefinitions());
}
$plugins = [];
$import_config_settings = $import_config
->get('import_processor_settings');
foreach ($plugin_ids as $plugin_id) {
$configuration = [];
if (isset($configurations[$plugin_id])) {
$configuration = $configurations[$plugin_id];
}
elseif (isset($import_config_settings[$plugin_id])) {
$configuration = $import_config_settings[$plugin_id];
}
try {
$plugins[$plugin_id] = $this->importProcessorPluginManager
->createInstance($plugin_id, $configuration);
} catch (PluginException $exception) {
throw new \Exception("Unknown import processor plugin with ID '{$plugin_id}'");
}
}
return $plugins;
}
public function getImportProcessors(ImportConfigInterface $import_config) {
$returned_processors = [];
$processors = $this
->createImportProcessorPlugins($import_config);
foreach ($processors as $processor_id => $processor) {
if (isset($import_config
->get('import_processor_settings')[$processor_id]) || $processor
->isLocked()) {
$returned_processors[$processor_id] = $processor;
}
}
return $returned_processors;
}
public function getImportProcessor(ImportConfigInterface $import_config, $processor_id) {
$processors = $this
->getImportProcessors($import_config);
if (empty($processors[$processor_id])) {
$import_config_label = $import_config
->label();
throw new \Exception("The import processor with ID '{$processor_id}' could not be retrieved for import config '{$import_config_label}'.");
}
return $processors[$processor_id];
}
public function getImportProcessorsByStages(ImportConfigInterface $import_config, array $overrides = []) {
$return_processors = [];
$stages = $this->importProcessorPluginManager
->getProcessingStages();
foreach (array_keys($stages) as $stage) {
$return_processors[$stage] = $this
->getImportProcessorsByStage($import_config, $stage, $overrides);
}
return $return_processors;
}
public function getImportProcessorsByStage(ImportConfigInterface $import_config, $stage, array $overrides = []) {
$processors = $this
->getImportProcessors($import_config);
$processor_weights = [];
foreach ($processors as $name => $processor) {
if ($processor
->supportsStage($stage)) {
$processor_weights[$name] = $processor
->getWeight($stage);
}
}
foreach ($overrides as $name => $config) {
$processor = $this->importProcessorPluginManager
->createInstance($name, $config);
if ($processor
->supportsStage($stage)) {
$processors[$name] = $processor;
$processor_weights[$name] = $processor
->getWeight($stage);
}
else {
unset($processor_weights[$name]);
}
}
asort($processor_weights);
$return_processors = [];
foreach (array_keys($processor_weights) as $name) {
$return_processors[$name] = $processors[$name];
}
return $return_processors;
}
}