You are here

class CommerceGCTransactionEntityController in Commerce GC 7

@file Giftcard transaction controller class.

Hierarchy

Expanded class hierarchy of CommerceGCTransactionEntityController

1 string reference to 'CommerceGCTransactionEntityController'
commerce_gc_entity_info in ./commerce_gc.module

File

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

View source
class CommerceGCTransactionEntityController extends DrupalCommerceEntityController {
  public $amount;
  public $transaction_id;
  public $coupon_id;
  public $status;
  public $date;

  /**
   * Create a default transaction.
   *
   * @param array $values
   *   An array of values to set, keyed by property name.
   * @return
   *   A product object with all default fields initialized.
   */
  public function create(array $values = array()) {
    $values += array(
      'coupon_id' => '',
      'transaction_id' => '',
      'amount' => 0,
      'date' => '',
      'status' => 'completed',
    );
    return parent::create($values);
  }

  /**
   * Saves a transaction with a created date if it is new.
   *
   * @param $commerce_coupon
   *   The full coupon object to save.
   * @param $transaction
   *   An optional transaction object.
   *
   * @return
   *   SAVED_NEW or SAVED_UPDATED depending on the operation performed.
   */
  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;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CommerceGCTransactionEntityController::$amount public property
CommerceGCTransactionEntityController::$coupon_id public property
CommerceGCTransactionEntityController::$date public property
CommerceGCTransactionEntityController::$status public property
CommerceGCTransactionEntityController::$transaction_id public property
CommerceGCTransactionEntityController::create public function Create a default transaction. Overrides DrupalCommerceEntityController::create
CommerceGCTransactionEntityController::save public function Saves a transaction with a created date if it is new. Overrides DrupalCommerceEntityController::save
DrupalCommerceEntityController::$controllerTransaction protected property Stores our transaction object, necessary for pessimistic locking to work.
DrupalCommerceEntityController::$lockedEntities protected property Stores the ids of locked entities, necessary for knowing when to release a lock by committing the transaction.
DrupalCommerceEntityController::$unchangedEntities protected property Stores the ids of unchanged entities, necessary for knowing if we're dealing with unchanged entities before acting on them.
DrupalCommerceEntityController::buildContent public function Builds a structured array representing the entity's content. Overrides EntityAPIControllerInterface::buildContent 2
DrupalCommerceEntityController::buildQuery protected function Override of DrupalDefaultEntityController::buildQuery(). Overrides DrupalDefaultEntityController::buildQuery
DrupalCommerceEntityController::delete public function Delete permanently saved entities. Overrides EntityAPIControllerInterface::delete 4
DrupalCommerceEntityController::export public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::export
DrupalCommerceEntityController::import public function Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface::import
DrupalCommerceEntityController::invoke public function (Internal use) Invokes a hook on behalf of the entity. Overrides EntityAPIControllerInterface::invoke
DrupalCommerceEntityController::isUnchanged public function Implements DrupalCommerceEntityControllerInterface::isUnchanged(). Overrides DrupalCommerceEntityControllerInterface::isUnchanged
DrupalCommerceEntityController::releaseLock protected function Checks the list of tracked locked entities, and if it's empty, commits the transaction in order to remove the acquired locks.
DrupalCommerceEntityController::resetCache public function Implements DrupalEntityControllerInterface::resetCache(). Overrides DrupalDefaultEntityController::resetCache
DrupalCommerceEntityController::view public function Generate an array for rendering the given entities. Overrides EntityAPIControllerInterface::view
DrupalDefaultEntityController::$cache protected property Whether this entity type should use the static cache.
DrupalDefaultEntityController::$entityCache protected property Static cache of entities, keyed by entity ID.
DrupalDefaultEntityController::$entityInfo protected property Array of information about the entity.
DrupalDefaultEntityController::$entityType protected property Entity type for this controller instance.
DrupalDefaultEntityController::$hookLoadArguments protected property Additional arguments to pass to hook_TYPE_load().
DrupalDefaultEntityController::$idKey protected property Name of the entity's ID field in the entity database table.
DrupalDefaultEntityController::$revisionKey protected property Name of entity's revision database table field, if it supports revisions.
DrupalDefaultEntityController::$revisionTable protected property The table that stores revisions, if the entity supports revisions.
DrupalDefaultEntityController::attachLoad protected function Attaches data to entities upon loading. 4
DrupalDefaultEntityController::cacheGet protected function Gets entities from the static cache. 1
DrupalDefaultEntityController::cacheSet protected function Stores entities in the static entity cache.
DrupalDefaultEntityController::cleanIds protected function Ensures integer entity IDs are valid.
DrupalDefaultEntityController::filterId protected function Callback for array_filter that removes non-integer IDs.
DrupalDefaultEntityController::load public function Implements DrupalEntityControllerInterface::load(). Overrides DrupalEntityControllerInterface::load
DrupalDefaultEntityController::__construct public function Constructor: sets basic variables.