You are here

public static function MigratePluginAlterer::getSourceValueOfMigrationProcess in Media Migration 8

Gets the value of a process property if it is not dynamically calculated.

Parameters

array $migration: The migration plugin's definition array.

string $process_property_key: The property to check.

Return value

mixed|null The value of the property if it can be determined, or NULL if it seems to be dynamic.

Throws

\LogicException. When the process property does not exists.

2 calls to MigratePluginAlterer::getSourceValueOfMigrationProcess()
MigratePluginAlterer::mapMigrationProcessValueToMedia in src/MigratePluginAlterer.php
Maps a migration's property from "file" to "media".
MigratePluginAltererTest::testGetSourceValueOfMigrationProcess in tests/src/Unit/MigratePluginAltererTest.php
Tests getSourceValueOfMigrationProcess().

File

src/MigratePluginAlterer.php, line 295

Class

MigratePluginAlterer
Service for performing migration plugin alterations.

Namespace

Drupal\media_migration

Code

public static function getSourceValueOfMigrationProcess(array $migration, string $process_property_key) {
  if (!array_key_exists('process', $migration) || !is_array($migration['process']) || !array_key_exists($process_property_key, $migration['process'])) {
    throw new \LogicException('No corresponding process found');
  }
  $property_processes = MigrationPluginTool::getAssociativeMigrationProcess($migration['process'][$process_property_key]);
  $the_first_process = reset($property_processes);
  $property_value = NULL;
  if (!array_key_exists('source', $migration) || count($property_processes) !== 1 || $the_first_process['plugin'] !== 'get' || empty($the_first_process['source'])) {
    return NULL;
  }
  $process_value_source = $the_first_process['source'];

  // Parsing string values like "whatever" or "constants/whatever/key".
  // If the property is set to an already available value (e.g. a constant),
  // we don't need our special mapping applied.
  $property_value = NestedArray::getValue($migration['source'], explode(Row::PROPERTY_SEPARATOR, $process_value_source), $key_exists);

  // Migrations using the "embedded_data" source plugin actually contain
  // rows with source values.
  if (!$key_exists && $migration['source']['plugin'] === 'embedded_data') {
    $embedded_rows = $migration['source']['data_rows'] ?? [];
    $embedded_property_values = array_reduce($embedded_rows, function (array $carry, array $row) use ($process_value_source) {
      $embedded_value = NestedArray::getValue($row, explode(Row::PROPERTY_SEPARATOR, $process_value_source));
      $carry = array_unique(array_merge($carry, [
        $embedded_value,
      ]));
      return $carry;
    }, []);
    return count($embedded_property_values) === 1 ? $embedded_property_values[0] : NULL;
  }
  return $key_exists ? $property_value : NULL;
}