You are here

public function TransactorHandler::doExecute in Transaction 8

Executes a transaction.

Parameters

\Drupal\transaction\TransactionInterface $transaction: The transaction to execute.

bool $save: Save the transaction after succeeded execution.

\Drupal\User\UserInterface $executor: (optional) The user that executes the transaction. The current user by default.

Return value

bool TRUE if transaction was executed, FALSE otherwise.

Throws

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

\Drupal\transaction\Exception\ExecutionTimeoutException If the transaction execution time exceeds the allowed threshold.

Overrides TransactorHandlerInterface::doExecute

File

src/TransactorHandler.php, line 119

Class

TransactorHandler
Transactor entity handler.

Namespace

Drupal\transaction

Code

public function doExecute(TransactionInterface $transaction, $save = TRUE, UserInterface $executor = NULL) {

  // Locks the transactional flow for execution, preventing other transactions
  // of the same flow to be executed simultaneously.
  if (!($execution_lock_name = $this
    ->executionLockAcquire($transaction))) {
    throw new ExecutionTimeoutException('Unable to lock the transactional flow for transaction execution.');
  }
  if (!$transaction
    ->isPending()) {

    // Releases the execution lock.
    $this->lock
      ->release($execution_lock_name);
    throw new InvalidTransactionStateException('Cannot execute an already executed transaction.');
  }
  $transaction_type = $transaction
    ->getType();
  $last_executed = $this->transactionService
    ->getLastExecutedTransaction($transaction
    ->getTargetEntityId(), $transaction_type);
  $transactor = $transaction_type
    ->getPlugin();
  if ($transactor
    ->executeTransaction($transaction, $last_executed)) {

    // If no result code set by the transactor, set the generic for
    // successful execution.
    if (!$transaction
      ->getResultCode()) {
      $transaction
        ->setResultCode(TransactorPluginInterface::RESULT_OK);
    }

    // Sets the execution time and sequence.
    $transaction
      ->setExecutionTime($this->timeService
      ->getRequestTime());
    $transaction
      ->setExecutionSequence($last_executed ? $last_executed
      ->getExecutionSequence() + 1 : 1);
    if (!$executor && $this->currentUser && $this->currentUser
      ->id()) {
      $executor = User::load($this->currentUser
        ->id());
    }
    $transaction
      ->setExecutor($executor ?: User::getAnonymousUser());

    // Launch the transaction execution event.
    $this->eventDispatcher
      ->dispatch(TransactionExecutionEvent::EVENT_NAME, new TransactionExecutionEvent($transaction));

    // Save the transaction.
    if ($save) {
      $transaction
        ->save();
    }
    $executed = TRUE;
  }
  else {

    // If no result code set by the transactor, set the generic error.
    if (!$transaction
      ->getResultCode()) {
      $transaction
        ->setResultCode(TransactorPluginInterface::RESULT_ERROR);
    }
    $executed = FALSE;
  }

  // Releases the execution lock.
  $this->lock
    ->release($execution_lock_name);
  return $executed;
}