View source
<?php
namespace Drupal\replication\Normalizer;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\multiversion\Entity\Index\UuidIndexInterface;
use Drupal\replication\Entity\ReplicationLog;
use Drupal\replication\Entity\ReplicationLogInterface;
use Drupal\serialization\Normalizer\NormalizerBase;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
class ReplicationLogNormalizer extends NormalizerBase implements DenormalizerInterface {
protected $supportedInterfaceOrClass = [
'Drupal\\replication\\Entity\\ReplicationLog',
];
protected $uuidIndex;
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager, UuidIndexInterface $uuid_index) {
$this->entityTypeManager = $entity_type_manager;
$this->uuidIndex = $uuid_index;
}
public function normalize($entity, $format = NULL, array $context = []) {
$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;
}
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) {
if (in_array($type, [
'Drupal\\Core\\Entity\\ContentEntityInterface',
'Drupal\\replication\\Entity\\ReplicationLog',
], true)) {
if (!isset($data['@type']) || $data['@type'] === 'replication_log') {
return true;
}
}
return false;
}
}