Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.

Commit c63e9e8

Browse files
author
Roman Glushko
committed
#141 Added checking of auth of the request & inited api functional test
1 parent 035baf6 commit c63e9e8

File tree

4 files changed

+174
-144
lines changed

4 files changed

+174
-144
lines changed

app/code/Magento/QuoteGraphQl/Model/Resolver/CartItem/CustomizableOptions.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Magento\QuoteGraphQl\Model\Resolver\CartItem;
99

1010
use Magento\Authorization\Model\UserContextInterface;
11+
use Magento\Catalog\Api\Data\ProductCustomOptionInterface;
1112
use Magento\Catalog\Model\Product\Option\Type\DefaultType as DefaultOptionType;
1213
use Magento\Catalog\Model\Product\Option\Type\Select as SelectOptionType;
1314
use Magento\Catalog\Model\Product\Option\Type\Text as TextOptionType;
@@ -108,7 +109,7 @@ private function getOptionData($cartItem, int $optionId): array
108109
->setConfigurationItem($cartItem)
109110
->setConfigurationItemOption($itemOption);
110111

111-
if ('file' == $option->getType()) {
112+
if (ProductCustomOptionInterface::OPTION_GROUP_FILE == $option->getType()) {
112113
$downloadParams = $cartItem->getFileDownloadParams();
113114

114115
if ($downloadParams) {
@@ -128,7 +129,9 @@ private function getOptionData($cartItem, int $optionId): array
128129
'label' => $optionTypeGroup->getFormattedOptionValue($itemOption->getValue()),
129130
];
130131

131-
if ('drop_down' == $option->getType()) {
132+
if (ProductCustomOptionInterface::OPTION_TYPE_DROP_DOWN == $option->getType()
133+
|| ProductCustomOptionInterface::OPTION_TYPE_RADIO == $option->getType()
134+
) {
132135
$optionValue = $option->getValueById($itemOption->getValue());
133136
$selectedOptionValueData['price'] = [
134137
'type' => strtoupper($optionValue->getPriceType()),
@@ -139,7 +142,11 @@ private function getOptionData($cartItem, int $optionId): array
139142
$selectedOptionValueData = [$selectedOptionValueData];
140143
}
141144

142-
if ('field' == $option->getType() || 'date' == $option->getType()) {
145+
if (ProductCustomOptionInterface::OPTION_TYPE_FIELD == $option->getType()
146+
|| ProductCustomOptionInterface::OPTION_TYPE_AREA == $option->getType()
147+
|| ProductCustomOptionInterface::OPTION_GROUP_DATE == $option->getType()
148+
|| ProductCustomOptionInterface::OPTION_TYPE_TIME == $option->getType()
149+
) {
143150
$selectedOptionValueData['price'] = [
144151
'type' => strtoupper($option->getPriceType()),
145152
'units' => '$',
@@ -149,7 +156,7 @@ private function getOptionData($cartItem, int $optionId): array
149156
$selectedOptionValueData = [$selectedOptionValueData];
150157
}
151158

152-
if ('multiple' == $option->getType()) {
159+
if (ProductCustomOptionInterface::OPTION_TYPE_MULTIPLE == $option->getType()) {
153160
$selectedOptionValueData = [];
154161
$optionIds = explode(',', $itemOption->getValue());
155162

app/code/Magento/SimpleProductGraphQl/Model/Resolver/Cart/AddSimpleProductsToCart.php

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\Catalog\Api\ProductRepositoryInterface;
1212
use Magento\Framework\DataObject;
1313
use Magento\Framework\DataObjectFactory;
14+
use Magento\Framework\Exception\NoSuchEntityException;
1415
use Magento\Framework\GraphQl\Config\Element\Field;
1516
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1617
use Magento\Framework\GraphQl\Query\Resolver\Value;
@@ -19,17 +20,28 @@
1920
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
2021
use Magento\Framework\Message\AbstractMessage;
2122
use Magento\Framework\Stdlib\ArrayManager;
23+
use Magento\Quote\Api\CartRepositoryInterface;
2224
use Magento\Quote\Api\Data\CartInterface;
2325
use Magento\Quote\Api\GuestCartRepositoryInterface;
26+
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
2427
use Magento\Quote\Model\Quote;
25-
use Magento\Quote\Model\QuoteRepository;
2628
use Magento\QuoteGraphQl\Model\Hydrator\CartHydrator;
2729

2830
/**
2931
* {@inheritdoc}
3032
*/
3133
class AddSimpleProductsToCart implements ResolverInterface
3234
{
35+
/**
36+
* @var CartRepositoryInterface
37+
*/
38+
private $cartRepository;
39+
40+
/**
41+
* @var MaskedQuoteIdToQuoteIdInterface
42+
*/
43+
private $maskedQuoteIdToQuoteId;
44+
3345
/**
3446
* @var DataObjectFactory
3547
*/
@@ -40,11 +52,6 @@ class AddSimpleProductsToCart implements ResolverInterface
4052
*/
4153
private $guestCartRepository;
4254

43-
/**
44-
* @var QuoteRepository
45-
*/
46-
private $quoteRepository;
47-
4855
/**
4956
* @var ProductRepositoryInterface
5057
*/
@@ -74,8 +81,9 @@ class AddSimpleProductsToCart implements ResolverInterface
7481
* @param DataObjectFactory $dataObjectFactory
7582
* @param CartHydrator $cartHydrator
7683
* @param ArrayManager $arrayManager
84+
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
85+
* @param CartRepositoryInterface $cartRepository
7786
* @param GuestCartRepositoryInterface $guestCartRepository
78-
* @param QuoteRepository $quoteRepository
7987
* @param ProductRepositoryInterface $productRepository
8088
* @param ValueFactory $valueFactory
8189
* @param UserContextInterface $userContext
@@ -84,8 +92,9 @@ public function __construct(
8492
DataObjectFactory $dataObjectFactory,
8593
CartHydrator $cartHydrator,
8694
ArrayManager $arrayManager,
95+
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
96+
CartRepositoryInterface $cartRepository,
8797
GuestCartRepositoryInterface $guestCartRepository,
88-
QuoteRepository $quoteRepository,
8998
ProductRepositoryInterface $productRepository,
9099
ValueFactory $valueFactory,
91100
UserContextInterface $userContext
@@ -95,20 +104,21 @@ public function __construct(
95104
$this->arrayManager = $arrayManager;
96105
$this->productRepository = $productRepository;
97106
$this->cartHydrator = $cartHydrator;
98-
$this->quoteRepository = $quoteRepository;
99107
$this->guestCartRepository = $guestCartRepository;
100108
$this->dataObjectFactory = $dataObjectFactory;
109+
$this->cartRepository = $cartRepository;
110+
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
101111
}
102112

103113
/**
104114
* {@inheritDoc}
105115
*/
106116
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) : Value
107117
{
108-
$cartId = $this->arrayManager->get('input/cart_id', $args);
118+
$cartHash = $this->arrayManager->get('input/cart_id', $args);
109119
$cartItems = $this->arrayManager->get('input/cartItems', $args);
110120

111-
if (!isset($cartId)) {
121+
if (!isset($cartHash)) {
112122
throw new GraphQlInputException(
113123
__('Missing key %1 in cart data', ['cart_id'])
114124
);
@@ -120,8 +130,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
120130
);
121131
}
122132

123-
/** @var CartInterface|Quote $cart */
124-
$cart = $this->guestCartRepository->get($cartId);
133+
$cart = $this->getCart((string) $cartHash);
125134

126135
foreach ($cartItems as $cartItem) {
127136
$sku = $this->arrayManager->get('details/sku', $cartItem);
@@ -134,7 +143,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
134143
}
135144
}
136145

137-
$this->quoteRepository->save($cart);
146+
$this->cartRepository->save($cart);
138147

139148
$result = function () use ($cart) {
140149
return [
@@ -184,4 +193,21 @@ private function getCartErrors($cart): string
184193

185194
return implode(PHP_EOL, $errorMessages);
186195
}
196+
197+
/**
198+
* @param string $cartHash
199+
* @return CartInterface|Quote
200+
* @throws NoSuchEntityException
201+
*/
202+
private function getCart(string $cartHash): CartInterface
203+
{
204+
$customerId = $this->userContext->getUserId();
205+
206+
if (!$customerId) {
207+
return $this->guestCartRepository->get($cartHash);
208+
}
209+
210+
$cartId = $this->maskedQuoteIdToQuoteId->execute((string) $cartHash);
211+
return $this->cartRepository->get($cartId);
212+
}
187213
}

app/code/Magento/SimpleProductGraphQl/Model/Resolver/CartItem/CustomizableOptions.php

Lines changed: 0 additions & 126 deletions
This file was deleted.

0 commit comments

Comments
 (0)