You are here

class OrderPlacedEventSubscriber in Commerce Reporting 8

Event subscriber to order placed transition event.

Hierarchy

  • class \Drupal\commerce_reports\EventSubscriber\OrderPlacedEventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of OrderPlacedEventSubscriber

1 string reference to 'OrderPlacedEventSubscriber'
commerce_reports.services.yml in ./commerce_reports.services.yml
commerce_reports.services.yml
1 service uses OrderPlacedEventSubscriber
commerce_reports.order_placed_subscriber in ./commerce_reports.services.yml
Drupal\commerce_reports\EventSubscriber\OrderPlacedEventSubscriber

File

src/EventSubscriber/OrderPlacedEventSubscriber.php, line 15

Namespace

Drupal\commerce_reports\EventSubscriber
View source
class OrderPlacedEventSubscriber implements EventSubscriberInterface {

  /**
   * The state key/value store.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * The order report generator.
   *
   * @var \Drupal\commerce_reports\OrderReportGeneratorInterface
   */
  protected $orderReportGenerator;

  /**
   * Constructs a new OrderPlacedEventSubscriber object.
   *
   * @param \Drupal\Core\State\StateInterface $state
   *   The state key/value store.
   * @param \Drupal\commerce_reports\OrderReportGeneratorInterface $order_report_generator
   *   The order report generator.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   */
  public function __construct(StateInterface $state, OrderReportGeneratorInterface $order_report_generator) {
    $this->state = $state;
    $this->orderReportGenerator = $order_report_generator;
  }

  /**
   * Flags the order to have a report generated.
   *
   * @todo come up with better flagging.
   *
   * @param \Drupal\state_machine\Event\WorkflowTransitionEvent $event
   *   The workflow transition event.
   */
  public function flagOrder(WorkflowTransitionEvent $event) {
    $order = $event
      ->getEntity();
    $existing = $this->state
      ->get('commerce_order_reports', []);
    $existing[] = $order
      ->id();
    $this->state
      ->set('commerce_order_reports', $existing);
  }

  /**
   * Generates order reports once output flushed.
   *
   * This creates the base order report populated with the bundle plugin ID,
   * order ID, and created timestamp from when the order was placed. Each
   * plugin then sets its values.
   *
   * @param \Symfony\Component\HttpKernel\Event\PostResponseEvent $event
   *   The post response event.
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  public function generateReports(PostResponseEvent $event) {
    $order_ids = $this->state
      ->get('commerce_order_reports', []);
    $this->orderReportGenerator
      ->generateReports($order_ids);

    // @todo this could lose data, possibly as its global state.
    $this->state
      ->set('commerce_order_reports', []);
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = [
      'commerce_order.place.pre_transition' => 'flagOrder',
      KernelEvents::TERMINATE => 'generateReports',
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OrderPlacedEventSubscriber::$orderReportGenerator protected property The order report generator.
OrderPlacedEventSubscriber::$state protected property The state key/value store.
OrderPlacedEventSubscriber::flagOrder public function Flags the order to have a report generated.
OrderPlacedEventSubscriber::generateReports public function Generates order reports once output flushed.
OrderPlacedEventSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
OrderPlacedEventSubscriber::__construct public function Constructs a new OrderPlacedEventSubscriber object.