You are here

class ReplicationLogNormalizer in Replication 8.2

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

Hierarchy

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

Expanded class hierarchy of ReplicationLogNormalizer

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

File

src/Normalizer/ReplicationLogNormalizer.php, line 12

Namespace

Drupal\replication\Normalizer
View source
class ReplicationLogNormalizer extends NormalizerBase implements DenormalizerInterface {

  /**
   * @var string[]
   */
  protected $supportedInterfaceOrClass = [
    'Drupal\\replication\\Entity\\ReplicationLog',
  ];

  /**
   * @var \Drupal\multiversion\Entity\Index\UuidIndexInterface
   */
  protected $uuidIndex;

  /**
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   * @param \Drupal\multiversion\Entity\Index\UuidIndexInterface $uuid_index
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, UuidIndexInterface $uuid_index) {
    $this->entityTypeManager = $entity_type_manager;
    $this->uuidIndex = $uuid_index;
  }

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

    // Strictly format the entity how CouchDB expects it, plus our JSON-LD data.
    $data = [
      '@context' => [
        '_id' => '@id',
      ],
      '@type' => 'replication_log',
      '_id' => '_local/' . $entity
        ->uuid(),
      '_rev' => $entity->_rev->value,
      'history' => $this->serializer
        ->normalize($entity
        ->get('history'), $format, $context),
      'session_id' => $entity
        ->getSessionId(),
      'source_last_seq' => $entity
        ->getSourceLastSeq(),
    ];
    return $data;
  }

  /**
   * @inheritDoc
   */
  public function denormalize($data, $class, $format = NULL, array $context = []) {
    $data['_id'] = str_replace('_local/', '', $data['_id']);
    $record = $this->uuidIndex
      ->get($data['_id']);
    if (!empty($record['entity_type_id']) && !empty($record['entity_id'])) {
      $storage = $this->entityTypeManager
        ->getStorage($record['entity_type_id']);
      $entity = $storage
        ->load($record['entity_id']);
      if ($entity instanceof ReplicationLogInterface) {
        foreach ($data as $name => $value) {
          $entity->{$name} = $value;
        }
        return $entity;
      }
    }
    try {
      $data['uuid'][0]['value'] = $data['_id'];
      $entity = ReplicationLog::create($data);
      return $entity;
    } catch (\Exception $e) {
      watchdog_exception('Replication', $e);
    }
  }
  public function supportsDenormalization($data, $type, $format = NULL) {

    // We need to accept both ReplicationLog and ContentEntityInterface classes.
    // LocalDocResource entities are treated as standard documents (content entities)
    if (in_array($type, [
      'Drupal\\Core\\Entity\\ContentEntityInterface',
      'Drupal\\replication\\Entity\\ReplicationLog',
    ], true)) {

      // If a document doesn't have a type set, we assume it's a replication log.
      // We also support documents specifically specified as replication logs.
      if (!isset($data['@type']) || $data['@type'] === 'replication_log') {
        return true;
      }
    }
    return false;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer. 1
ReplicationLogNormalizer::$entityTypeManager protected property
ReplicationLogNormalizer::$supportedInterfaceOrClass protected property Overrides NormalizerBase::$supportedInterfaceOrClass
ReplicationLogNormalizer::$uuidIndex protected property
ReplicationLogNormalizer::denormalize public function @inheritDoc
ReplicationLogNormalizer::normalize public function Normalizes an object into a set of arrays/scalars.
ReplicationLogNormalizer::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() Overrides NormalizerBase::supportsDenormalization
ReplicationLogNormalizer::__construct public function