You are here

class LicenseAvailabilityCheckerExistingRights in Commerce License 8.2

Prevents purchase of a license that grants rights the user already has.

This does not check existing licenses, but checks the granted features directly. For example, for a role license, this checks whether the user has the role the license grants, rather than whether they have a license for that role.

Using an availability checker rather than an order processor, even though they currently ultimately do the same thing (as availability checkers are processed by AvailabilityOrderProcessor, which is itself an order processor), because eventually availability checkers should deal with hiding the 'add to cart' form -- see https://www.drupal.org/node/2710107.

Hierarchy

Expanded class hierarchy of LicenseAvailabilityCheckerExistingRights

See also

\Drupal\commerce_license\LicenseOrderProcessorMultiples

1 string reference to 'LicenseAvailabilityCheckerExistingRights'
commerce_license.services.yml in ./commerce_license.services.yml
commerce_license.services.yml
1 service uses LicenseAvailabilityCheckerExistingRights
commerce_license.license_availability_checker_existing in ./commerce_license.services.yml
Drupal\commerce_license\LicenseAvailabilityCheckerExistingRights

File

src/LicenseAvailabilityCheckerExistingRights.php, line 29

Namespace

Drupal\commerce_license
View source
class LicenseAvailabilityCheckerExistingRights implements AvailabilityCheckerInterface {

  /**
   * The current active user.
   *
   * @var \Drupal\Core\Session\AccountProxyInterface
   */
  protected $currentUser;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a new LicenseAvailabilityCheckerExistingRights object.
   *
   * @param \Drupal\Core\Session\AccountProxyInterface $current_user
   *   The current active user.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(AccountProxyInterface $current_user, EntityTypeManagerInterface $entity_type_manager) {
    $this->currentUser = $current_user;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function applies(OrderItemInterface $order_item) {
    $purchased_entity = $order_item
      ->getPurchasedEntity();

    // This applies only to product variations which have our license trait on
    // them. Check for the field the trait provides, as checking for the trait
    // on the bundle is expensive -- see https://www.drupal.org/node/2894805.
    if (!$purchased_entity
      ->hasField('license_type') || $purchased_entity
      ->get('license_type')
      ->isEmpty()) {
      return FALSE;
    }

    // Don't do an availability check on recurring orders.
    if ($order_item
      ->getOrder() && $order_item
      ->getOrder()
      ->bundle() === 'recurring') {
      return FALSE;
    }

    // This applies only to license types that implement the interface.
    $license_type_plugin = $purchased_entity->license_type
      ->first()
      ->getTargetInstance();
    if ($license_type_plugin instanceof ExistingRightsFromConfigurationCheckingInterface) {
      return TRUE;
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function check(OrderItemInterface $order_item, Context $context) {

    // Hand over to the license type plugin configured in the product variation,
    // to let it determine whether the user already has what the license would
    // grant.
    $customer = $context
      ->getCustomer();
    $purchased_entity = $order_item
      ->getPurchasedEntity();
    $license_type_plugin = $purchased_entity->license_type
      ->first()
      ->getTargetInstance();

    // Load the full user entity for the plugin.
    $user = $this->entityTypeManager
      ->getStorage('user')
      ->load($customer
      ->id());
    $existing_rights_result = $license_type_plugin
      ->checkUserHasExistingRights($user);
    if (!$existing_rights_result
      ->hasExistingRights()) {
      return AvailabilityResult::neutral();
    }

    // Show a message that includes the reason from the rights check.
    if ($user
      ->id() == $this->currentUser
      ->id()) {
      $rights_check_message = $existing_rights_result
        ->getOwnerUserMessage();
    }
    else {
      $rights_check_message = $existing_rights_result
        ->getOtherUserMessage();
    }
    $message = $rights_check_message . ' ' . t("You may not purchase the @product-label product.", [
      '@product-label' => $purchased_entity
        ->label(),
    ]);
    return AvailabilityResult::unavailable($message);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LicenseAvailabilityCheckerExistingRights::$currentUser protected property The current active user.
LicenseAvailabilityCheckerExistingRights::$entityTypeManager protected property The entity type manager.
LicenseAvailabilityCheckerExistingRights::applies public function Determines whether the checker applies to the given order item. Overrides AvailabilityCheckerInterface::applies
LicenseAvailabilityCheckerExistingRights::check public function Checks the availability of the given order item. Overrides AvailabilityCheckerInterface::check
LicenseAvailabilityCheckerExistingRights::__construct public function Constructs a new LicenseAvailabilityCheckerExistingRights object.