class CommerceCouponUsageTransactionEntityController in Commerce Coupon 7.2
Coupon usage transaction controller class.
Hierarchy
- class \DrupalDefaultEntityController implements DrupalEntityControllerInterface
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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CommerceCouponUsageTransactionEntityController:: |
public | property | ||
CommerceCouponUsageTransactionEntityController:: |
public | property | ||
CommerceCouponUsageTransactionEntityController:: |
public | property | ||
CommerceCouponUsageTransactionEntityController:: |
public | property | ||
CommerceCouponUsageTransactionEntityController:: |
public | property | ||
CommerceCouponUsageTransactionEntityController:: |
public | function |
Create a new usage transaction. Overrides DrupalCommerceEntityController:: |
|
CommerceCouponUsageTransactionEntityController:: |
public | function |
Saves a usage transaction. Overrides DrupalCommerceEntityController:: |
|
DrupalCommerceEntityController:: |
protected | property | Stores our transaction object, necessary for pessimistic locking to work. | |
DrupalCommerceEntityController:: |
protected | property | Stores the ids of locked entities, necessary for knowing when to release a lock by committing the transaction. | |
DrupalCommerceEntityController:: |
protected | property | Stores the ids of unchanged entities, necessary for knowing if we're dealing with unchanged entities before acting on them. | |
DrupalCommerceEntityController:: |
public | function |
Builds a structured array representing the entity's content. Overrides EntityAPIControllerInterface:: |
2 |
DrupalCommerceEntityController:: |
protected | function |
Override of DrupalDefaultEntityController::buildQuery(). Overrides DrupalDefaultEntityController:: |
|
DrupalCommerceEntityController:: |
public | function |
Delete permanently saved entities. Overrides EntityAPIControllerInterface:: |
4 |
DrupalCommerceEntityController:: |
public | function |
Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface:: |
|
DrupalCommerceEntityController:: |
public | function |
Implements EntityAPIControllerInterface. Overrides EntityAPIControllerInterface:: |
|
DrupalCommerceEntityController:: |
public | function |
(Internal use) Invokes a hook on behalf of the entity. Overrides EntityAPIControllerInterface:: |
|
DrupalCommerceEntityController:: |
public | function |
Implements DrupalCommerceEntityControllerInterface::isUnchanged(). Overrides DrupalCommerceEntityControllerInterface:: |
|
DrupalCommerceEntityController:: |
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:: |
public | function |
Implements DrupalEntityControllerInterface::resetCache(). Overrides DrupalDefaultEntityController:: |
|
DrupalCommerceEntityController:: |
public | function |
Generate an array for rendering the given entities. Overrides EntityAPIControllerInterface:: |
|
DrupalDefaultEntityController:: |
protected | property | Whether this entity type should use the static cache. | |
DrupalDefaultEntityController:: |
protected | property | Static cache of entities, keyed by entity ID. | |
DrupalDefaultEntityController:: |
protected | property | Array of information about the entity. | |
DrupalDefaultEntityController:: |
protected | property | Entity type for this controller instance. | |
DrupalDefaultEntityController:: |
protected | property | Additional arguments to pass to hook_TYPE_load(). | |
DrupalDefaultEntityController:: |
protected | property | Name of the entity's ID field in the entity database table. | |
DrupalDefaultEntityController:: |
protected | property | Name of entity's revision database table field, if it supports revisions. | |
DrupalDefaultEntityController:: |
protected | property | The table that stores revisions, if the entity supports revisions. | |
DrupalDefaultEntityController:: |
protected | function | Attaches data to entities upon loading. | 4 |
DrupalDefaultEntityController:: |
protected | function | Gets entities from the static cache. | 1 |
DrupalDefaultEntityController:: |
protected | function | Stores entities in the static entity cache. | |
DrupalDefaultEntityController:: |
protected | function | Ensures integer entity IDs are valid. | |
DrupalDefaultEntityController:: |
protected | function | Callback for array_filter that removes non-integer IDs. | |
DrupalDefaultEntityController:: |
public | function |
Implements DrupalEntityControllerInterface::load(). Overrides DrupalEntityControllerInterface:: |
|
DrupalDefaultEntityController:: |
public | function | Constructor: sets basic variables. |