Commit 94817506 authored by Jonathan Sacksick's avatar Jonathan Sacksick Committed by Jonathan Sacksick
Browse files

Issue #3085785 by a.dmitriiev, jsacksick, andypost: Generate order reports...

Issue #3085785 by a.dmitriiev, jsacksick, andypost: Generate order reports from a destructable service and fix several phpcs violations.
parent 2efb9e25
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
<?php

/**
 * @file
 * Provides reporting capabilities for Commerce.
 */

use Drupal\commerce_reports\Plugin\views\field\PriceNumericField;
use Drupal\Core\Database\Query\AlterableInterface;
use Drupal\views\Plugin\views\field\NumericField;
@@ -81,7 +86,7 @@ function commerce_reports_views_pre_build(ViewExecutable $view) {
      // Determine if this is a price field being aggregated.
      $intersect_test = array_intersect([
        $field_plugin->field . '_number',
        $field_plugin->field . '_currency_code'
        $field_plugin->field . '_currency_code',
      ], $field_plugin->additional_fields);

      if (!empty($intersect_test)) {
+1 −1
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@ function commerce_reports_post_update_add_permission_to_purchased_items_report(&
  $access = [
    'type' => 'perm',
    'options' => [
      'perm' => 'access commerce reports'
      'perm' => 'access commerce reports',
    ],
  ];
  $view->set('display.default.display_options.access', $access);
+2 −1
Original line number Diff line number Diff line
services:
  commerce_reports.order_placed_subscriber:
    class: Drupal\commerce_reports\EventSubscriber\OrderPlacedEventSubscriber
    arguments: ['@state', '@commerce_reports.order_report_generator']
    arguments: ['@commerce_reports.order_report_generator']
    tags:
      - { name: event_subscriber }
      - { name: needs_destruction }

  commerce_reports.query_builder:
    class: Drupal\commerce_reports\ReportQueryBuilder
+18 −36
Original line number Diff line number Diff line
@@ -3,90 +3,72 @@
namespace Drupal\commerce_reports\EventSubscriber;

use Drupal\commerce_reports\OrderReportGeneratorInterface;
use Drupal\Core\State\StateInterface;
use Drupal\Core\DestructableInterface;
use Drupal\state_machine\Event\WorkflowTransitionEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
 * Event subscriber to order placed transition event.
 */
class OrderPlacedEventSubscriber implements EventSubscriberInterface {
class OrderPlacedEventSubscriber implements EventSubscriberInterface, DestructableInterface {

  /**
   * The state key/value store.
   * The order report generator.
   *
   * @var \Drupal\Core\State\StateInterface
   * @var \Drupal\commerce_reports\OrderReportGeneratorInterface
   */
  protected $state;
  protected $orderReportGenerator;

  /**
   * The order report generator.
   * Static cache of order IDS that were placed during this request.
   *
   * @var \Drupal\commerce_reports\OrderReportGeneratorInterface
   * @var array
   */
  protected $orderReportGenerator;
  protected $orderIds = [];

  /**
   * 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;
  public function __construct(OrderReportGeneratorInterface $order_report_generator) {
    $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) {
  public function onOrderPlace(WorkflowTransitionEvent $event) {
    $order = $event->getEntity();
    $existing = $this->state->get('commerce_order_reports', []);
    $existing[] = $order->id();
    $this->state->set('commerce_order_reports', $existing);
    $this->orderIds[$order->id()] = $order->id();
  }

  /**
   * Generates order reports once output flushed.
   * Generates order reports on destruct.
   *
   * 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', []);
  public function destruct() {
    if (!empty($this->orderIds)) {
      $this->orderReportGenerator->generateReports($this->orderIds);
    }
  }

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

}
+6 −6
Original line number Diff line number Diff line
@@ -5,11 +5,10 @@ namespace Drupal\commerce_reports\Form;
use Drupal\commerce_reports\ReportTypeManager;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Profides a form for bulk generating order reports.
 * Provides a form for bulk generating order reports.
 */
class OrderReportGenerateForm extends FormBase {

@@ -82,7 +81,6 @@ class OrderReportGenerateForm extends FormBase {
    return $form;
  }


  /**
   * {@inheritdoc}
   */
@@ -107,10 +105,12 @@ class OrderReportGenerateForm extends FormBase {
  /**
   * Processes the batch and generates the order reports.
   *
   * @param string $plugin_id
   *   The plugin ID.
   * @param array $context
   *   The batch context information.
   */
  public static function processBatch($plugin_id, array &$context) {
  public static function processBatch(string $plugin_id, array &$context) {
    $order_storage = \Drupal::entityTypeManager()->getStorage('commerce_order');

    // Initialization.
Loading