You are here

public function Download::transform in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/migrate/src/Plugin/migrate/process/Download.php \Drupal\migrate\Plugin\migrate\process\Download::transform()
  2. 9 core/modules/migrate/src/Plugin/migrate/process/Download.php \Drupal\migrate\Plugin\migrate\process\Download::transform()

Performs the associated process.

Parameters

mixed $value: The value to be transformed.

\Drupal\migrate\MigrateExecutableInterface $migrate_executable: The migration in which this process is being executed.

\Drupal\migrate\Row $row: The row from the source to process. Normally, just transforming the value is adequate but very rarely you might need to change two columns at the same time or something like that.

string $destination_property: The destination property currently worked on. This is only used together with the $row above.

Return value

mixed The newly transformed value.

Overrides ProcessPluginBase::transform

File

core/modules/migrate/src/Plugin/migrate/process/Download.php, line 114

Class

Download
Downloads a file from a HTTP(S) remote location into the local file system.

Namespace

Drupal\migrate\Plugin\migrate\process

Code

public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {

  // If we're stubbing a file entity, return a uri of NULL so it will get
  // stubbed by the general process.
  if ($row
    ->isStub()) {
    return NULL;
  }
  [
    $source,
    $destination,
  ] = $value;

  // Modify the destination filename if necessary.
  $final_destination = $this->fileSystem
    ->getDestinationFilename($destination, $this->configuration['file_exists']);

  // Reuse if file exists.
  if (!$final_destination) {
    return $destination;
  }

  // Try opening the file first, to avoid calling prepareDirectory()
  // unnecessarily. We're suppressing fopen() errors because we want to try
  // to prepare the directory before we give up and fail.
  $destination_stream = @fopen($final_destination, 'w');
  if (!$destination_stream) {

    // If fopen didn't work, make sure there's a writable directory in place.
    $dir = $this->fileSystem
      ->dirname($final_destination);
    if (!$this->fileSystem
      ->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {
      throw new MigrateException("Could not create or write to directory '{$dir}'");
    }

    // Let's try that fopen again.
    $destination_stream = @fopen($final_destination, 'w');
    if (!$destination_stream) {
      throw new MigrateException("Could not write to file '{$final_destination}'");
    }
  }

  // Stream the request body directly to the final destination stream.
  $this->configuration['guzzle_options']['sink'] = $destination_stream;
  try {

    // Make the request. Guzzle throws an exception for anything but 200.
    $this->httpClient
      ->get($source, $this->configuration['guzzle_options']);
  } catch (\Exception $e) {
    throw new MigrateException("{$e->getMessage()} ({$source})");
  }
  if (is_resource($destination_stream)) {
    fclose($destination_stream);
  }
  return $final_destination;
}