You are here

class StockServiceManager in Commerce Stock 8

The stock service manager.

Responsible for handling services and transactions.

@package Drupal\commerce_stock

Hierarchy

Expanded class hierarchy of StockServiceManager

See also

StockAvailabilityChecker.

6 files declare their use of StockServiceManager
OrderEventsTransactionsTest.php in modules/local_storage/tests/src/Kernel/OrderEventsTransactionsTest.php
SimpleStockLevelFormatter.php in modules/field/src/Plugin/Field/FieldFormatter/SimpleStockLevelFormatter.php
SimpleStockLevelWidget.php in modules/field/src/Plugin/Field/FieldWidget/SimpleStockLevelWidget.php
StockLevelWidgetBase.php in modules/field/src/Plugin/Field/FieldWidget/StockLevelWidgetBase.php
StockServiceManagerTest.php in tests/src/Kernel/StockServiceManagerTest.php

... See full list

1 string reference to 'StockServiceManager'
commerce_stock.services.yml in ./commerce_stock.services.yml
commerce_stock.services.yml
1 service uses StockServiceManager
commerce_stock.service_manager in ./commerce_stock.services.yml
Drupal\commerce_stock\StockServiceManager

File

src/StockServiceManager.php, line 20

Namespace

Drupal\commerce_stock
View source
class StockServiceManager implements StockServiceManagerInterface, StockTransactionsInterface {
  use ContextCreatorTrait;

  /**
   * The stock services.
   *
   * @var \Drupal\commerce_stock\StockServiceInterface[]
   */
  protected $stockServices = [];

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The current store.
   *
   * @var \Drupal\commerce_store\CurrentStoreInterface
   */
  protected $currentStore;

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

  /**
   * Constructs a StockServiceManager object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\commerce_store\CurrentStoreInterface $current_store
   *   The current store.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   */
  public function __construct(ConfigFactoryInterface $config_factory, CurrentStoreInterface $current_store, AccountInterface $current_user) {
    $this->configFactory = $config_factory;
    $this->currentStore = $current_store;
    $this->currentUser = $current_user;
  }

  /**
   * {@inheritdoc}
   */
  public function addService(StockServiceInterface $stock_service) {
    $this->stockServices[$stock_service
      ->getId()] = $stock_service;
  }

  /**
   * {@inheritdoc}
   */
  public function getService(PurchasableEntityInterface $entity) {
    $config = $this->configFactory
      ->get('commerce_stock.service_manager');
    $default_service_id = $config
      ->get('default_service_id');
    $entity_type = $entity
      ->getEntityTypeId();
    $entity_bundle = $entity
      ->bundle();
    $entity_config_key = $entity_type . '_' . $entity_bundle . '_service_id';
    $entity_service_id = $config
      ->get($entity_config_key);
    $service_id = $entity_service_id ?: $default_service_id;
    return $this->stockServices[$service_id];
  }

  /**
   * {@inheritdoc}
   */
  public function listServices() {
    return $this->stockServices;
  }

  /**
   * {@inheritdoc}
   */
  public function listServiceIds() {
    $ids = [];
    foreach ($this->stockServices as $service) {
      $ids[$service
        ->getId()] = $service
        ->getName();
    }
    return $ids;
  }

  /**
   * {@inheritdoc}
   *
   * @todo code sniffer error here, can't have optional params first.
   */
  public function getTransactionLocation(Context $context = NULL, PurchasableEntityInterface $entity, $quantity) {
    $stock_config = $this
      ->getService($entity)
      ->getConfiguration();
    return $stock_config
      ->getTransactionLocation($context, $entity, $quantity);
  }

  /**
   * {@inheritdoc}
   */
  public function createTransaction(PurchasableEntityInterface $entity, $location_id, $zone, $quantity, $unit_cost, $currency_code, $transaction_type_id, array $metadata = []) {
    $stock_updater = $this
      ->getService($entity)
      ->getStockUpdater();
    $stock_updater
      ->createTransaction($entity, $location_id, $zone, $quantity, $unit_cost, $currency_code, $transaction_type_id, $metadata);
  }

  /**
   * {@inheritdoc}
   */
  public function receiveStock(PurchasableEntityInterface $entity, $location_id, $zone, $quantity, $unit_cost, $currency_code, $message = NULL) {
    $transaction_type_id = StockTransactionsInterface::NEW_STOCK;
    if (is_null($message)) {
      $metadata = [];
    }
    else {
      $metadata = [
        'data' => [
          'message' => $message,
        ],
      ];
    }

    // Make sure quantity is positive.
    $quantity = abs($quantity);
    $stock_updater = $this
      ->getService($entity)
      ->getStockUpdater();
    $stock_updater
      ->createTransaction($entity, $location_id, $zone, $quantity, $unit_cost, $currency_code, $transaction_type_id, $metadata);
  }

  /**
   * {@inheritdoc}
   */
  public function sellStock(PurchasableEntityInterface $entity, $location_id, $zone, $quantity, $unit_cost, $currency_code, $order_id, $user_id, $message = NULL) {
    $transaction_type_id = StockTransactionsInterface::STOCK_SALE;
    $metadata = [
      'related_oid' => $order_id,
      'related_uid' => $user_id,
    ];
    if (!is_null($message)) {
      $metadata['data']['message'] = $message;
    }

    // Make sure quantity is positive.
    $quantity = -1 * abs($quantity);
    $stock_updater = $this
      ->getService($entity)
      ->getStockUpdater();
    $stock_updater
      ->createTransaction($entity, $location_id, $zone, $quantity, $unit_cost, $currency_code, $transaction_type_id, $metadata);
  }

