You are here

class BulkDocsNormalizer in Replication 8

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

Hierarchy

  • class \Drupal\serialization\Normalizer\NormalizerBase implements \Symfony\Component\Serializer\SerializerAwareInterface, CacheableNormalizerInterface uses \Symfony\Component\Serializer\SerializerAwareTrait
    • class \Drupal\replication\Normalizer\BulkDocsNormalizer implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface

Expanded class hierarchy of BulkDocsNormalizer

1 string reference to 'BulkDocsNormalizer'
replication.services.yml in ./replication.services.yml
replication.services.yml
1 service uses BulkDocsNormalizer
replication.normalizer.bulk_docs in ./replication.services.yml
Drupal\replication\Normalizer\BulkDocsNormalizer

File

src/Normalizer/BulkDocsNormalizer.php, line 10

Namespace

Drupal\replication\Normalizer
View source
class BulkDocsNormalizer extends NormalizerBase implements DenormalizerInterface {
  protected $supportedInterfaceOrClass = [
    'Drupal\\replication\\BulkDocs\\BulkDocsInterface',
  ];

  /**
   * {@inheritdoc}
   */
  public function normalize($bulk_docs, $format = NULL, array $context = []) {
    $data = [];

    /** @var \Drupal\replication\BulkDocs\BulkDocsInterface $bulk_docs */
    foreach ($bulk_docs
      ->getResult() as $result) {
      $data[] = $result;
    }
    return $data;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BulkDocsNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides NormalizerBase::$supportedInterfaceOrClass
BulkDocsNormalizer::denormalize public function Denormalizes data back into an object of the given class.
BulkDocsNormalizer::normalize public function Normalizes an object into a set of arrays/scalars.
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY constant Name of key for bubbling cacheability metadata via serialization context.
NormalizerBase::$format protected property List of formats which supports (de-)normalization. 3
NormalizerBase::addCacheableDependency protected function Adds cacheability if applicable.
NormalizerBase::checkFormat protected function Checks if the provided format is supported by this normalizer. 2
NormalizerBase::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() 1
NormalizerBase::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer. 1