Skip to content

Commit 1cec875

Browse files
committed
Refactoring
1 parent ba64fbf commit 1cec875

File tree

4 files changed

+98
-105
lines changed

4 files changed

+98
-105
lines changed

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

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

1010
use Magento\Catalog\Api\ProductRepositoryInterface;
11-
use Magento\Framework\DataObject;
12-
use Magento\Framework\DataObjectFactory;
1311
use Magento\Framework\Exception\NoSuchEntityException;
1412
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1513
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
@@ -21,25 +19,25 @@
2119
class AddSimpleProductToCart
2220
{
2321
/**
24-
* @var DataObjectFactory
22+
* @var CreateBuyRequest
2523
*/
26-
private $dataObjectFactory;
24+
private $createBuyRequest;
2725

2826
/**
2927
* @var ProductRepositoryInterface
3028
*/
3129
private $productRepository;
3230

3331
/**
34-
* @param DataObjectFactory $dataObjectFactory
3532
* @param ProductRepositoryInterface $productRepository
33+
* @param CreateBuyRequest $createBuyRequest
3634
*/
3735
public function __construct(
38-
DataObjectFactory $dataObjectFactory,
39-
ProductRepositoryInterface $productRepository
36+
ProductRepositoryInterface $productRepository,
37+
CreateBuyRequest $createBuyRequest
4038
) {
41-
$this->dataObjectFactory = $dataObjectFactory;
4239
$this->productRepository = $productRepository;
40+
$this->createBuyRequest = $createBuyRequest;
4341
}
4442

4543
/**
@@ -56,7 +54,7 @@ public function execute(Quote $cart, array $cartItemData): void
5654
{
5755
$sku = $this->extractSku($cartItemData);
5856
$quantity = $this->extractQuantity($cartItemData);
59-
$customizableOptions = $this->extractCustomizableOptions($cartItemData);
57+
$customizableOptions = $cartItemData['customizable_options'] ?? [];
6058

6159
try {
6260
$product = $this->productRepository->get($sku);
@@ -65,7 +63,7 @@ public function execute(Quote $cart, array $cartItemData): void
6563
}
6664

6765
try {
68-
$result = $cart->addProduct($product, $this->createBuyRequest($quantity, $customizableOptions));
66+
$result = $cart->addProduct($product, $this->createBuyRequest->execute($quantity, $customizableOptions));
6967
} catch (\Exception $e) {
7068
throw new GraphQlInputException(
7169
__(
@@ -139,37 +137,4 @@ private function extractCustomizableOptions(array $cartItemData): array
139137
}
140138
return $customizableOptionsData;
141139
}
142-
143-
/**
144-
* Format GraphQl input data to a shape that buy request has
145-
*
146-
* @param float $quantity
147-
* @param array $customOptions
148-
* @return DataObject
149-
*/
150-
private function createBuyRequest(float $quantity, array $customOptions): DataObject
151-
{
152-
return $this->dataObjectFactory->create([
153-
'data' => [
154-
'qty' => $quantity,
155-
'options' => $customOptions,
156-
],
157-
]);
158-
}
159-
160-
/**
161-
* Convert custom options vakue
162-
*
163-
* @param string $value
164-
* @return string|array
165-
*/
166-
private function convertCustomOptionValue(string $value)
167-
{
168-
$value = trim($value);
169-
if (substr($value, 0, 1) === "[" &&
170-
substr($value, strlen($value) - 1, 1) === "]") {
171-
return explode(',', substr($value, 1, -1));
172-
}
173-
return $value;
174-
}
175140
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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\Framework\DataObject;
11+
use Magento\Framework\DataObjectFactory;
12+
13+
/**
14+
* Creates buy request that can be used for working with cart items
15+
*/
16+
class CreateBuyRequest
17+
{
18+
/**
19+
* @var DataObjectFactory
20+
*/
21+
private $dataObjectFactory;
22+
23+
/**
24+
* @param DataObjectFactory $dataObjectFactory
25+
*/
26+
public function __construct(
27+
DataObjectFactory $dataObjectFactory
28+
) {
29+
$this->dataObjectFactory = $dataObjectFactory;
30+
}
31+
32+
/**
33+
* Returns buy request for working with cart items
34+
*
35+
* @param float $qty
36+
* @param array $customizableOptionsData
37+
* @return DataObject
38+
*/
39+
public function execute(float $qty, array $customizableOptionsData): DataObject
40+
{
41+
$customizableOptions = [];
42+
foreach ($customizableOptionsData as $customizableOption) {
43+
if (isset($customizableOption['value_string'])) {
44+
$customizableOptions[$customizableOption['id']] = $this->convertCustomOptionValue(
45+
$customizableOption['value_string']
46+
);
47+
}
48+
}
49+
50+
return $this->dataObjectFactory->create([
51+
'data' => [
52+
'qty' => $qty,
53+
'options' => $customizableOptions,
54+
],
55+
]);
56+
}
57+
58+
/**
59+
* Convert custom options value
60+
*
61+
* @param string $value
62+
* @return string|array
63+
*/
64+
private function convertCustomOptionValue(string $value)
65+
{
66+
$value = trim($value);
67+
if (substr($value, 0, 1) === "[" &&
68+
substr($value, strlen($value) - 1, 1) === "]") {
69+
return explode(',', substr($value, 1, -1));
70+
}
71+
return $value;
72+
}
73+
}

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

Lines changed: 16 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
namespace Magento\QuoteGraphQl\Model\Cart;
99

10-
use Magento\Framework\DataObject;
11-
use Magento\Framework\DataObjectFactory;
1210
use Magento\Framework\Exception\LocalizedException;
1311
use Magento\Framework\Exception\NoSuchEntityException;
1412
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
@@ -24,6 +22,11 @@
2422
*/
2523
class UpdateCartItem
2624
{
25+
/**
26+
* @var CreateBuyRequest
27+
*/
28+
private $createBuyRequest;
29+
2730
/**
2831
* @var CartRepositoryInterface
2932
*/
@@ -35,56 +38,44 @@ class UpdateCartItem
3538
private $cartItemRepository;
3639

3740
/**
38-
* @var DataObjectFactory
39-
*/
40-
private $dataObjectFactory;
41-
42-
/**
43-
* @param DataObjectFactory $dataObjectFactory
4441
* @param CartItemRepositoryInterface $cartItemRepository
4542
* @param CartRepositoryInterface $quoteRepository
43+
* @param CreateBuyRequest $createBuyRequest
4644
*/
4745
public function __construct(
48-
DataObjectFactory $dataObjectFactory,
4946
CartItemRepositoryInterface $cartItemRepository,
50-
CartRepositoryInterface $quoteRepository
47+
CartRepositoryInterface $quoteRepository,
48+
CreateBuyRequest $createBuyRequest
5149
) {
52-
$this->dataObjectFactory = $dataObjectFactory;
5350
$this->cartItemRepository = $cartItemRepository;
5451
$this->quoteRepository = $quoteRepository;
52+
$this->createBuyRequest = $createBuyRequest;
5553
}
5654

5755
/**
5856
* Update cart item
5957
*
6058
* @param Quote $cart
6159
* @param int $cartItemId
62-
* @param float $qty
60+
* @param float $quantity
6361
* @param array $customizableOptionsData
6462
* @return void
6563
* @throws GraphQlInputException
6664
* @throws GraphQlNoSuchEntityException
6765
* @throws NoSuchEntityException
6866
*/
69-
public function execute(Quote $cart, int $cartItemId, float $qty, array $customizableOptionsData): void
67+
public function execute(Quote $cart, int $cartItemId, float $quantity, array $customizableOptionsData): void
7068
{
7169
if (count($customizableOptionsData) === 0) { // Update only item's qty
72-
$this->updateItemQty($cartItemId, $cart, $qty);
70+
$this->updateItemQuantity($cartItemId, $cart, $quantity);
7371

7472
return;
7573
}
7674

77-
$customizableOptions = [];
78-
foreach ($customizableOptionsData as $customizableOption) {
79-
$customizableOptions[$customizableOption['id']] = $this->convertCustomOptionValue(
80-
$customizableOption['value_string']
81-
);
82-
}
83-
8475
try {
8576
$result = $cart->updateItem(
8677
$cartItemId,
87-
$this->createBuyRequest($qty, $customizableOptions)
78+
$this->createBuyRequest->execute($quantity, $customizableOptionsData)
8879
);
8980
} catch (LocalizedException $e) {
9081
throw new GraphQlInputException(
@@ -117,20 +108,20 @@ public function execute(Quote $cart, int $cartItemId, float $qty, array $customi
117108
*
118109
* @param int $itemId
119110
* @param Quote $cart
120-
* @param float $qty
111+
* @param float $quantity
121112
* @throws GraphQlNoSuchEntityException
122113
* @throws NoSuchEntityException
123114
* @throws GraphQlNoSuchEntityException
124115
*/
125-
private function updateItemQty(int $itemId, Quote $cart, float $qty)
116+
private function updateItemQuantity(int $itemId, Quote $cart, float $quantity)
126117
{
127118
$cartItem = $cart->getItemById($itemId);
128119
if ($cartItem === false) {
129120
throw new GraphQlNoSuchEntityException(
130121
__('Could not find cart item with id: %1.', $itemId)
131122
);
132123
}
133-
$cartItem->setQty($qty);
124+
$cartItem->setQty($quantity);
134125
$this->validateCartItem($cartItem);
135126
$this->cartItemRepository->save($cartItem);
136127
}
@@ -159,40 +150,4 @@ private function validateCartItem(Item $cartItem): void
159150
}
160151
}
161152
}
162-
163-
/**
164-
* Format GraphQl input data to a shape that buy request has
165-
*
166-
* @param float $qty
167-
* @param array $customOptions
168-
* @return DataObject
169-
*/
170-
private function createBuyRequest(float $qty, array $customOptions): DataObject
171-
{
172-
return $this->dataObjectFactory->create([
173-
'data' => [
174-
'qty' => $qty,
175-
'options' => $customOptions,
176-
],
177-
]);
178-
}
179-
180-
// TODO: Refactor the code duplication with addCartItem
181-
// TODO: Make a reusable logic that is shared between add to cart / change cart approaches
182-
183-
/**
184-
* Convert custom options vakue
185-
*
186-
* @param string $value
187-
* @return string|array
188-
*/
189-
private function convertCustomOptionValue(string $value)
190-
{
191-
$value = trim($value);
192-
if (substr($value, 0, 1) === "[" &&
193-
substr($value, strlen($value) - 1, 1) === "]") {
194-
return explode(',', substr($value, 1, -1));
195-
}
196-
return $value;
197-
}
198153
}

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/EditQuoteItemCustomOptionsTest.php renamed to dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/EditQuoteItemWithCustomOptionsTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
/**
2020
* Edit cart customizable options test
2121
*/
22-
class EditQuoteItemCustomOptionsTest extends GraphQlAbstract
22+
class EditQuoteItemWithCustomOptionsTest extends GraphQlAbstract
2323
{
2424
/**
2525
* @var ProductRepositoryInterface

0 commit comments

Comments
 (0)