  /**
   * {@inheritdoc}
   */
  public function moveStock(PurchasableEntityInterface $entity, $from_location_id, $to_location_id, $from_zone, $to_zone, $quantity, $unit_cost, $currency_code, $message = NULL) {
    if (is_null($message)) {
      $metadata = [];
    }
    else {
      $metadata = [
        'data' => [
          'message' => $message,
        ],
      ];
    }

    // Make sure quantity is positive.
    $quantity_from = -1 * abs($quantity);
    $quantity_to = abs($quantity);
    $stock_updater = $this
      ->getService($entity)
      ->getStockUpdater();
    $tid = $stock_updater
      ->createTransaction($entity, $from_location_id, $from_zone, $quantity_from, $unit_cost, $currency_code, StockTransactionsInterface::MOVEMENT_FROM, $metadata);

    // The second transaction will point to the first one.
    $metadata['related_tid'] = $tid;
    $stock_updater
      ->createTransaction($entity, $to_location_id, $to_zone, $quantity_to, $unit_cost, $currency_code, StockTransactionsInterface::MOVEMENT_TO, $metadata);
  }

  /**
   * {@inheritdoc}
   */
  public function returnStock(PurchasableEntityInterface $entity, $location_id, $zone, $quantity, $unit_cost, $currency_code, $order_id, $user_id, $message = NULL) {
    $transaction_type_id = StockTransactionsInterface::STOCK_RETURN;
    $metadata = [
      'related_oid' => $order_id,
      'related_uid' => $user_id,
    ];
    if (!is_null($message)) {
      $metadata['data']['message'] = $message;
    }

    // Make sure quantity is positive.
    $quantity = abs($quantity);
    $stock_updater = $this
      ->getService($entity)
      ->getStockUpdater();
    $stock_updater
      ->createTransaction($entity, $location_id, $zone, $quantity, $unit_cost, $currency_code, $transaction_type_id, $metadata);
  }

  /**
   * Gets the total stock level for a given purchasable entity.
   *
   * @todo - we should make the method more obscure as it does not support
   * the context. Only useful for single store sites.
   *
   * @param \Drupal\commerce\PurchasableEntityInterface $entity
   *   The purchasable entity to get the stock level for.
   *
   * @throws \Exception
   *
   * @return int
   *   The stock level.
   */
  public function getStockLevel(PurchasableEntityInterface $entity) {
    if ($entity
      ->isNew()) {
      return 0;
    }
    $stock_config = $this
      ->getService($entity)
      ->getConfiguration();
    $stock_checker = $this
      ->getService($entity)
      ->getStockChecker();
    $locations = $stock_config
      ->getAvailabilityLocations($this
      ->getContext($entity), $entity);
    return $stock_checker
      ->getTotalStockLevel($entity, $locations);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContextCreatorTrait::createContextFromOrder public static function Creates a commerce context object.
ContextCreatorTrait::getContext public function Returns the active commerce context.
ContextCreatorTrait::getContextDetails private function Get context details.
ContextCreatorTrait::isValidContext public function Checks that the context returned is valid for $entity.
StockServiceManager::$configFactory protected property The config factory.
StockServiceManager::$currentStore protected property The current store.
StockServiceManager::$currentUser protected property The current user.
StockServiceManager::$stockServices protected property The stock services.
StockServiceManager::addService public function Adds a Stock service. Overrides StockServiceManagerInterface::addService
StockServiceManager::createTransaction public function Create a transaction. Overrides StockTransactionsInterface::createTransaction
StockServiceManager::getService public function Get a service relevant for the entity. Overrides StockServiceManagerInterface::getService
StockServiceManager::getStockLevel public function Gets the total stock level for a given purchasable entity.
StockServiceManager::getTransactionLocation public function @todo code sniffer error here, can't have optional params first. Overrides StockTransactionsInterface::getTransactionLocation
StockServiceManager::listServiceIds public function Returns an array of the IDs of all registered stock services. Overrides StockServiceManagerInterface::listServiceIds
StockServiceManager::listServices public function Returns an array of all registered stock services. Overrides StockServiceManagerInterface::listServices
StockServiceManager::moveStock public function Move stock. Overrides StockTransactionsInterface::moveStock
StockServiceManager::receiveStock public function Receive stock. Overrides StockTransactionsInterface::receiveStock
StockServiceManager::returnStock public function Stock returns. Overrides StockTransactionsInterface::returnStock
StockServiceManager::sellStock public function Sell stock. Overrides StockTransactionsInterface::sellStock
StockServiceManager::__construct public function Constructs a StockServiceManager object.
StockTransactionsInterface::MOVEMENT_FROM constant
StockTransactionsInterface::MOVEMENT_TO constant
StockTransactionsInterface::NEW_STOCK constant
StockTransactionsInterface::STOCK_IN constant
StockTransactionsInterface::STOCK_OUT constant
StockTransactionsInterface::STOCK_RETURN constant
StockTransactionsInterface::STOCK_SALE constant