SU-SWS/stanford_migrate

View on GitHub
src/Plugin/migrate/process/StanfordFileImport.php

Summary

Maintainability
A
0 mins
Test Coverage
A
100%
<?php

namespace Drupal\stanford_migrate\Plugin\migrate\process;

use Drupal\Component\Utility\Bytes;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;
use Drupal\migrate_file\Plugin\migrate\process\FileImport;

/**
 * Override the migrate_file module's plugin to add a max_size configuration.
 */
class StanfordFileImport extends FileImport {

  /**
   * {@inheritDoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    if (!empty($this->configuration['max_size'])) {
      $allowed = FALSE;
      $headers = get_headers($value, TRUE);

      if (isset($headers['Content-Length']) && str_contains($headers[0], '200 OK')) {
        $size = (int) (is_array($headers['Content-Length']) ? end($headers['Content-Length']) : $headers['Content-Length']);

        if ($size && $size <= Bytes::toNumber($this->configuration['max_size'])) {
          $allowed = TRUE;
        }
      }

      $value = $allowed ? $value : NULL;
    }
    return parent::transform($value, $migrate_executable, $row, $destination_property);
  }

}