You are here

class CoreStockEvents in Commerce Stock 8

Core Stock Events.

Plugin annotation


@StockEvents(
  id = "core_stock_events",
  description = @Translation("Core stock Events."),
)

Hierarchy

Expanded class hierarchy of CoreStockEvents

1 file declares its use of CoreStockEvents
OrderEventSubscriber.php in src/EventSubscriber/OrderEventSubscriber.php

File

src/Plugin/StockEvents/CoreStockEvents.php, line 19

Namespace

Drupal\commerce_stock\Plugin\StockEvents
View source
class CoreStockEvents extends CoreStockEventsBase {

  /**
   * {@inheritdoc}
   */
  public function stockEvent(Context $context, PurchasableEntityInterface $entity, $stockEvent, $quantity, StockLocationInterface $location, $transaction_type, array $metadata) {
    $config = \Drupal::configFactory()
      ->get('commerce_stock.core_stock_events');

    // Check if event should be handled.
    $order_placed_events = [
      StockEventsInterface::ORDER_PLACE_EVENT,
    ];
    $order_update_events = [
      StockEventsInterface::ORDER_UPDATE_EVENT,
      StockEventsInterface::ORDER_ITEM_UPDATE_EVENT,
    ];
    $order_delete_events = [
      StockEventsInterface::ORDER_CANCEL_EVENT,
      StockEventsInterface::ORDER_DELET_EVENT,
      StockEventsInterface::ORDER_ITEM_DELETE_EVENT,
    ];

    // Cancel if event type is not enabled.
    if (in_array($stockEvent, $order_placed_events) && !$config
      ->get('core_stock_events_order_complete')) {
      return FALSE;
    }
    elseif (in_array($stockEvent, $order_update_events) && !$config
      ->get('core_stock_events_order_updates')) {
      return FALSE;
    }
    elseif (in_array($stockEvent, $order_delete_events) && !$config
      ->get('core_stock_events_order_cancel')) {
      return FALSE;
    }

    // Get the stock service.
    $stockService = \Drupal::service('commerce_stock.service_manager')
      ->getService($entity);

    // Use the stock updater to create the transaction.
    $transaction_id = $stockService
      ->getStockUpdater()
      ->createTransaction($entity, $location
      ->getId(), '', $quantity, NULL, $currency_code = NULL, $transaction_type, $metadata);

    // Return the transaction ID.
    return $transaction_id;
  }

  /**
   * {@inheritdoc}
   */
  public function configFormOptions() {
    $config = \Drupal::configFactory()
      ->get('commerce_stock.core_stock_events');
    $form_options['core_stock_events_order_complete'] = [
      '#type' => 'checkbox',
      '#title' => t('Reserve stock on order complete'),
      '#default_value' => $config
        ->get('core_stock_events_order_complete'),
    ];
    $form_options['core_stock_events_order_cancel'] = [
      '#type' => 'checkbox',
      '#title' => t('Automatically return stock on cancel'),
      '#default_value' => $config
        ->get('core_stock_events_order_cancel'),
    ];
    $form_options['core_stock_events_order_updates'] = [
      '#type' => 'checkbox',
      '#title' => t('Adjust stock on order updates (after the order was completed)'),
      '#default_value' => $config
        ->get('core_stock_events_order_updates'),
    ];
    return $form_options;
  }

  /**
   * {@inheritdoc}
   */
  public function saveConfigFormOptions(array $form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    $config = \Drupal::configFactory()
      ->getEditable('commerce_stock.core_stock_events');
    $config
      ->set('core_stock_events_order_complete', $values['core_stock_events_order_complete']);
    $config
      ->set('core_stock_events_order_cancel', $values['core_stock_events_order_cancel']);
    $config
      ->set('core_stock_events_order_updates', $values['core_stock_events_order_updates']);
    $config
      ->save();
  }

  /**
   * To ensure backwards compatibility we introduced StockEventTypes plugins
   * without changing the StockEventsInterface. This functions maps the
   * interface constants to StockEventTypes.
   *
   * @param int $event_type_id
   *   The StockEventsInterface interface constant.
   *
   * @return string
   *   The StockEventType id or FALSE if not exists.
   */
  public static function mapStockEventTypes($event_type_id) {
    $map = self::getEventTypeMap();
    $result = array_key_exists($event_type_id, $map) ? $map[$event_type_id] : FALSE;
    return $result;
  }

  /**
   * To ensure backwards compatibility we introduced StockEventTypes plugins
   * without changing the StockEventsInterface. This functions maps the
   * interface constants to StockEventTypes.
   *
   * @param string $stock_event_type_id
   *   The StockEventType id.
   *
   * @return int
   *   The StockEventsInterface interface constant or FALSE if it not exists.
   */
  public static function mapStockEventIds($stock_event_type_id) {
    $map = array_flip(self::getEventTypeMap());
    $result = array_key_exists($stock_event_type_id, $map) ? $map[$stock_event_type_id] : FALSE;
    return $result;
  }

  /**
   * Get the map of StockEvenTypes.
   */
  private static function getEventTypeMap() {
    return $map = [
      StockEventsInterface::ORDER_PLACE_EVENT => 'commerce_stock_order_place',
      StockEventsInterface::ORDER_UPDATE_EVENT => 'commerce_stock_order_update',
      StockEventsInterface::ORDER_CANCEL_EVENT => 'commerce_stock_order_cancel',
      StockEventsInterface::ORDER_DELET_EVENT => 'commerce_stock_order_delete',
      StockEventsInterface::ORDER_ITEM_DELETE_EVENT => 'commerce_stock_order_item_delete',
      StockEventsInterface::ORDER_ITEM_UPDATE_EVENT => 'commerce_stock_order_item_update',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CoreStockEvents::configFormOptions public function Return form elements holding the configuration options. Overrides StockEventsInterface::configFormOptions
CoreStockEvents::getEventTypeMap private static function Get the map of StockEvenTypes.
CoreStockEvents::mapStockEventIds public static function To ensure backwards compatibility we introduced StockEventTypes plugins without changing the StockEventsInterface. This functions maps the interface constants to StockEventTypes.
CoreStockEvents::mapStockEventTypes public static function To ensure backwards compatibility we introduced StockEventTypes plugins without changing the StockEventsInterface. This functions maps the interface constants to StockEventTypes.
CoreStockEvents::saveConfigFormOptions public function Save the configuration options. Overrides StockEventsInterface::saveConfigFormOptions
CoreStockEvents::stockEvent public function A stock event with transaction details. Overrides StockEventsInterface::stockEvent
CoreStockEventsBase::getMetadata protected function Helper method for building the transaction metadata.
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.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
StockEventsInterface::ORDER_CANCEL_EVENT constant
StockEventsInterface::ORDER_DELET_EVENT constant
StockEventsInterface::ORDER_ITEM_DELETE_EVENT constant
StockEventsInterface::ORDER_ITEM_UPDATE_EVENT constant
StockEventsInterface::ORDER_PLACE_EVENT constant
StockEventsInterface::ORDER_UPDATE_EVENT constant