You are here

class CommerceCouponUsageTransactionEntityController in Commerce Coupon 7.2

Coupon usage transaction controller class.

Hierarchy

Expanded class hierarchy of CommerceCouponUsageTransactionEntityController

1 string reference to 'CommerceCouponUsageTransactionEntityController'
commerce_coupon_usage_entity_info in modules/usage/commerce_coupon_usage.module
Implements hook_entity_info().

File

modules/usage/includes/commerce_coupon_usage.controller.inc, line 11
Coupon usage transaction controller class.

View source
class CommerceCouponUsageTransactionEntityController extends DrupalCommerceEntityController {
  public $transaction_id;
  public $coupon_id;
  public $order_id;
  public $uid;
  public $date;

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

  /**
   * Saves a usage transaction.
   *
   * @param object $entity
   *   The full coupon object to reference.
   * @param DatabaseTransaction $transaction
   *   An optional transaction object.
   *
   * @return mixed
   *   SAVED_NEW or SAVED_UPDATED depending on the operation performed.
   */
  public function save($entity, DatabaseTransaction $transaction = NULL) {

    // Do a normal entity save if the entity is not new. Usage records are just
    // boolean counters. We do not need atomic transaction handling on updates
    // since there is no "amount" field that can be changed.
    if (empty($entity->is_new)) {
      return parent::save($entity);
    }

    // Load the usage.
    if (!$entity->coupon_id) {
      throw new Exception('Cannot save a usage transaction without a coupon id');
    }
    $max_usage = commerce_coupon_usage_get_max_usage($entity->coupon_id);

    // If the entity is new, we have to use a transaction.
    if (!isset($transaction)) {
      $transaction = db_transaction();
    }

    // Lock the related coupon. Any future save operations will have to wait
    // until this lock is cleared before the following query will return in
    // their thread.
    try {
      db_select('commerce_coupon', 'c')
        ->fields('c', array(
        'coupon_id',
      ))
        ->condition('c.coupon_id', $entity->coupon_id)
        ->forUpdate()
        ->execute();

      // Load the usage.
      $usage = commerce_coupon_usage_get_usage($entity->coupon_id);

      // Only save if the coupon referenced will not exceed its maximum
      // usage settings. Zero max usage means unlimited.
      if ($max_usage > $usage || !$max_usage) {
        return parent::save($entity, $transaction);
      }

      // End the transaction if the allowed usage has been reached.
      $transaction
        ->rollback();
    } catch (Exception $e) {

      // Cancel the transaction if the coupon has no more uses. This clears the
      // lock on the control commerce_coupon row so that future save processes
      // may run.
      $transaction
        ->rollback();
      watchdog_exception($this->entityType, $e);
      throw $e;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CommerceCouponUsageTransactionEntityController::$coupon_id public property
CommerceCouponUsageTransactionEntityController::$date public property
CommerceCouponUsageTransactionEntityController::$order_id public property
CommerceCouponUsageTransactionEntityController::$transaction_id public property
CommerceCouponUsageTransactionEntityController::$uid public property
CommerceCouponUsageTransactionEntityController::create public function Create a new usage transaction. Overrides DrupalCommerceEntityController::create
CommerceCouponUsageTransactionEntityController::save public function Saves a usage transaction. 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.