You are here

public function BulkDocsNormalizer::denormalize in Replication 8

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

File

src/Normalizer/BulkDocsNormalizer.php, line 29

Class

BulkDocsNormalizer

Namespace

Drupal\replication\Normalizer

Code

public function denormalize($data, $class, $format = NULL, array $context = []) {
  if (!isset($context['workspace'])) {
    throw new LogicException('A \'workspace\' context is required to denormalize revision diff data.');
  }
  try {

    /** @var \Drupal\replication\BulkDocs\BulkDocsInterface $bulk_docs */
    $bulk_docs = \Drupal::service('replication.bulkdocs_factory')
      ->get($context['workspace']);
    if (isset($data['new_edits']) && $data['new_edits'] === FALSE || isset($context['query']['new_edits']) && $context['query']['new_edits'] === FALSE) {
      $bulk_docs
        ->newEdits(FALSE);
    }
    $entities = [];
    if (isset($data['docs'])) {
      foreach ($data['docs'] as $doc) {
        if (!empty($doc)) {
          if (is_string($doc)) {
            $doc = Json::decode($doc);
          }

          // @todo {@link https://www.drupal.org/node/2599934 Find a more generic way to denormalize.}
          if (!empty($doc['_id']) && strpos($doc['_id'], 'local') !== FALSE) {

            // Denormalize replication_log entities. This is used when the
            // replication_log entity format is not standard, for example when
            // replicating content from PouchDB.
            list($prefix, $entity_uuid) = explode('/', $doc['_id']);
            if ($prefix == '_local' && $entity_uuid) {
              $entity = $this->serializer
                ->denormalize($doc, 'Drupal\\replication\\Entity\\ReplicationLog', $format, $context);
            }
          }
          elseif (isset($doc['@context'])) {
            $entity = $this->serializer
              ->denormalize($doc, 'Drupal\\Core\\Entity\\ContentEntityInterface', $format, $context);
          }

          // Ensure a deleted doc really is marked as deleted. This may be
          // necessary when an entity is to be deleted only on certain
          // replication targets; e.g., due to filtered replication.
          if (!empty($doc['deleted']) && !empty($entity)) {
            $entity->_deleted->value = TRUE;
          }
          if (!empty($entity)) {
            $entities[] = $entity;
          }
        }
      }
    }
    $bulk_docs
      ->setEntities($entities);
  } catch (\Exception $e) {
    watchdog_exception('Replication', $e);
  }
  return $bulk_docs;
}