You are here

public function AttachmentNormalizer::denormalize in Replication 8.2

Same name and namespace in other branches
  1. 8 src/Normalizer/AttachmentNormalizer.php \Drupal\replication\Normalizer\AttachmentNormalizer::denormalize()

Overrides ContentEntityNormalizer::denormalize

File

src/Normalizer/AttachmentNormalizer.php, line 38

Class

AttachmentNormalizer

Namespace

Drupal\replication\Normalizer

Code

public function denormalize($data, $class, $format = NULL, array $context = []) {
  $meta_data = is_resource($data) ? stream_get_meta_data($data) : NULL;

  // @todo {@link https://www.drupal.org/node/2599926 Use $class to instantiate the entity.}
  $file_data = [];
  if (isset($meta_data['uri'])) {
    $file_data['uri'] = $meta_data['uri'];
  }
  elseif (isset($context['uri'])) {
    $file_data['uri'] = $context['uri'];
  }
  $file_info_keys = [
    'uuid',
    'status',
    'uid',
    'workspace',
  ];
  foreach ($file_info_keys as $key) {
    if (isset($context[$key])) {
      $file_data[$key] = $context[$key];
    }
  }
  if (isset($context['uuid'])) {
    $workspace = isset($context['workspace']) ? $context['workspace'] : NULL;

    /** @var UuidIndexInterface $uuid_index */
    $uuid_index = $this->indexFactory
      ->get('multiversion.entity_index.uuid', $workspace);
    $entity_info = $uuid_index
      ->get($context['uuid']);
    if (!empty($entity_info)) {

      /** @var FileInterface $file */
      $file = $this->entityManager
        ->getStorage($entity_info['entity_type_id'])
        ->load($entity_info['entity_id']);
      if (!empty($file)) {
        foreach ($file_data as $key => $data) {
          $file->{$key} = $data;
        }
        return $file;
      }
    }
  }
  return $this->entityManager
    ->getStorage('file')
    ->create($file_data);
}