You are here

class OrderEventSubscriber in Commerce Core 8.2

Same name in this branch
  1. 8.2 modules/log/src/EventSubscriber/OrderEventSubscriber.php \Drupal\commerce_log\EventSubscriber\OrderEventSubscriber
  2. 8.2 modules/cart/src/EventSubscriber/OrderEventSubscriber.php \Drupal\commerce_cart\EventSubscriber\OrderEventSubscriber
  3. 8.2 modules/promotion/src/EventSubscriber/OrderEventSubscriber.php \Drupal\commerce_promotion\EventSubscriber\OrderEventSubscriber

Hierarchy

  • class \Drupal\commerce_log\EventSubscriber\OrderEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of OrderEventSubscriber

File

modules/log/src/EventSubscriber/OrderEventSubscriber.php, line 10

Namespace

Drupal\commerce_log\EventSubscriber
View source
class OrderEventSubscriber implements EventSubscriberInterface {

  /**
   * The log storage.
   *
   * @var \Drupal\commerce_log\LogStorageInterface
   */
  protected $logStorage;

  /**
   * Constructs a new OrderEventSubscriber object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->logStorage = $entity_type_manager
      ->getStorage('commerce_log');
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      'commerce_order.order.assign' => [
        'onOrderAssign',
        -100,
      ],
      'commerce_order.post_transition' => [
        'onOrderPostTransition',
      ],
    ];
  }

  /**
   * Creates a log when an order is assigned.
   *
   * @param \Drupal\commerce_order\Event\OrderAssignEvent $event
   *   The order assign event.
   */
  public function onOrderAssign(OrderAssignEvent $event) {
    $order = $event
      ->getOrder();
    $this->logStorage
      ->generate($order, 'order_assigned', [
      'user' => $event
        ->getCustomer()
        ->getDisplayName(),
    ])
      ->save();
  }

  /**
   * Creates a log on order state update.
   *
   * @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
   *   The transition event.
   */
  public function onOrderPostTransition(WorkflowTransitionEvent $event) {
    $transition = $event
      ->getTransition();

    /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
    $order = $event
      ->getEntity();
    $original_state_id = $order
      ->getState()
      ->getOriginalId();
    $original_state = $event
      ->getWorkflow()
      ->getState($original_state_id);
    $this->logStorage
      ->generate($order, 'order_state_updated', [
      'transition_label' => $transition
        ->getLabel(),
      'from_state' => $original_state ? $original_state
        ->getLabel() : $original_state_id,
      'to_state' => $order
        ->getState()
        ->getLabel(),
    ])
      ->save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OrderEventSubscriber::$logStorage protected property The log storage.
OrderEventSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
OrderEventSubscriber::onOrderAssign public function Creates a log when an order is assigned.
OrderEventSubscriber::onOrderPostTransition public function Creates a log on order state update.
OrderEventSubscriber::__construct public function Constructs a new OrderEventSubscriber object.