You are here

class TimestampEventSubscriber in Commerce Core 8.2

Hierarchy

  • class \Drupal\commerce_order\EventSubscriber\TimestampEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of TimestampEventSubscriber

1 string reference to 'TimestampEventSubscriber'
commerce_order.services.yml in modules/order/commerce_order.services.yml
modules/order/commerce_order.services.yml
1 service uses TimestampEventSubscriber
commerce_order.timestamp_event_subscriber in modules/order/commerce_order.services.yml
Drupal\commerce_order\EventSubscriber\TimestampEventSubscriber

File

modules/order/src/EventSubscriber/TimestampEventSubscriber.php, line 9

Namespace

Drupal\commerce_order\EventSubscriber
View source
class TimestampEventSubscriber implements EventSubscriberInterface {

  /**
   * The time.
   *
   * @var \Drupal\Component\Datetime\TimeInterface
   */
  protected $time;

  /**
   * Constructs a new TimestampEventSubscriber object.
   *
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The time.
   */
  public function __construct(TimeInterface $time) {
    $this->time = $time;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = [
      'commerce_order.place.pre_transition' => 'onPlaceTransition',
      'commerce_order.pre_transition' => 'onAnyTransition',
    ];
    return $events;
  }

  /**
   * Sets the order's placed timestamp.
   *
   * @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
   *   The transition event.
   */
  public function onPlaceTransition(WorkflowTransitionEvent $event) {

    /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
    $order = $event
      ->getEntity();
    if (empty($order
      ->getPlacedTime())) {
      $order
        ->setPlacedTime($this->time
        ->getRequestTime());
    }
  }

  /**
   * Sets the order's completed timestamp.
   *
   * @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
   *   The transition event.
   */
  public function onAnyTransition(WorkflowTransitionEvent $event) {

    /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
    $order = $event
      ->getEntity();
    $to_state_id = $event
      ->getTransition()
      ->getToState()
      ->getId();
    if ($to_state_id == 'completed' && empty($order
      ->getCompletedTime())) {
      $order
        ->setCompletedTime($this->time
        ->getRequestTime());
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TimestampEventSubscriber::$time protected property The time.
TimestampEventSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
TimestampEventSubscriber::onAnyTransition public function Sets the order's completed timestamp.
TimestampEventSubscriber::onPlaceTransition public function Sets the order's placed timestamp.
TimestampEventSubscriber::__construct public function Constructs a new TimestampEventSubscriber object.