Skip to content

Commit 939e556

Browse files
Roman HaninRoman Hanin
authored andcommitted
B2B-2424: Optimize permissions read in addProductsToCart mutation
1 parent 65c71f2 commit 939e556

File tree

4 files changed

+109
-27
lines changed

4 files changed

+109
-27
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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\CartItem;
9+
10+
use Magento\GraphQl\Model\Query\ContextInterface;
11+
12+
/**
13+
* Chained precursor composite.
14+
*/
15+
class PrecursorComposite implements PrecursorInterface
16+
{
17+
/**
18+
* @var PrecursorInterface[]
19+
*/
20+
private $precursors = [];
21+
22+
/**
23+
* @var array
24+
*/
25+
private $errors = [];
26+
27+
/**
28+
* @param array $precursors
29+
*/
30+
public function __construct(array $precursors)
31+
{
32+
$this->precursors = $precursors;
33+
}
34+
35+
/**
36+
* @inheirtdoc
37+
*/
38+
public function process(array $cartItemData, ContextInterface $context): array
39+
{
40+
foreach ($this->precursors as $precursor) {
41+
$cartItemData = $precursor->process($cartItemData, $context);
42+
array_merge($this->errors, $precursor->getErrors());
43+
}
44+
return $cartItemData;
45+
}
46+
47+
/**
48+
* @inheirtdoc
49+
*/
50+
public function getErrors(): array
51+
{
52+
$errors = [];
53+
foreach ($this->precursors as $precursor) {
54+
foreach ($precursor->getErrors() as $error) {
55+
$errors[] = $error;
56+
}
57+
}
58+
return $errors;
59+
}
60+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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\CartItem;
9+
10+
use Magento\GraphQl\Model\Query\ContextInterface;
11+
use Magento\Quote\Model\Cart\Data\CartItem;
12+
use Magento\QuoteGraphQl\Model\CartItem\DataProvider\Processor\ItemDataProcessorInterface;
13+
14+
/**
15+
* Cart items preparator for cart operations.
16+
*/
17+
interface PrecursorInterface extends ItemDataProcessorInterface
18+
{
19+
/**
20+
* Preprocess cart items for Graphql request.
21+
*
22+
* @param ContextInterface $context
23+
* @param array $cartItemData
24+
* @return array
25+
*/
26+
public function process(array $cartItemData, ContextInterface $context): array;
27+
28+
/**
29+
* Get precursor errors.
30+
*
31+
* @return array
32+
*/
33+
public function getErrors(): array;
34+
}

app/code/Magento/QuoteGraphQl/Model/Resolver/AddProductsToCart.php

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Magento\QuoteGraphQl\Model\Resolver;
99

10+
use Magento\Framework\App\ObjectManager;
1011
use Magento\Framework\GraphQl\Config\Element\Field;
1112
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1213
use Magento\Framework\GraphQl\Query\ResolverInterface;
@@ -19,6 +20,7 @@
1920
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
2021
use Magento\Quote\Model\Cart\Data\Error;
2122
use Magento\QuoteGraphQl\Model\CartItem\DataProvider\Processor\ItemDataProcessorInterface;
23+
use Magento\QuoteGraphQl\Model\CartItem\PrecursorInterface;
2224

2325
/**
2426
* Resolver for addProductsToCart mutation
@@ -38,31 +40,35 @@ class AddProductsToCart implements ResolverInterface
3840
private $addProductsToCartService;
3941

4042
/**
41-
* @var ItemDataProcessorInterface
43+
* @var QuoteMutexInterface
4244
*/
43-
private $itemDataProcessor;
45+
private $quoteMutex;
4446

4547
/**
46-
* @var QuoteMutexInterface
48+
* @var PrecursorInterface|null
4749
*/
48-
private $quoteMutex;
50+
private $cartItemPrecursor;
4951

5052
/**
5153
* @param GetCartForUser $getCartForUser
5254
* @param AddProductsToCartService $addProductsToCart
5355
* @param ItemDataProcessorInterface $itemDataProcessor
5456
* @param QuoteMutexInterface $quoteMutex
57+
* @param PrecursorInterface|null $cartItemPrecursor
58+
*
59+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
5560
*/
5661
public function __construct(
5762
GetCartForUser $getCartForUser,
5863
AddProductsToCartService $addProductsToCart,
5964
ItemDataProcessorInterface $itemDataProcessor,
60-
QuoteMutexInterface $quoteMutex
65+
QuoteMutexInterface $quoteMutex,
66+
PrecursorInterface $cartItemPrecursor = null
6167
) {
6268
$this->getCartForUser = $getCartForUser;
6369
$this->addProductsToCartService = $addProductsToCart;
64-
$this->itemDataProcessor = $itemDataProcessor;
6570
$this->quoteMutex = $quoteMutex;
71+
$this->cartItemPrecursor = $cartItemPrecursor ?: ObjectManager::getInstance()->get(PrecursorInterface::class);
6672
}
6773

6874
/**
@@ -102,11 +108,9 @@ private function run($context, ?array $args): array
102108

103109
// Shopping Cart validation
104110
$this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
111+
$cartItemsData = $this->cartItemPrecursor->process($cartItemsData, $context);
105112
$cartItems = [];
106113
foreach ($cartItemsData as $cartItemData) {
107-
if (!$this->itemIsAllowedToCart($cartItemData, $context)) {
108-
continue;
109-
}
110114
$cartItems[] = (new CartItemFactory())->create($cartItemData);
111115
}
112116

@@ -125,25 +129,8 @@ function (Error $error) {
125129
'path' => [$error->getCartItemPosition()]
126130
];
127131
},
128-
$addProductsToCartOutput->getErrors()
132+
array_merge($addProductsToCartOutput->getErrors(), $this->cartItemPrecursor->getErrors())
129133
)
130134
];
131135
}
132-
133-
/**
134-
* Check if the item can be added to cart
135-
*
136-
* @param array $cartItemData
137-
* @param ContextInterface $context
138-
* @return bool
139-
*/
140-
private function itemIsAllowedToCart(array $cartItemData, ContextInterface $context): bool
141-
{
142-
$cartItemData = $this->itemDataProcessor->process($cartItemData, $context);
143-
if (isset($cartItemData['grant_checkout']) && $cartItemData['grant_checkout'] === false) {
144-
return false;
145-
}
146-
147-
return true;
148-
}
149136
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
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\CartItem\DataProvider\CustomizableOptionValueInterface" type="Magento\QuoteGraphQl\Model\CartItem\DataProvider\CustomizableOptionValue\Composite" />
1010
<preference for="Magento\QuoteGraphQl\Model\CartItem\DataProvider\Processor\ItemDataProcessorInterface" type="Magento\QuoteGraphQl\Model\CartItem\DataProvider\Processor\ItemDataCompositeProcessor" />
11+
<preference for="Magento\QuoteGraphQl\Model\CartItem\PrecursorInterface" type="Magento\QuoteGraphQl\Model\CartItem\PrecursorComposite" />
1112
<type name="Magento\QuoteGraphQl\Model\Resolver\CartItemTypeResolver">
1213
<arguments>
1314
<argument name="supportedTypes" xsi:type="array">

0 commit comments

Comments
 (0)