You are here

public function CommerceLicenseRemoteBase::save in Commerce License 7

Overrides Entity::save().

Overrides CommerceLicenseBase::save

5 calls to CommerceLicenseRemoteBase::save()
CommerceLicenseRemoteBase::activate in includes/plugins/license_type/base.inc
Overrides CommerceLicenseBase::activate().
CommerceLicenseRemoteBase::expire in includes/plugins/license_type/base.inc
Overrides CommerceLicenseBase::expire().
CommerceLicenseRemoteBase::revoke in includes/plugins/license_type/base.inc
Overrides CommerceLicenseBase::revoke().
CommerceLicenseRemoteBase::suspend in includes/plugins/license_type/base.inc
Overrides CommerceLicenseBase::suspend().
CommerceLicenseRemoteExample::synchronize in modules/commerce_license_example/plugins/license_type/CommerceLicenseRemoteExample.class.php
Implements CommerceLicenseSynchronizableInterface::synchronize().

File

includes/plugins/license_type/base.inc, line 522
Abstract and interface plugin implementation.

Class

CommerceLicenseRemoteBase
Remote license base class.

Code

public function save() {

  // If this is an update, get the original sync status for later comparison.
  $original_sync_status = NULL;
  if (!empty($this->license_id)) {
    $this->original = entity_load_unchanged('commerce_license', $this->license_id);
    $original_sync_status = $this->original->wrapper->sync_status
      ->value();
    $sync_status = $this->wrapper->sync_status
      ->value();
    $sync_status_changed = $original_sync_status != $sync_status;
    $synced = $sync_status == COMMERCE_LICENSE_SYNCED;
    $pending = $this->status == COMMERCE_LICENSE_PENDING;

    // A pending license was just synchronized, update its status.
    if ($pending && $sync_status_changed && $synced) {
      $this->status = COMMERCE_LICENSE_ACTIVE;
    }
  }

  // Perform the save. This allows a presave / insert / update hook to
  // request synchronization by changing the sync_status field.
  parent::save();

  // Enqueue the sync if needed. Make sure not to enqueue if the license
  // alredy needed sync before this update.
  $sync_status = $this->wrapper->sync_status
    ->value();
  $needs_sync = $sync_status == COMMERCE_LICENSE_NEEDS_SYNC;
  $new_license = empty($this->license_id);
  $sync_status_changed = $original_sync_status != $sync_status;
  if ($needs_sync && ($new_license || $sync_status_changed)) {
    commerce_license_enqueue_sync($this);
  }
}