You are here

public function Changes::getNormal in Replication 8

Same name and namespace in other branches
  1. 8.2 src/Changes/Changes.php \Drupal\replication\Changes\Changes::getNormal()

Return the changes in a 'normal' way.

Overrides ChangesInterface::getNormal

File

src/Changes/Changes.php, line 162

Class

Changes

Namespace

Drupal\replication\Changes

Code

public function getNormal() {
  $sequences = $this->sequenceIndex
    ->useWorkspace($this->workspaceId)
    ->getRange($this->since, $this->stop);

  // Setup filter plugin.
  $parameters = is_array($this->parameters) ? $this->parameters : [];
  $filter = NULL;
  if (is_string($this->filter) && $this->filter) {
    $filter = $this->filterManager
      ->createInstance($this->filter, $parameters);
  }
  elseif (isset($parameters['doc_ids'])) {
    $filter = $this->filterManager
      ->createInstance('_doc_ids', $parameters);
  }
  elseif (isset($parameters['uuids'])) {
    $filter = $this->filterManager
      ->createInstance('uuid', $parameters);
  }

  // Format the result array.
  $changes = [];
  $count = 0;
  foreach ($sequences as $sequence) {
    if (!empty($sequence['local']) || !empty($sequence['is_stub'])) {
      continue;
    }

    // When we have the since parameter set, we should exclude the value with
    // that sequence from the results.
    if ($this->since > 0 && $sequence['seq'] == $this->since) {
      continue;
    }

    // Get the document.
    $revision = NULL;
    if ($this->includeDocs == TRUE || $filter !== NULL) {

      /** @var \Drupal\multiversion\Entity\Storage\ContentEntityStorageInterface $storage */
      $storage = $this->entityTypeManager
        ->getStorage($sequence['entity_type_id']);
      $storage
        ->useWorkspace($this->workspaceId);
      $revision = $storage
        ->loadRevision($sequence['revision_id']);
      $storage
        ->useWorkspace(NULL);
    }

    // Filter the document.
    if ($revision && $filter !== NULL && !$filter
      ->filter($revision)) {
      continue;
    }
    if ($this->limit && $count >= $this->limit) {
      break;
    }
    $uuid = $sequence['entity_uuid'];
    if (!isset($changes[$uuid])) {
      $count++;
    }
    $changes[$uuid] = [
      'changes' => [
        [
          'rev' => $sequence['rev'],
        ],
      ],
      'id' => $uuid,
      'seq' => $sequence['seq'],
    ];
    if ($sequence['deleted']) {
      $changes[$uuid]['deleted'] = TRUE;
    }

    // Include the document.
    if ($this->includeDocs == TRUE) {
      $changes[$uuid]['doc'] = $this->serializer
        ->normalize($revision);
    }
  }

  // Now when we have rebuilt the result array we need to ensure that the
  // results array is still sorted on the sequence key, as in the index.
  $return = array_values($changes);
  usort($return, function ($a, $b) {
    return $a['seq'] - $b['seq'];
  });
  return $return;
}