Skip to content

Commit 8b56ad0

Browse files
committed
Separate buy request creation into extensible builder
This allows product type graphql modules a method for managing how inputs are mapped when buy requests are created.
1 parent 0d0b300 commit 8b56ad0

File tree

6 files changed

+156
-61
lines changed

6 files changed

+156
-61
lines changed

app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php

Lines changed: 10 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1616
use Magento\Framework\Stdlib\ArrayManager;
1717
use Magento\Quote\Model\Quote;
18+
use Magento\QuoteGraphQl\Model\Cart\BuyRequest\BuyRequestBuilder;
1819

1920
/**
2021
* Add simple product to cart
@@ -29,28 +30,28 @@ class AddSimpleProductToCart
2930
private $arrayManager;
3031

3132
/**
32-
* @var DataObjectFactory
33+
* @var ProductRepositoryInterface
3334
*/
34-
private $dataObjectFactory;
35+
private $productRepository;
3536

3637
/**
37-
* @var ProductRepositoryInterface
38+
* @var BuyRequestBuilder
3839
*/
39-
private $productRepository;
40+
private $buyRequestBuilder;
4041

4142
/**
4243
* @param ArrayManager $arrayManager
43-
* @param DataObjectFactory $dataObjectFactory
4444
* @param ProductRepositoryInterface $productRepository
45+
* @param BuyRequestBuilder $buyRequestBuilder
4546
*/
4647
public function __construct(
4748
ArrayManager $arrayManager,
48-
DataObjectFactory $dataObjectFactory,
49-
ProductRepositoryInterface $productRepository
49+
ProductRepositoryInterface $productRepository,
50+
BuyRequestBuilder $buyRequestBuilder
5051
) {
5152
$this->arrayManager = $arrayManager;
52-
$this->dataObjectFactory = $dataObjectFactory;
5353
$this->productRepository = $productRepository;
54+
$this->buyRequestBuilder = $buyRequestBuilder;
5455
}
5556

