class ReplicationLogNormalizer in Replication 8
Same name and namespace in other branches
- 8.2 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'
1 service uses ReplicationLogNormalizer
File
- src/
Normalizer/ ReplicationLogNormalizer.php, line 12
Namespace
Drupal\replication\NormalizerView source
class ReplicationLogNormalizer extends NormalizerBase implements DenormalizerInterface {
/**
* @var string[]
*/
protected $supportedInterfaceOrClass = [
'Drupal\\replication\\Entity\\ReplicationLog',
];
/**
* @var \Drupal\multiversion\Entity\Index\UuidIndexInterface
*/
protected $uuidIndex;
/**
* @var string[]
*/
protected $format = [
'json',
];
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;
}
}