You are here

class LicenseSubscription in Commerce License 8.2

Provides a Commerce Recurring subscription type for use with licenses.

Plugin annotation


@CommerceSubscriptionType(
  id = "license",
  label = @Translation("License"),
  purchasable_entity_type = "commerce_product_variation",
)

Hierarchy

Expanded class hierarchy of LicenseSubscription

File

src/Plugin/Commerce/SubscriptionType/LicenseSubscription.php, line 20

Namespace

Drupal\commerce_license\Plugin\Commerce\SubscriptionType
View source
class LicenseSubscription extends SubscriptionTypeBase {

  /**
   * {@inheritdoc}
   */
  public function onSubscriptionCreate(SubscriptionInterface $subscription, OrderItemInterface $order_item) {
    $purchased_entity = $subscription
      ->getPurchasedEntity();
    $uid = $subscription
      ->getCustomerId();

    // Ensure that the order item being used has the license trait, otherwise
    // the license won't get handled properly.
    if (!$order_item
      ->hasField('license')) {
      throw new \Exception(sprintf("Order item type %s used for product variation %s is missing the license field.", $order_item
        ->bundle(), $purchased_entity
        ->id()));
    }

    // The order item should already have a license set, as our
    // \Drupal\commerce_license\EventSubscriber\OrderSubscriber's
    // commerce_order.place.pre_transition listener
    // should run before Commerce Recurring's
    // \Drupal\commerce_recurring\EventSubscriber\EventSubscriber listener,
    // which is what then creates the subscription.
    if (empty($order_item->license->entity)) {

      // Something's gone wrong: either other code has changed priorities, or
      // the modules' relative priorities have become out of sync due to changes
      // in code.
      throw new \Exception(sprintf("Attempt to create a license subscription with order item ID %s that doesn't have a license.", $order_item
        ->id()));
    }

    // Get the license the order item refers to.
    $license = $order_item->license->entity;

    // Ensure that the license expiry is unlimited.
    if ($license->expiration_type->target_plugin_id != 'unlimited') {
      throw new \Exception(sprintf("Invalid expiry type %s on product variation %s", $license->expiration_type->target_plugin_id, $purchased_entity
        ->id()));
    }

    // Set the license on the subscription, but don't save the subscription, as
    // it's currently only being created by the storage handler.
    $subscription->license = $license;
  }

  /**
   * {@inheritdoc}
   */
  public function onSubscriptionActivate(SubscriptionInterface $subscription, OrderInterface $order) {

    // We don't need to do anything here, as LicenseOrderSyncSubscriber takes
    // care of activating the license in an initial order with a subscription.
  }

  /**
   * {@inheritdoc}
   */
  public function onSubscriptionRenew(SubscriptionInterface $subscription, OrderInterface $order, OrderInterface $next_order) {
    $license = $subscription->license->entity;

    // Change the license's renewed time and save it.
    // Use the subscription's renewed time rather than the current time to
    // ensure the timestamps are in sync.
    $license
      ->setRenewedTime($subscription
      ->getRenewedTime());
    $license
      ->save();
  }

  /**
   * {@inheritdoc}
   */
  public function onSubscriptionExpire(SubscriptionInterface $subscription) {
    $license = $subscription->license->entity;

    // Change the license's state to expired.
    // The License entity will handle deactivating the license type plugin.
    $transition = $license
      ->getState()
      ->getWorkflow()
      ->getTransition('expire');
    $license
      ->getState()
      ->applyTransition($transition);
    $license
      ->save();
  }

  /**
   * {@inheritdoc}
   */
  public function onSubscriptionCancel(SubscriptionInterface $subscription) {
    $license = $subscription->license->entity;

    // Change the license's state to canceled.
    $transition = $license
      ->getState()
      ->getWorkflow()
      ->getTransition('cancel');
    $license
      ->getState()
      ->applyTransition($transition);
    $license
      ->save();
  }

  /**
   * {@inheritdoc}
   */
  public function buildFieldDefinitions() {
    $fields = parent::buildFieldDefinitions();
    $fields['license'] = BundleFieldDefinition::create('entity_reference')
      ->setLabel(t('License'))
      ->setDescription(t('The license this subscription controls.'))
      ->setCardinality(1)
      ->setRequired(TRUE)
      ->setSetting('target_type', 'commerce_license');
    return $fields;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
LicenseSubscription::buildFieldDefinitions public function Builds the field definitions for entities of this bundle. Overrides SubscriptionTypeBase::buildFieldDefinitions
LicenseSubscription::onSubscriptionActivate public function Acts on a subscription after it has been activated. Overrides SubscriptionTypeBase::onSubscriptionActivate
LicenseSubscription::onSubscriptionCancel public function Acts on a subscription after it has been canceled. Overrides SubscriptionTypeBase::onSubscriptionCancel
LicenseSubscription::onSubscriptionCreate public function Acts on a subscription after it has been created from an order item. Overrides SubscriptionTypeBase::onSubscriptionCreate
LicenseSubscription::onSubscriptionExpire public function Acts on a subscription after it has expired. Overrides SubscriptionTypeBase::onSubscriptionExpire
LicenseSubscription::onSubscriptionRenew public function Acts on a subscription after it has been renewed. Overrides SubscriptionTypeBase::onSubscriptionRenew
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
SubscriptionTypeBase::$entityTypeManager protected property The entity type manager.
SubscriptionTypeBase::adjustBillingPeriod protected function Adjusts the billing period to reflect the subscription start/end dates.
SubscriptionTypeBase::adjustTrialPeriod protected function Adjusts the trial period to reflect the trial end date.
SubscriptionTypeBase::collectCharges public function Collects charges for a subscription's billing period. Overrides SubscriptionTypeInterface::collectCharges
SubscriptionTypeBase::collectTrialCharges public function Collects charges for a subscription's trial period. Overrides SubscriptionTypeInterface::collectTrialCharges
SubscriptionTypeBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
SubscriptionTypeBase::getLabel public function Gets the subscription type label. Overrides SubscriptionTypeInterface::getLabel
SubscriptionTypeBase::getPurchasableEntityTypeId public function Gets the subscription type's purchasable entity type ID. Overrides SubscriptionTypeInterface::getPurchasableEntityTypeId
SubscriptionTypeBase::onSubscriptionTrialCancel public function Acts on a subscription after a trial has been canceled. Overrides SubscriptionTypeInterface::onSubscriptionTrialCancel
SubscriptionTypeBase::onSubscriptionTrialStart public function Acts on a subscription after a trial has been started. Overrides SubscriptionTypeInterface::onSubscriptionTrialStart
SubscriptionTypeBase::__construct public function Constructs a new SubscriptionTypeBase object. Overrides PluginBase::__construct