You are here

abstract class CommerceLicenseRemoteBase in Commerce License 7

Remote license base class.

Hierarchy

Expanded class hierarchy of CommerceLicenseRemoteBase

File

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

View source
abstract class CommerceLicenseRemoteBase extends CommerceLicenseBase implements CommerceLicenseSynchronizableInterface {

  /**
   * Constructor.
   *
   * @see Entity::__construct()
   */
  public function __construct(array $values = array(), $entityType = NULL) {
    parent::__construct($values, $entityType);

    // Initialize the sync status.
    // The [LANGUAGE_NONE][0]['value'] = 0; default is then added automatically.
    if (!isset($this->sync_status)) {
      $this->sync_status = array();
    }
  }

  /**
   * Implements EntityBundlePluginProvideFieldsInterface::fields().
   */
  static function fields() {
    $fields = parent::fields();
    $fields['sync_status']['field'] = array(
      'type' => 'list_integer',
      'cardinality' => 1,
      'settings' => array(
        'allowed_values' => array(
          0 => t('N/A'),
          COMMERCE_LICENSE_NEEDS_SYNC => t('Needs synchronization'),
          COMMERCE_LICENSE_SYNCED => t('Synchronized'),
          COMMERCE_LICENSE_SYNC_FAILED => t('Synchronization failed'),
        ),
      ),
    );
    $fields['sync_status']['instance'] = array(
      'label' => t('Synchronization status'),
      'required' => TRUE,
      'widget' => array(
        'type' => 'options_select',
      ),
    );
    return $fields;
  }

  /**
   * Overrides CommerceLicenseBase::form().
   */
  public function form(&$form, &$form_state) {
    parent::form($form, $form_state);

    // The sync status should not be editable by the customer.
    $form['sync_status']['#access'] = FALSE;
  }

  /**
   * Implements CommerceLicenseSynchronizableInterface::synchronize().
   */
  public function synchronize() {
    return TRUE;
  }

  /**
   * Overrides CommerceLicenseBase::checkoutCompletionMessage().
   */
  public function checkoutCompletionMessage() {
    $message = '';
    $sync_status = $this->wrapper->sync_status
      ->value();
    switch ($sync_status) {
      case COMMERCE_LICENSE_NEEDS_SYNC:
        $message = t("Please wait while we're contacting the remote service...");
        break;
      case COMMERCE_LICENSE_SYNCED:
        $message = 'Your license has been successfully created: <br />';
        $message .= $this
          ->accessDetails();
        break;
      case COMMERCE_LICENSE_SYNC_FAILED_RETRY:
        $message = t('Your license has been queued for processing.');
        break;
      case COMMERCE_LICENSE_SYNC_FAILED:
        $message = t('Oops... We were unable to generate your credentials.');
        break;
    }
    return $message;
  }

  /**
   * Overrides CommerceLicenseBase::activate().
   */
  public function activate($sync = TRUE) {
    if ($sync) {

      // The license will be activated after the initial synchronization.
      $this->wrapper->status = COMMERCE_LICENSE_PENDING;
      $this->wrapper->sync_status = COMMERCE_LICENSE_NEEDS_SYNC;
    }
    else {
      $this->wrapper->status = COMMERCE_LICENSE_ACTIVE;
    }
    $this
      ->save();
  }

  /**
   * Overrides CommerceLicenseBase::expire().
   */
  public function expire($sync = TRUE) {
    if ($sync) {
      $this->wrapper->sync_status = COMMERCE_LICENSE_NEEDS_SYNC;
    }
    $this->wrapper->status = COMMERCE_LICENSE_EXPIRED;
    $this
      ->save();
  }

  /**
   * Overrides CommerceLicenseBase::suspend().
   */
  public function suspend($sync = TRUE) {
    if ($sync) {
      $this->wrapper->sync_status = COMMERCE_LICENSE_NEEDS_SYNC;
    }
    $this->wrapper->status = COMMERCE_LICENSE_SUSPENDED;
    $this
      ->save();
  }

  /**
   * Overrides CommerceLicenseBase::revoke().
   */
  public function revoke($sync = TRUE) {
    if ($sync) {
      $this->wrapper->sync_status = COMMERCE_LICENSE_NEEDS_SYNC;
    }
    $this->wrapper->status = COMMERCE_LICENSE_REVOKED;
    $this
      ->save();
  }

