BulkDocsNormalizer.php in Replication 8
File
src/Normalizer/BulkDocsNormalizer.php
View source
<?php
namespace Drupal\replication\Normalizer;
use Drupal\Component\Serialization\Json;
use Drupal\serialization\Normalizer\NormalizerBase;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
class BulkDocsNormalizer extends NormalizerBase implements DenormalizerInterface {
protected $supportedInterfaceOrClass = [
'Drupal\\replication\\BulkDocs\\BulkDocsInterface',
];
public function normalize($bulk_docs, $format = NULL, array $context = []) {
$data = [];
foreach ($bulk_docs
->getResult() as $result) {
$data[] = $result;
}
return $data;
}
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 {
$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);
}
if (!empty($doc['_id']) && strpos($doc['_id'], 'local') !== FALSE) {
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);
}
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;
}
}