You are here

class AvailabilityManager in Commerce Core 8.2

Same name in this branch
  1. 8.2 src/AvailabilityManager.php \Drupal\commerce\AvailabilityManager
  2. 8.2 modules/order/src/AvailabilityManager.php \Drupal\commerce_order\AvailabilityManager

Default implementation of the availability manager.

Hierarchy

Expanded class hierarchy of AvailabilityManager

1 file declares its use of AvailabilityManager
AvailabilityManagerTest.php in modules/order/tests/src/Unit/AvailabilityManagerTest.php
1 string reference to 'AvailabilityManager'
commerce_order.services.yml in modules/order/commerce_order.services.yml
modules/order/commerce_order.services.yml
1 service uses AvailabilityManager
commerce_order.availability_manager in modules/order/commerce_order.services.yml
Drupal\commerce_order\AvailabilityManager

File

modules/order/src/AvailabilityManager.php, line 12

Namespace

Drupal\commerce_order
View source
class AvailabilityManager implements AvailabilityManagerInterface {

  /**
   * The checkers.
   *
   * @var \Drupal\commerce_order\AvailabilityCheckerInterface[]
   */
  protected $checkers = [];

  /**
   * The "legacy" checkers.
   *
   * @var \Drupal\commerce\AvailabilityCheckerInterface[]
   */
  protected $legacyCheckers = [];

  /**
   * {@inheritdoc}
   */
  public function addChecker(AvailabilityCheckerInterface $checker) {
    $this->checkers[] = $checker;
  }

  /**
   * {@inheritdoc}
   */
  public function addLegacyChecker(LegacyAvailabilityCheckerInterface $checker) {
    $this->legacyCheckers[] = $checker;
  }

  /**
   * {@inheritdoc}
   */
  public function check(OrderItemInterface $order_item, Context $context) : AvailabilityResult {
    foreach ($this->checkers as $checker) {
      if (!$checker
        ->applies($order_item)) {
        continue;
      }
      $result = $checker
        ->check($order_item, $context);
      if ($result instanceof AvailabilityResult && $result
        ->isUnavailable()) {
        return $result;
      }
    }

    // Invoke the legacy checkers next, and wrap the return value into our
    // new AvailabilityResult value object.
    $purchased_entity = $order_item
      ->getPurchasedEntity();
    $quantity = $order_item
      ->getQuantity();
    foreach ($this->legacyCheckers as $checker) {
      @trigger_error(get_class($checker) . ' implements \\Drupal\\commerce\\AvailabilityCheckerInterface which is deprecated in commerce:8.x-2.18 and is removed from commerce:3.x. use \\Drupal\\commerce_order\\AvailabilityCheckerInterface instead.', E_USER_DEPRECATED);
      if (!$checker
        ->applies($purchased_entity)) {
        continue;
      }
      $result = $checker
        ->check($purchased_entity, $quantity, $context);
      if ($result === FALSE) {
        return AvailabilityResult::unavailable();
      }
    }
    return AvailabilityResult::neutral();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AvailabilityManager::$checkers protected property The checkers.
AvailabilityManager::$legacyCheckers protected property The "legacy" checkers.
AvailabilityManager::addChecker public function Adds a checker. Overrides AvailabilityManagerInterface::addChecker
AvailabilityManager::addLegacyChecker public function Adds a "legacy" (i.e "deprecated") checker. Overrides AvailabilityManagerInterface::addLegacyChecker
AvailabilityManager::check public function Checks the availability of the given order item. Overrides AvailabilityManagerInterface::check