You are here

protected function PublisherTracker::insertOrUpdate in Acquia Content Hub 8.2

Determines if an entity will be inserted or updated with a status.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity to add tracking to.

string $status: The status of the tracking.

string $hash: A sha1 hash of the data attribute for change management.

Return value

\Drupal\Core\Database\StatementInterface|int|null Database statement.

Throws

\Exception

2 calls to PublisherTracker::insertOrUpdate()
PublisherTracker::queue in modules/acquia_contenthub_publisher/src/PublisherTracker.php
Add tracking for an entity in a self::QUEUED state.
PublisherTracker::track in modules/acquia_contenthub_publisher/src/PublisherTracker.php
Add tracking for an entity in a self::EXPORTED state.

File

modules/acquia_contenthub_publisher/src/PublisherTracker.php, line 157

Class

PublisherTracker
The publisher tracker table class.

Namespace

Drupal\acquia_contenthub_publisher

Code

protected function insertOrUpdate(EntityInterface $entity, string $status, string $hash = '') {
  if ($entity instanceof EntityChangedInterface) {
    $modified = date('c', $entity
      ->getChangedTime());
  }
  else {
    $modified = date('c');
  }

  // If we've previously tracked this thing, set its created date.
  if ($entity instanceof NodeInterface) {
    $created = date('c', $entity
      ->getCreatedTime());
  }
  else {
    $created = $modified;
  }

  // Must check because of created field.
  $results = $this
    ->get($entity
    ->uuid());

  // If entity in the table update fields but not all of them.
  if ($results) {
    $values = [
      'status' => $status,
      'modified' => $modified,
      'entity_uuid' => $entity
        ->uuid(),
    ];
    if ($hash) {
      $values['hash'] = $hash;
    }
  }
  else {
    $values = [
      'entity_type' => $entity
        ->getEntityTypeId(),
      'entity_id' => $entity
        ->id(),
      'entity_uuid' => $entity
        ->uuid(),
      'status' => $status,
      'created' => $created,
      'modified' => $modified,
      'hash' => $hash,
    ];
  }
  return $this->database
    ->merge(self::EXPORT_TRACKING_TABLE)
    ->key('entity_uuid', $entity
    ->uuid())
    ->fields($values)
    ->execute();
}