You are here

public function LicenseAvailabilityCheckerExistingRights::applies in Commerce License 8.2

Determines whether the checker applies to the given order item.

Parameters

\Drupal\commerce_order\Entity\OrderItemInterface $order_item: The order item.

Return value

bool TRUE if the checker applies to the given order item, FALSE otherwise.

Overrides AvailabilityCheckerInterface::applies

File

src/LicenseAvailabilityCheckerExistingRights.php, line 64

Class

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

Namespace

Drupal\commerce_license

Code

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;
}