You are here

public function TransactionService::getLastExecutedTransaction in Transaction 8

Gets the last executed transaction for a given type and target entity.

Parameters

string|\Drupal\Core\Entity\ContentEntityInterface $target_entity: The target entity object or ID.

string|\Drupal\transaction\TransactionTypeInterface $transaction_type: The transaction type object or ID.

Return value

null|\Drupal\transaction\TransactionInterface The last executed transaction, NULL if not found.

Throws

\InvalidArgumentException If the given transaction type ID does not exists.

Overrides TransactionServiceInterface::getLastExecutedTransaction

File

src/TransactionService.php, line 32

Class

TransactionService
Transaction service.

Namespace

Drupal\transaction

Code

public function getLastExecutedTransaction($target_entity, $transaction_type) {

  // Argument processing.
  $target_entity_id = is_object($target_entity) ? $target_entity
    ->id() : $target_entity;
  if (!is_object($transaction_type) && !($transaction_type = $this->entityTypeManager
    ->getStorage('transaction_type')
    ->load($transaction_type))) {
    throw new \InvalidArgumentException('Invalid transaction type.');
  }
  $transaction_type_id = $transaction_type
    ->id();
  $target_entity_type_id = $transaction_type
    ->getTargetEntityTypeId();

  // Search the last executed transaction.
  $storage = $this->entityTypeManager
    ->getStorage('transaction');
  $result = $storage
    ->getQuery()
    ->condition('type', $transaction_type_id)
    ->condition('target_entity.target_type', $target_entity_type_id)
    ->condition('target_entity.target_id', $target_entity_id)
    ->exists('executed')
    ->range(0, 1)
    ->sort('execution_sequence', 'DESC')
    ->sort('executed', 'DESC')
    ->execute();
  return count($result) ? $storage
    ->load(array_pop($result)) : NULL;
}