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

Commit 1064854

Browse files
author
Roman Glushko
committed
#141 Performed decoupling of the resolvers
1 parent 01404a0 commit 1064854

File tree

6 files changed

+318
-212
lines changed

6 files changed

+318
-212
lines changed

app/code/Magento/CatalogGraphQl/Model/Product/Option/DateType.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313

1414
/**
1515
* Catalog product option date validator
16+
* {@inheritdoc}
1617
*/
1718
class DateType extends ProductDateOptionType
1819
{
1920
/**
2021
* Make valid string as a value of date option type for GraphQl queries
21-
*
2222
* {@inheritdoc}
2323
*/
2424
public function validateUserValue($values)
@@ -32,9 +32,7 @@ public function validateUserValue($values)
3232

3333
/**
3434
* Format date value from string to date array
35-
*
3635
* @param [] $values
37-
*
3836
* @return []
3937
* @throws LocalizedException
4038
*/
@@ -58,9 +56,7 @@ protected function formatValues($values)
5856
}
5957

6058
/**
61-
* Check if calendar should be shown
62-
*
63-
* @return bool
59+
* @inheritdoc
6460
*/
6561
public function useCalendar()
6662
{
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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;
9+
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
use Magento\Framework\DataObject;
12+
use Magento\Framework\DataObjectFactory;
13+
use Magento\Framework\Exception\LocalizedException;
14+
use Magento\Framework\Exception\NoSuchEntityException;
15+
use Magento\Framework\Stdlib\ArrayManager;
16+
use Magento\Quote\Api\CartRepositoryInterface;
17+
use Magento\Quote\Api\Data\CartInterface;
18+
use Magento\Quote\Api\GuestCartRepositoryInterface;
19+
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
20+
use Magento\Quote\Model\Quote;
21+
use Magento\Quote\Model\Quote\Item as QuoteItem;
22+
23+
/**
24+
* Add simple product to cart process
25+
*/
26+
class AddSimpleProductToCartProcessor
27+
{
28+
/**
29+
* @var ArrayManager
30+
*/
31+
private $arrayManager;
32+
33+
/**
34+
* @var CartRepositoryInterface
35+
*/
36+
private $cartRepository;
37+
38+
/**
39+
* @var MaskedQuoteIdToQuoteIdInterface
40+
*/
41+
private $maskedQuoteIdToQuoteId;
42+
43+
/**
44+
* @var DataObjectFactory
45+
*/
46+
private $dataObjectFactory;
47+
48+
/**
49+
* @var GuestCartRepositoryInterface
50+
*/
51+
private $guestCartRepository;
52+
53+
/**
54+
* @var ProductRepositoryInterface
55+
*/
56+
private $productRepository;
57+
58+
/**
59+
* @param ArrayManager $arrayManager
60+
* @param DataObjectFactory $dataObjectFactory
61+
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
62+
* @param CartRepositoryInterface $cartRepository
63+
* @param GuestCartRepositoryInterface $guestCartRepository
64+
* @param ProductRepositoryInterface $productRepository
65+
*/
66+
public function __construct(
67+
ArrayManager $arrayManager,
68+
DataObjectFactory $dataObjectFactory,
69+
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
70+
CartRepositoryInterface $cartRepository,
71+
GuestCartRepositoryInterface $guestCartRepository,
72+
ProductRepositoryInterface $productRepository
73+
) {
74+
$this->productRepository = $productRepository;
75+
$this->guestCartRepository = $guestCartRepository;
76+
$this->dataObjectFactory = $dataObjectFactory;
77+
$this->cartRepository = $cartRepository;
78+
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
79+
$this->arrayManager = $arrayManager;
80+
}
81+
82+
/**
83+
* Resolve adding simple product to cart for customers/guests
84+
* @param CartInterface|Quote $cart
85+
* @param array $cartItemData
86+
* @return QuoteItem|string
87+
* @throws LocalizedException
88+
* @throws NoSuchEntityException
89+
*/
90+
public function process($cart, array $cartItemData)
91+
{
92+
$sku = $this->arrayManager->get('details/sku', $cartItemData);
93+
$product = $this->productRepository->get($sku);
94+
95+
return $cart->addProduct($product, $this->getBuyRequest($cartItemData));
96+
}
97+
98+
/**
99+
* Format GraphQl input data to a shape that buy request has
100+
* @param array $cartItem
101+
* @return DataObject
102+
*/
103+
private function getBuyRequest(array $cartItem): DataObject
104+
{
105+
$customOptions = [];
106+
$qty = $this->arrayManager->get('details/qty', $cartItem);
107+
$customizableOptions = $this->arrayManager->get('customizable_options', $cartItem, []);
108+
109+
foreach ($customizableOptions as $customizableOption) {
110+
$customOptions[$customizableOption['id']] = $customizableOption['value'];
111+
}
112+
113+
return $this->dataObjectFactory->create([
114+
'data' => [
115+
'qty' => $qty,
116+
'options' => $customOptions
117+
]
118+
]);
119+
}
120+
}

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

Lines changed: 14 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
namespace Magento\QuoteGraphQl\Model\Resolver\Cart;
99

1010
use Magento\Authorization\Model\UserContextInterface;
11-
use Magento\Catalog\Api\ProductRepositoryInterface;
12-
use Magento\Framework\DataObject;
13-
use Magento\Framework\DataObjectFactory;
1411
use Magento\Framework\Exception\NoSuchEntityException;
1512
use Magento\Framework\GraphQl\Config\Element\Field;
1613
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
@@ -25,15 +22,20 @@
2522
use Magento\Quote\Api\GuestCartRepositoryInterface;
2623
use Magento\Quote\Model\MaskedQuoteIdToQuoteIdInterface;
2724
use Magento\Quote\Model\Quote;
28-
use Magento\QuoteGraphQl\Model\Hydrator\CartHydrator;
25+
use Magento\QuoteGraphQl\Model\Cart\AddSimpleProductToCartProcessor;
26+
use Magento\QuoteGraphQl\Model\Resolver\DataProvider\Cart\CartHydrator;
2927

3028
/**
3129
* Add simple product to cart GraphQl resolver
32-
*
3330
* {@inheritdoc}
3431
*/
3532
class AddSimpleProductsToCart implements ResolverInterface
3633
{
34+
/**
35+
* @var AddSimpleProductToCartProcessor
36+
*/
37+
private $addSimpleProductToCartProcessor;
38+
3739
/**
3840
* @var CartRepositoryInterface
3941
*/
@@ -44,21 +46,11 @@ class AddSimpleProductsToCart implements ResolverInterface
4446
*/
4547
private $maskedQuoteIdToQuoteId;
4648

47-
/**
48-
* @var DataObjectFactory
49-
*/
50-
private $dataObjectFactory;
51-
5249
/**
5350
* @var GuestCartRepositoryInterface
5451
*/
5552
private $guestCartRepository;
5653

57-
/**
58-
* @var ProductRepositoryInterface
59-
*/
60-
private $productRepository;
61-
6254
/**
6355
* @var CartHydrator
6456
*/
@@ -80,42 +72,38 @@ class AddSimpleProductsToCart implements ResolverInterface
8072
private $userContext;
8173

8274
/**
83-
* @param DataObjectFactory $dataObjectFactory
75+
* @param AddSimpleProductToCartProcessor $addSimpleProductToCartProcessor
8476
* @param CartHydrator $cartHydrator
8577
* @param ArrayManager $arrayManager
8678
* @param MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId
8779
* @param CartRepositoryInterface $cartRepository
8880
* @param GuestCartRepositoryInterface $guestCartRepository
89-
* @param ProductRepositoryInterface $productRepository
9081
* @param ValueFactory $valueFactory
9182
* @param UserContextInterface $userContext
9283
*/
9384
public function __construct(
94-
DataObjectFactory $dataObjectFactory,
85+
AddSimpleProductToCartProcessor $addSimpleProductToCartProcessor,
9586
CartHydrator $cartHydrator,
9687
ArrayManager $arrayManager,
9788
MaskedQuoteIdToQuoteIdInterface $maskedQuoteIdToQuoteId,
9889
CartRepositoryInterface $cartRepository,
9990
GuestCartRepositoryInterface $guestCartRepository,
100-
ProductRepositoryInterface $productRepository,
10191
ValueFactory $valueFactory,
10292
UserContextInterface $userContext
10393
) {
10494
$this->valueFactory = $valueFactory;
10595
$this->userContext = $userContext;
10696
$this->arrayManager = $arrayManager;
107-
$this->productRepository = $productRepository;
10897
$this->cartHydrator = $cartHydrator;
10998
$this->guestCartRepository = $guestCartRepository;
110-
$this->dataObjectFactory = $dataObjectFactory;
11199
$this->cartRepository = $cartRepository;
112100
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
101+
$this->addSimpleProductToCartProcessor = $addSimpleProductToCartProcessor;
113102
}
114103

115104
/**
116105
* Resolve adding simple product to cart for customers/guests
117-
*
118-
* {@inheritDoc}
106+
* {@inheritdoc}
119107
*/
120108
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null) : Value
121109
{
@@ -136,11 +124,10 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
136124

137125
$cart = $this->getCart((string) $cartHash);
138126

139-
foreach ($cartItems as $cartItem) {
140-
$sku = $this->arrayManager->get('details/sku', $cartItem);
141-
$product = $this->productRepository->get($sku);
127+
foreach ($cartItems as $cartItemData) {
128+
$sku = $this->arrayManager->get('details/sku', $cartItemData);
142129

143-
$message = $cart->addProduct($product, $this->getBuyRequest($cartItem));
130+
$message = $this->addSimpleProductToCartProcessor->process($cart, $cartItemData);
144131

145132
if (is_string($message)) {
146133
throw new GraphQlInputException(
@@ -166,33 +153,8 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
166153
return $this->valueFactory->create($result);
167154
}
168155

169-
/**
170-
* Format GraphQl input data to a shape that buy request has
171-
*
172-
* @param array $cartItem
173-
* @return DataObject
174-
*/
175-
private function getBuyRequest($cartItem): DataObject
176-
{
177-
$customOptions = [];
178-
$qty = $this->arrayManager->get('details/qty', $cartItem);
179-
$customizableOptions = $this->arrayManager->get('customizable_options', $cartItem, []);
180-
181-
foreach ($customizableOptions as $customizableOption) {
182-
$customOptions[$customizableOption['id']] = $customizableOption['value'];
183-
}
184-
185-
return $this->dataObjectFactory->create([
186-
'data' => [
187-
'qty' => $qty,
188-
'options' => $customOptions
189-
]
190-
]);
191-
}
192-
193156
/**
194157
* Collecting cart errors
195-
*
196158
* @param CartInterface|Quote $cart
197159
* @return string
198160
*/
@@ -210,7 +172,6 @@ private function getCartErrors($cart): string
210172

211173
/**
212174
* Retrieving quote mode based on customer authorization
213-
*
214175
* @param string $cartHash
215176
* @return CartInterface|Quote
216177
* @throws NoSuchEntityException

0 commit comments

Comments
 (0)