BulkDocs.php in Replication 8
File
src/BulkDocs/BulkDocs.php
View source
<?php
namespace Drupal\replication\BulkDocs;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Lock\LockBackendInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\Utility\Error;
use Drupal\multiversion\Entity\Index\RevisionIndexInterface;
use Drupal\multiversion\Entity\Index\UuidIndexInterface;
use Drupal\multiversion\Entity\WorkspaceInterface;
use Drupal\multiversion\Workspace\WorkspaceManagerInterface;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
class BulkDocs implements BulkDocsInterface {
use DependencySerializationTrait;
protected $workspaceManager;
protected $workspace;
protected $uuidIndex;
protected $revIndex;
protected $entityTypeManager;
protected $lock;
protected $logger;
protected $entities = [];
protected $newEdits = TRUE;
protected $result = [];
protected $state;
protected $config;
public function __construct(WorkspaceManagerInterface $workspace_manager, WorkspaceInterface $workspace, UuidIndexInterface $uuid_index, RevisionIndexInterface $rev_index, EntityTypeManagerInterface $entity_type_manager, LockBackendInterface $lock, LoggerChannelInterface $logger, StateInterface $state, ImmutableConfig $config) {
$this->workspaceManager = $workspace_manager;
$this->workspace = $workspace;
$this->uuidIndex = $uuid_index;
$this->revIndex = $rev_index;
$this->entityTypeManager = $entity_type_manager;
$this->lock = $lock;
$this->logger = $logger;
$this->state = $state;
$this->config = $config;
}
public function newEdits($new_edits) {
$this->newEdits = (bool) $new_edits;
return $this;
}
public function setEntities($entities) {
$this->entities = $entities;
return $this;
}
public function getEntities() {
return $this->entities;
}
public function save() {
do {
if ($this->lock
->lockMayBeAvailable('bulk_docs')) {
if ($this->lock
->acquire('bulk_docs')) {
break;
}
}
$this->logger
->critical('Lock exists on bulk operation. Waiting.');
} while ($this->lock
->wait('bulk_docs', 3000));
$inital_workspace = $this->workspaceManager
->getActiveWorkspace();
$this->workspaceManager
->setActiveWorkspace($this->workspace);
$this->state
->set('comment.maintain_entity_statistics', FALSE);
foreach ($this->entities as $entity) {
$uuid = $entity
->uuid();
$rev = $entity->_rev->value;
try {
$record = $this->revIndex
->useWorkspace($this->workspace
->id())
->get("{$uuid}:{$rev}");
if ($record) {
if (!$this->newEdits && !$record['is_stub']) {
$this->result[] = [
'error' => 'Conflict',
'reason' => 'Document update conflict.',
'id' => $uuid,
'rev' => $rev,
];
continue;
}
}
$entity_type = $this->entityTypeManager
->getDefinition($entity
->getEntityTypeId());
$id_key = $entity_type
->getKey('id');
if ($record = $this->uuidIndex
->useWorkspace($this->workspace
->id())
->get($entity
->uuid())) {
$entity->{$id_key}->value = $record['entity_id'];
$entity
->enforceIsNew(FALSE);
}
else {
$entity
->enforceIsNew(TRUE);
$entity->{$id_key}->value = NULL;
}
$entity->workspace->target_id = $this->workspace
->id();
$entity->_rev->new_edit = $this->newEdits;
$this->entityTypeManager
->getStorage($entity
->getEntityTypeId())
->useWorkspace($this->workspace
->id());
if ($entity
->save()) {
$this->result[] = [
'id' => $uuid,
'ok' => TRUE,
'rev' => $entity->_rev->value,
];
if ($this->config
->get('verbose_logging')) {
$this->logger
->info($entity_type
->getLabel() . ' ' . $entity
->label() . ' saved in workspace ' . $this->workspace
->label());
}
}
} catch (\Throwable $e) {
$message = $e
->getMessage();
$this->result[] = [
'error' => $message,
'reason' => 'Exception',
'id' => $uuid,
'rev' => $entity->_rev->value,
];
$arguments = Error::decodeException($e) + [
'%uuid' => $uuid,
];
$this->logger
->error('%type: @message in %function (line %line of %file). The error occurred while saving the entity with the UUID: %uuid.', $arguments);
}
}
$this->state
->set('comment.maintain_entity_statistics', TRUE);
$this->workspaceManager
->setActiveWorkspace($inital_workspace);
$this->lock
->release('bulk_docs');
return $this;
}
public function getResult() {
return $this->result;
}
}