You are here

public function TransactorHandler::getPreviousTransaction in Transaction 8

Get the previous same-type transaction in order of execution.

Parameters

\Drupal\transaction\TransactionInterface $transaction: The transaction from which to get the previous.

Return value

\Drupal\transaction\TransactionInterface The previously executed transaction. NULL if this is the first one.

Throws

\Drupal\transaction\InvalidTransactionStateException If the transaction is no executed.

Overrides TransactorHandlerInterface::getPreviousTransaction

File

src/TransactorHandler.php, line 270

Class

TransactorHandler
Transactor entity handler.

Namespace

Drupal\transaction

Code

public function getPreviousTransaction(TransactionInterface $transaction) {
  if ($transaction
    ->isPending()) {
    throw new InvalidTransactionStateException('Cannot get the previously executed transaction to one that is pending execution.');
  }
  $result = $this->transactionStorage
    ->getQuery()
    ->condition('type', $transaction
    ->getTypeId())
    ->condition('target_entity.target_id', $transaction
    ->getTargetEntityId())
    ->condition('target_entity.target_type', $transaction
    ->getType()
    ->getTargetEntityTypeId())
    ->exists('executed')
    ->condition('executed', $transaction
    ->getExecutionTime(), '<')
    ->range(0, 1)
    ->sort('executed', 'DESC')
    ->execute();
  return count($result) ? $this->transactionStorage
    ->load(array_pop($result)) : NULL;
}