  /**
   * Overrides Entity::save().
   */
  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);
    }
  }

  /**
   * Overrides CommerceLicenseBase::isValid().
   */
  public static function isValid() {
    $valid = parent::isValid();

    // Remote license types can't be used without the advancedqueue module.
    return $valid && module_exists('advancedqueue');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CommerceLicenseBase::$expires public property The date (unix timestamp) when the license expires. 0 for never.
CommerceLicenseBase::$expires_automatically public property Whether the module should expire the license automatically.
CommerceLicenseBase::$granted public property The date (unix timestamp) when the license was granted.
CommerceLicenseBase::$license_id public property The license id.
CommerceLicenseBase::$product_id public property The product_id of the licensed product.
CommerceLicenseBase::$revision_id public property The revision id.
CommerceLicenseBase::$status public property The license status.
CommerceLicenseBase::$type public property The license type (bundle).
CommerceLicenseBase::$uid public property The uid of the license owner.
CommerceLicenseBase::$wrapper public property License metadata wrapper. Overrides Entity::$wrapper
CommerceLicenseBase::accessDetails public function Implements CommerceLicenseInterface::accessDetails(). Overrides CommerceLicenseInterface::accessDetails 2
CommerceLicenseBase::formSubmit public function Implements CommerceLicenseInterface::formSubmit(). Overrides CommerceLicenseInterface::formSubmit
CommerceLicenseBase::formValidate public function Implements CommerceLicenseInterface::formValidate(). Overrides CommerceLicenseInterface::formValidate 1
CommerceLicenseBase::isConfigurable public function Implements CommerceLicenseInterface::isConfigurable(). Overrides CommerceLicenseInterface::isConfigurable 3
CommerceLicenseBase::renew public function Implements CommerceLicenseInterface::renew(). Overrides CommerceLicenseInterface::renew
CommerceLicenseRemoteBase::activate public function Overrides CommerceLicenseBase::activate(). Overrides CommerceLicenseBase::activate
CommerceLicenseRemoteBase::checkoutCompletionMessage public function Overrides CommerceLicenseBase::checkoutCompletionMessage(). Overrides CommerceLicenseBase::checkoutCompletionMessage
CommerceLicenseRemoteBase::expire public function Overrides CommerceLicenseBase::expire(). Overrides CommerceLicenseBase::expire
CommerceLicenseRemoteBase::fields static function Implements EntityBundlePluginProvideFieldsInterface::fields(). Overrides CommerceLicenseBase::fields 1
CommerceLicenseRemoteBase::form public function Overrides CommerceLicenseBase::form(). Overrides CommerceLicenseBase::form
CommerceLicenseRemoteBase::isValid public static function Overrides CommerceLicenseBase::isValid(). Overrides CommerceLicenseBase::isValid
CommerceLicenseRemoteBase::revoke public function Overrides CommerceLicenseBase::revoke(). Overrides CommerceLicenseBase::revoke
CommerceLicenseRemoteBase::save public function Overrides Entity::save(). Overrides CommerceLicenseBase::save
CommerceLicenseRemoteBase::suspend public function Overrides CommerceLicenseBase::suspend(). Overrides CommerceLicenseBase::suspend
CommerceLicenseRemoteBase::synchronize public function Implements CommerceLicenseSynchronizableInterface::synchronize(). Overrides CommerceLicenseSynchronizableInterface::synchronize 1
CommerceLicenseRemoteBase::__construct public function Constructor. Overrides CommerceLicenseBase::__construct
Entity::$defaultLabel protected property 1
Entity::$entityInfo protected property
Entity::$entityType protected property
Entity::$idKey protected property
Entity::buildContent public function Builds a structured array representing the entity's content. Overrides EntityInterface::buildContent 1
Entity::bundle public function Returns the bundle of the entity. Overrides EntityInterface::bundle
Entity::defaultLabel protected function Defines the entity label if the 'entity_class_label' callback is used. 1
Entity::defaultUri protected function Override this in order to implement a custom default URI and specify 'entity_class_uri' as 'uri callback' hook_entity_info().
Entity::delete public function Permanently deletes the entity. Overrides EntityInterface::delete
Entity::entityInfo public function Returns the info of the type of the entity. Overrides EntityInterface::entityInfo
Entity::entityType public function Returns the type of the entity. Overrides EntityInterface::entityType
Entity::export public function Exports the entity. Overrides EntityInterface::export
Entity::getTranslation public function Gets the raw, translated value of a property or field. Overrides EntityInterface::getTranslation
Entity::hasStatus public function Checks if the entity has a certain exportable status. Overrides EntityInterface::hasStatus
Entity::identifier public function Returns the entity identifier, i.e. the entities name or numeric id. Overrides EntityInterface::identifier
Entity::internalIdentifier public function Returns the internal, numeric identifier. Overrides EntityInterface::internalIdentifier
Entity::isDefaultRevision public function Checks whether the entity is the default revision. Overrides EntityInterface::isDefaultRevision
Entity::label public function Returns the label of the entity. Overrides EntityInterface::label
Entity::setUp protected function Set up the object instance on construction or unserializiation.
Entity::uri public function Returns the uri of the entity just as entity_uri(). Overrides EntityInterface::uri
Entity::view public function Generate an array for rendering the entity. Overrides EntityInterface::view
Entity::wrapper public function Returns the EntityMetadataWrapper of the entity. Overrides EntityInterface::wrapper
Entity::__sleep public function Magic method to only serialize what's necessary.
Entity::__wakeup public function Magic method to invoke setUp() on unserialization.