You are here

private function ContentEntityCdfNormalizer::getFilePath in Acquia Content Hub 8

Extracts the filepath and creates directories so that files can be stored.

Parameters

\Acquia\ContentHubClient\Entity $contenthub_entity: The Content Hub Entity.

Return value

null|string The File URI if the directories can be created, NULL otherwise.

1 call to ContentEntityCdfNormalizer::getFilePath()
ContentEntityCdfNormalizer::denormalize in src/Normalizer/ContentEntityCdfNormalizer.php
Denormalizes data back into an object of the given class.

File

src/Normalizer/ContentEntityCdfNormalizer.php, line 1503

Class

ContentEntityCdfNormalizer
Converts the Drupal entity object to a Acquia Content Hub CDF array.

Namespace

Drupal\acquia_contenthub\Normalizer

Code

private function getFilePath(ContentHubEntity $contenthub_entity) {
  if ($contenthub_entity
    ->getType() !== 'file') {
    return NULL;
  }
  if (!($attribute_filepath = $contenthub_entity
    ->getAttribute('_filepath'))) {
    return NULL;
  }
  $uri = isset($attribute_filepath['value'][LanguageInterface::LANGCODE_NOT_SPECIFIED]) ? $attribute_filepath['value'][LanguageInterface::LANGCODE_NOT_SPECIFIED] : NULL;
  if (substr($uri, 0, 9) !== 'public://') {
    return NULL;
  }
  $file_uri = $uri;

  // Create directories.
  $path = pathinfo($file_uri);
  $filepath = $path['dirname'];
  if (!is_dir($filepath) || !is_writable($filepath)) {
    if (!\Drupal::service('file_system')
      ->prepareDirectory($filepath, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS)) {

      // Log that directory could not be created.
      $this->loggerFactory
        ->get('acquia_contenthub')
        ->error('Cannot create files subdirectory "@dir". Please check filesystem permissions.', [
        '@dir' => $filepath,
      ]);
      $file_uri = NULL;
    }
  }
  return $file_uri;
}