You are here

public function LinkExtractorService::updateEntityExtractIndex in Link checker 8

Adds or updates extract index for given entity.

This should be run after saving extracted links from given entity.

Parameters

\Drupal\Core\Entity\FieldableEntityInterface $entity: The entity.

File

src/LinkExtractorService.php, line 372

Class

LinkExtractorService
Class LinkExtractor.

Namespace

Drupal\linkchecker

Code

public function updateEntityExtractIndex(FieldableEntityInterface $entity) {

  // We can`t use Connection::upsert() here cause primary key consist of two
  // columns, entity_id and entity_type.
  $isExistsQuery = $this->database
    ->select('linkchecker_index', 'i');
  $isExistsQuery
    ->fields('i');
  $isExistsQuery
    ->condition('entity_id', $entity
    ->id());
  $isExistsQuery
    ->condition('entity_type', $entity
    ->getEntityTypeId());
  $isExistsQuery
    ->range(0, 1);
  $isExists = $isExistsQuery
    ->execute()
    ->fetchField();
  if (empty($isExists)) {
    $this->database
      ->insert('linkchecker_index')
      ->fields([
      'entity_id' => $entity
        ->id(),
      'entity_type' => $entity
        ->getEntityTypeId(),
      'last_extracted_time' => $this->time
        ->getCurrentTime(),
    ])
      ->execute();
  }
  else {
    $this->database
      ->update('linkchecker_index')
      ->fields([
      'last_extracted_time' => $this->time
        ->getCurrentTime(),
    ])
      ->condition('entity_id', $entity
      ->id())
      ->condition('entity_type', $entity
      ->getEntityTypeId())
      ->execute();
  }
}