You are here

public function Changes::getNormal in Replication 8.2

Same name and namespace in other branches
  1. 8 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 140

Class

Changes

Namespace

Drupal\replication\Changes

Code

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

  // When we have the since parameter set, we should return values starting
  // just after the since sequence (that is first in the $sequences array).
  if ($this->since > 0) {
    array_shift($sequences);
  }

  // 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['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;
    }

    // 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']);
    }

    // Filter the document.
    if ($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;
}