5657
/**
@@ -66,8 +67,6 @@ public function __construct(
6667
public function execute(Quote $cart, array $cartItemData): void
6768
{
6869
$sku = $this->extractSku($cartItemData);
69-
$qty = $this->extractQty($cartItemData);
70-
$customizableOptions = $this->extractCustomizableOptions($cartItemData);
7170

7271
try {
7372
$product = $this->productRepository->get($sku);
@@ -76,7 +75,7 @@ public function execute(Quote $cart, array $cartItemData): void
7675
}
7776

7877
try {
79-
$result = $cart->addProduct($product, $this->createBuyRequest($qty, $customizableOptions));
78+
$result = $cart->addProduct($product, $this->buyRequestBuilder->build($cartItemData));
8079
} catch (\Exception $e) {
8180
throw new GraphQlInputException(
8281
__(
@@ -106,54 +105,4 @@ private function extractSku(array $cartItemData): string
106105
}
107106
return (string)$sku;
108107
}
109-
110-
/**
111-
* Extract Qty from cart item data
112-
*
113-
* @param array $cartItemData
114-
* @return float
115-
* @throws GraphQlInputException
116-
*/
117-
private function extractQty(array $cartItemData): float
118-
{
119-
$qty = $this->arrayManager->get('data/qty', $cartItemData);
120-
if (!isset($qty)) {
121-
throw new GraphQlInputException(__('Missing key "qty" in cart item data'));
122-
}
123-
return (float)$qty;
124-
}
125-
126-
/**
127-
* Extract Customizable Options from cart item data
128-
*
129-
* @param array $cartItemData
130-
* @return array
131-
*/
132-
private function extractCustomizableOptions(array $cartItemData): array
133-
{
134-
$customizableOptions = $this->arrayManager->get('customizable_options', $cartItemData, []);
135-
136-
$customizableOptionsData = [];
137-
foreach ($customizableOptions as $customizableOption) {
138-
$customizableOptionsData[$customizableOption['id']] = $customizableOption['value'];
139-
}
140-
return $customizableOptionsData;
141-
}
142-
143-
/**
144-
* Format GraphQl input data to a shape that buy request has
145-
*
146-
* @param float $qty
147-
* @param array $customOptions
148-
* @return DataObject
149-
*/
150-
private function createBuyRequest(float $qty, array $customOptions): DataObject
151-
{
152-
return $this->dataObjectFactory->create([
153-
'data' => [
154-
'qty' => $qty,
155-
'options' => $customOptions,
156-
],
157-
]);
158-
}
159108
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Cart\BuyRequest;
9+
10+
use Magento\Framework\DataObject;
11+
use Magento\Framework\DataObjectFactory;
12+
13+
class BuyRequestBuilder
14+
{
15+
/**
16+
* @var BuyRequestDataProviderInterface[]
17+
*/
18+
private $providers;
19+
20+
/**
21+
* @var DataObjectFactory
22+
*/
23+
private $dataObjectFactory;
24+
25+
public function __construct(
26+
DataObjectFactory $dataObjectFactory,
27+
array $providers = []
28+
) {
29+
$this->dataObjectFactory = $dataObjectFactory;
30+
$this->providers = $providers;
31+
}
32+
33+
public function build(array $cartItemData): DataObject
34+
{
35+
$requestData = [];
36+
foreach ($this->providers as $provider) {
37+
$requestData = array_merge($requestData, $provider->execute($cartItemData));
38+
}
39+
40+
return $this->dataObjectFactory->create(['data' => $requestData]);
41+
}
42+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Cart\BuyRequest;
9+
10+
interface BuyRequestDataProviderInterface
11+
{
12+
public function execute(array $cartItemData): array;
13+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Cart\BuyRequest;
9+
10+
use Magento\Framework\Stdlib\ArrayManager;
11+
12+
class CustomizableOptionsDataProvider implements BuyRequestDataProviderInterface
13+
{
14+
/**
15+
* @var ArrayManager
16+
*/
17+
private $arrayManager;
18+
19+
/**
20+
* @param ArrayManager $arrayManager
21+
*/
22+
public function __construct(
23+
ArrayManager $arrayManager
24+
) {
25+
$this->arrayManager = $arrayManager;
26+
}
27+
28+
/**
29+
* @inheritdoc
30+
*/
31+
public function execute(array $cartItemData): array
32+
{
33+
$customizableOptions = $this->arrayManager->get('customizable_options', $cartItemData, []);
34+
35+
$customizableOptionsData = [];
36+
foreach ($customizableOptions as $customizableOption) {
37+
$customizableOptionsData[$customizableOption['id']] = $customizableOption['value'];
38+
}
39+
40+
return ['options' => $customizableOptionsData];
41+
}
42+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Model\Cart\BuyRequest;
9+
10+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
11+
use Magento\Framework\Stdlib\ArrayManager;
12+
13+
class DefaultDataProvider implements BuyRequestDataProviderInterface
14+
{
15+
/**
16+
* @var ArrayManager
17+
*/
18+
private $arrayManager;
19+
20+
/**
21+
* @param ArrayManager $arrayManager
22+
*/
23+
public function __construct(
24+
ArrayManager $arrayManager
25+
) {
26+
$this->arrayManager = $arrayManager;
27+
}
28+
29+
/**
30+
* @inheritdoc
31+
*/
32+
public function execute(array $cartItemData): array
33+
{
34+
$qty = $this->arrayManager->get('data/qty', $cartItemData);
35+
if (!isset($qty)) {
36+
throw new GraphQlInputException(__('Missing key "qty" in cart item data'));
37+
}
38+
39+
return ['qty' => (float)$qty];
40+
}
41+
}

app/code/Magento/QuoteGraphQl/etc/graphql/di.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@
88
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
99
<preference for="Magento\QuoteGraphQl\Model\Cart\SetShippingAddressesOnCartInterface"
1010
type="Magento\QuoteGraphQl\Model\Cart\SetShippingAddressOnCart" />
11+
<type name="Magento\QuoteGraphQl\Model\Cart\BuyRequest\BuyRequestBuilder">
12+
<arguments>
13+
<argument name="providers" xsi:type="array">
14+
<item name="default" xsi:type="object">Magento\QuoteGraphQl\Model\Cart\BuyRequest\DefaultDataProvider</item>
15+
<item name="customizable_options" xsi:type="object">Magento\QuoteGraphQl\Model\Cart\BuyRequest\CustomizableOptionsDataProvider</item>
16+
</argument>
17+
</arguments>
18+
</type>
1119
</config>

0 commit comments

Comments
 (0)