You are here

public function CommerceGCTransactionEntityController::save in Commerce GC 7

Saves a transaction with a created date if it is new.

Parameters

$commerce_coupon: The full coupon object to save.

$transaction: An optional transaction object.

Return value

SAVED_NEW or SAVED_UPDATED depending on the operation performed.

Overrides DrupalCommerceEntityController::save

File

includes/commerce_gc.controller.inc, line 51
Giftcard transaction controller class.

Class

CommerceGCTransactionEntityController
@file Giftcard transaction controller class.

Code

public function save($entity, DatabaseTransaction $transaction = NULL) {
  if (empty($entity->{$this->idKey}) || !empty($entity->is_new)) {

    // Set the creation timestamp if not set, for new entities.
    if (empty($entity->date)) {
      $entity->date = REQUEST_TIME;
    }
  }

  // We do not do a transaction on updates nor do we do any balance checking.
  // Only administrators should ever be updating transaction amounts, and
  // they may have reasons for taking a giftcard balance below zero.
  if (empty($entity->is_new)) {
    return parent::save($entity);
  }

  // Ensure some level of data integrity before starting the transaction.
  if (!isset($entity->amount) || empty($entity->coupon_id)) {
    throw new Exception('Unable to save giftcard transaction: required properties missing');
  }

  // Insert operations incorporate a transactional balance check.
  if (!isset($transaction)) {
    $transaction = db_transaction();
  }
  try {

    // Load the balance. Set the forUpdate arg to TRUE to lock any matching
    // rows.
    $balance = commerce_gc_giftcard_balance($entity->coupon_id, TRUE);

    // If the transaction is a debit, only save if balance is sufficient.
    if ($entity->amount > 0 || abs($entity->amount) <= $balance) {
      return parent::save($entity, $transaction);
    }

    // Otherwise cancel the transaction to free the locked rows.
    $transaction
      ->rollback();
  } catch (Exception $e) {

    // Roll back the transaction if there was a problem like a lock timeout
    // or some other error.
    $transaction
      ->rollback();
    watchdog_exception($this->entityType, $e);
    throw $e;
  }
}