From f4774627e0cda7c73344b1b54c0e0e2b4da3d52a Mon Sep 17 00:00:00 2001
From: mglaman <mglaman@2416470.no-reply.drupal.org>
Date: Thu, 30 Jun 2016 17:16:43 -0500
Subject: [PATCH] Issue #2758861 by mglaman: Provide an OrderTypeResolver

---
 modules/cart/src/Form/AddToCartForm.php            | 23 ++++++--
 modules/order/commerce_order.services.yml          | 11 ++++
 .../order/src/Resolver/ChainOrderTypeResolver.php  | 54 ++++++++++++++++++
 .../Resolver/ChainOrderTypeResolverInterface.php   | 29 ++++++++++
 .../src/Resolver/DefaultOrderTypeResolver.php      | 39 +++++++++++++
 .../src/Resolver/OrderTypeResolverInterface.php    | 24 ++++++++
 .../src/Kernel/ChainOrderTypeResolverTest.php      | 66 ++++++++++++++++++++++
 7 files changed, 240 insertions(+), 6 deletions(-)
 create mode 100644 modules/order/commerce_order.services.yml
 create mode 100644 modules/order/src/Resolver/ChainOrderTypeResolver.php
 create mode 100644 modules/order/src/Resolver/ChainOrderTypeResolverInterface.php
 create mode 100644 modules/order/src/Resolver/DefaultOrderTypeResolver.php
 create mode 100644 modules/order/src/Resolver/OrderTypeResolverInterface.php
 create mode 100644 modules/order/tests/src/Kernel/ChainOrderTypeResolverTest.php

diff --git a/modules/cart/src/Form/AddToCartForm.php b/modules/cart/src/Form/AddToCartForm.php
index 061386a..784033a 100644
--- a/modules/cart/src/Form/AddToCartForm.php
+++ b/modules/cart/src/Form/AddToCartForm.php
@@ -5,6 +5,7 @@
 use Drupal\commerce\PurchasableEntityInterface;
 use Drupal\commerce_cart\CartManagerInterface;
 use Drupal\commerce_cart\CartProviderInterface;
+use Drupal\commerce_order\Resolver\OrderTypeResolverInterface;
 use Drupal\commerce_store\StoreContextInterface;
 use Drupal\Core\Entity\EntityManagerInterface;
 use Drupal\Core\Entity\ContentEntityForm;
