You are here

protected function ExportDrupalCommand::execute in Search API Synonym 8

File

src/Command/ExportDrupalCommand.php, line 77

Class

ExportDrupalCommand
Drupal Console Command for export synonyms.

Namespace

Drupal\search_api_synonym\Command

Code

protected function execute(InputInterface $input, OutputInterface $output) {

  // Plugin manager
  $pluginManager = \Drupal::service('plugin.manager.search_api_synonym.export');

  // Options
  $plugin = $input
    ->getOption('plugin');
  $langcode = $input
    ->getOption('langcode');
  $type = $input
    ->getOption('type');
  $filter = $input
    ->getOption('filter');
  $file = $input
    ->getOption('file');
  $incremental = $input
    ->getOption('incremental');

  // Command output
  $io = new DrupalStyle($input, $output);

  // Validate option: plugin
  if (!$pluginManager
    ->validatePlugin($plugin)) {
    $error = TRUE;
    $io
      ->info($this
      ->trans('commands.searchapi.synonym.export.messages.invalidplugin'));
  }

  // Validate option: langcode
  if (empty($langcode)) {
    $error = TRUE;
    $io
      ->info($this
      ->trans('commands.searchapi.synonym.export.messages.invalidlangcode'));
  }

  // Validate option: type
  if (!empty($type) && !$this
    ->validateOptionType($type)) {
    $error = TRUE;
    $io
      ->info($this
      ->trans('commands.searchapi.synonym.export.messages.invalidtype'));
  }

  // Validate option: filter
  if (!empty($filter) && !$this
    ->validateOptionFilter($filter)) {
    $error = TRUE;
    $io
      ->info($this
      ->trans('commands.searchapi.synonym.export.messages.invalidfilter'));
  }

  // Prepare export
  if (!isset($error)) {
    $io
      ->info($this
      ->trans('commands.searchapi.synonym.export.messages.start'));
    $options = [
      'langcode' => $langcode,
      'type' => $type,
      'filter' => $filter,
      'file' => $file,
      'incremental' => (int) $incremental,
    ];
    $pluginManager
      ->setPluginId($plugin);
    $pluginManager
      ->setExportOptions($options);

    // Execute export
    if ($result = $pluginManager
      ->executeExport()) {

      // Output result
      $io
        ->info($this
        ->trans('commands.searchapi.synonym.export.messages.success'));
      $io
        ->info($result);
    }
  }
}