@@ -32,6 +33,13 @@ class AddToCartForm extends ContentEntityForm {
   protected $cartProvider;
 
   /**
+   * The order type resolver.
+   *
+   * @var \Drupal\commerce_order\Resolver\OrderTypeResolverInterface
+   */
+  protected $orderTypeResolver;
+
+  /**
    * The store context.
    *
    * @var \Drupal\commerce_store\StoreContextInterface
@@ -47,14 +55,17 @@ class AddToCartForm extends ContentEntityForm {
    *   The cart manager.
    * @param \Drupal\commerce_cart\CartProviderInterface $cart_provider
    *   The cart provider.
+   * @param \Drupal\commerce_order\Resolver\OrderTypeResolverInterface $order_type_resolver
+   *   The order type resolver.
    * @param \Drupal\commerce_store\StoreContextInterface $store_context
    *   The store context.
    */
-  public function __construct(EntityManagerInterface $entity_manager, CartManagerInterface $cart_manager, CartProviderInterface $cart_provider, StoreContextInterface $store_context) {
+  public function __construct(EntityManagerInterface $entity_manager, CartManagerInterface $cart_manager, CartProviderInterface $cart_provider, OrderTypeResolverInterface $order_type_resolver, StoreContextInterface $store_context) {
     parent::__construct($entity_manager);
 
     $this->cartManager = $cart_manager;
     $this->cartProvider = $cart_provider;
+    $this->orderTypeResolver = $order_type_resolver;
     $this->storeContext = $store_context;
   }
 
@@ -66,6 +77,7 @@ public static function create(ContainerInterface $container) {
       $container->get('entity.manager'),
       $container->get('commerce_cart.cart_manager'),
       $container->get('commerce_cart.cart_provider'),
+      $container->get('commerce_order.chain_order_type_resolver'),
       $container->get('commerce_store.store_context')
     );
   }
@@ -103,18 +115,17 @@ protected function actions(array $form, FormStateInterface $form_state) {
   public function submitForm(array &$form, FormStateInterface $form_state) {
     parent::submitForm($form, $form_state);
 
-    $line_item_type_storage = $this->entityManager->getStorage('commerce_line_item_type');
     /** @var \Drupal\commerce_order\Entity\LineItemInterface $line_item */
     $line_item = $this->entity;
     /** @var \Drupal\commerce\PurchasableEntityInterface $purchased_entity */
     $purchased_entity = $line_item->getPurchasedEntity();
-    /** @var \Drupal\commerce_order\Entity\LineItemTypeInterface $line_item_type */
-    $line_item_type = $line_item_type_storage->load($line_item->bundle());
+
+    $order_type = $this->orderTypeResolver->resolve($line_item);
 
     $store = $this->selectStore($purchased_entity);
-    $cart = $this->cartProvider->getCart($line_item_type->getOrderTypeId(), $store);
+    $cart = $this->cartProvider->getCart($order_type, $store);
     if (!$cart) {
-      $cart = $this->cartProvider->createCart('default', $store);
+      $cart = $this->cartProvider->createCart($order_type, $store);
     }
     $this->cartManager->addLineItem($cart, $line_item, $form_state->get(['settings', 'combine']));
 
diff --git a/modules/order/commerce_order.services.yml b/modules/order/commerce_order.services.yml
new file mode 100644
index 0000000..87bf6ee
--- /dev/null
+++ b/modules/order/commerce_order.services.yml
@@ -0,0 +1,11 @@
+services:
+  commerce_order.chain_order_type_resolver:
+    class: Drupal\commerce_order\Resolver\ChainOrderTypeResolver
+    tags:
+      - { name: service_collector, call: addResolver, tag: commerce_order.order_type_resolver }
+
+  commerce_order.default_order_type_resolver:
+    class: Drupal\commerce_order\Resolver\DefaultOrderTypeResolver
+    arguments: ['@entity_type.manager']
+    tags:
+      - { name: commerce_order.order_type_resolver, priority: 10 }
diff --git a/modules/order/src/Resolver/ChainOrderTypeResolver.php b/modules/order/src/Resolver/ChainOrderTypeResolver.php
new file mode 100644
index 0000000..08af037
--- /dev/null
+++ b/modules/order/src/Resolver/ChainOrderTypeResolver.php
@@ -0,0 +1,54 @@
+<?php
+
+namespace Drupal\commerce_order\Resolver;
+use Drupal\commerce_order\Entity\LineItemInterface;
+
+/**
+ * Default implementation of the chain order type resolver.
+ */
+class ChainOrderTypeResolver implements ChainOrderTypeResolverInterface {
+
+  /**
+   * The resolvers.
+   *
+   * @var \Drupal\commerce_order\Resolver\OrderTypeResolverInterface[]
+   */
+  protected $resolvers = [];
+
+  /**
+   * Constructs a new ChainOrderTypeResolver object.
+   *
+   * @param \Drupal\commerce_order\Resolver\OrderTypeResolverInterface[] $resolvers
+   *   The resolvers.
+   */
+  public function __construct(array $resolvers = []) {
+    $this->resolvers = $resolvers;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function addResolver(OrderTypeResolverInterface $resolver) {
+    $this->resolvers[] = $resolver;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function getResolvers() {
+    return $this->resolvers;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function resolve(LineItemInterface $line_item) {
+    foreach ($this->resolvers as $resolver) {
+      $result = $resolver->resolve($line_item);
+      if ($result) {
+        return $result;
+      }
+    }
+  }
+
+}
diff --git a/modules/order/src/Resolver/ChainOrderTypeResolverInterface.php b/modules/order/src/Resolver/ChainOrderTypeResolverInterface.php
new file mode 100644
index 0000000..324ca2d
--- /dev/null
+++ b/modules/order/src/Resolver/ChainOrderTypeResolverInterface.php
@@ -0,0 +1,29 @@
+<?php
+
+namespace Drupal\commerce_order\Resolver;
+
+/**
+ * Runs the added resolvers one by one until one of them returns the order type.
+ *
+ * Each resolver in the chain can be another chain, which is why this interface
+ * extends the order type resolver one.
+ */
+interface ChainOrderTypeResolverInterface extends OrderTypeResolverInterface {
+
+  /**
+   * Adds a resolver.
+   *
+   * @param \Drupal\commerce_order\Resolver\OrderTypeResolverInterface $resolver
+   *   The resolver.
+   */
+  public function addResolver(OrderTypeResolverInterface $resolver);
+
+  /**
+   * Gets all added resolvers.
+   *
+   * @return \Drupal\commerce_order\Resolver\OrderTypeResolverInterface[]
+   *   The resolvers.
+   */
+  public function getResolvers();
+
+}
diff --git a/modules/order/src/Resolver/DefaultOrderTypeResolver.php b/modules/order/src/Resolver/DefaultOrderTypeResolver.php
new file mode 100644
index 0000000..bb1b627
--- /dev/null
+++ b/modules/order/src/Resolver/DefaultOrderTypeResolver.php
@@ -0,0 +1,39 @@
+<?php
+
+namespace Drupal\commerce_order\Resolver;
+use Drupal\commerce_order\Entity\LineItemInterface;
+use Drupal\Core\Entity\EntityTypeManagerInterface;
+
+/**
+ * Returns the order type, based on line item type configuration.
+ */
+class DefaultOrderTypeResolver implements OrderTypeResolverInterface {
+
+  /**
+   * The line item type storage.
+   *
+   * @var \Drupal\Core\Entity\EntityStorageInterface
+   */
+  protected $lineItemTypeStorage;
+
+  /**
+   * Constructs a new DefaultOrderTypeResolver object.
+   *
+   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
+   *   The entity type manager.
+   */
+  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
+    $this->lineItemTypeStorage = $entity_type_manager->getStorage('commerce_line_item_type');
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  public function resolve(LineItemInterface $line_item) {
+    /** @var \Drupal\commerce_order\Entity\LineItemTypeInterface $line_item_type */
+    $line_item_type = $this->lineItemTypeStorage->load($line_item->bundle());
+
+    return $line_item_type->getOrderTypeId();
+  }
+
+}
diff --git a/modules/order/src/Resolver/OrderTypeResolverInterface.php b/modules/order/src/Resolver/OrderTypeResolverInterface.php
new file mode 100644
index 0000000..ac349d2
--- /dev/null
+++ b/modules/order/src/Resolver/OrderTypeResolverInterface.php
@@ -0,0 +1,24 @@
+<?php
+
+namespace Drupal\commerce_order\Resolver;
+
+use Drupal\commerce_order\Entity\LineItemInterface;
+
+/**
+ * Defines the interface for order type resolvers.
+ */
+interface OrderTypeResolverInterface {
+
+  /**
+   * Resolves the order type.
+   *
+   * @param \Drupal\commerce_order\Entity\LineItemInterface $line_item
+   *   The line item being added to an order.
+   *
+   * @return \Drupal\commerce_order\Entity\OrderTypeInterface|NULL
+   *   The order type, if resolved. Otherwise NULL, indicating that the next
+   *   resolver in the chain should be called.
+   */
+  public function resolve(LineItemInterface $line_item);
+
+}
diff --git a/modules/order/tests/src/Kernel/ChainOrderTypeResolverTest.php b/modules/order/tests/src/Kernel/ChainOrderTypeResolverTest.php
new file mode 100644
index 0000000..8876fbb
--- /dev/null
+++ b/modules/order/tests/src/Kernel/ChainOrderTypeResolverTest.php
@@ -0,0 +1,66 @@
+<?php
+
+namespace Drupal\Tests\commerce_order\Kernel;
+
+use Drupal\commerce_order\Entity\LineItem;
+use Drupal\commerce_order\Entity\LineItemType;
+use Drupal\KernelTests\KernelTestBase;
+
+/**
+ * Tests the chain order type resolver.
+ *
+ * @group commerce
+ */
+class ChainOrderTypeResolverTest extends KernelTestBase {
+
+  /**
+   * Modules to enable.
+   *
+   * @var array
+   */
+  public static $modules = [
+    'system', 'field', 'options', 'user', 'entity',
+    'views', 'address', 'profile', 'state_machine',
+    'inline_entity_form', 'commerce', 'commerce_price',
+    'commerce_store', 'commerce_product',
+    'commerce_order',
+  ];
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function setUp() {
+    parent::setUp();
+    $this->installSchema('system', 'router');
+    $this->installEntitySchema('user');
+    $this->installEntitySchema('profile');
+    $this->installEntitySchema('commerce_store');
+    $this->installEntitySchema('commerce_order');
+    $this->installEntitySchema('commerce_line_item');
+    $this->installConfig('commerce_order');
+    // A line item type that doesn't need a purchasable entity, for simplicity.
+    LineItemType::create([
+      'id' => 'test',
+      'label' => 'Test',
+      'orderType' => 'default',
+    ])->save();
+  }
+
+  /**
+   * Tests resolving the order type.
+   */
+  public function testOrderTypeResolution() {
+    $line_item = LineItem::create([
+      'type' => 'test',
+    ]);
+    $line_item->save();
+
+    /** @var \Drupal\commerce_order\Resolver\ChainOrderTypeResolverInterface $resolver */
+    $resolver = \Drupal::service('commerce_order.chain_order_type_resolver');
+
+    $order_type = $resolver->resolve($line_item);
+
+    $this->assertEquals('default', $order_type);
+  }
+
+}
