Skip to content

Commit 3522857

Browse files
committed
GraphQL-37: [Cart Operations] Manage Cart Items
-- Refactoring
1 parent c9d6491 commit 3522857

File tree

6 files changed

+343
-221
lines changed

6 files changed

+343
-221
lines changed
Lines changed: 77 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,118 @@
11
<?php
2-
declare(strict_types=1);
32
/**
43
* Copyright © Magento, Inc. All rights reserved.
54
* See COPYING.txt for license details.
65
*/
6+
declare(strict_types=1);
7+
78
namespace Magento\QuoteGraphQl\Model\Resolver;
89

10+
use Magento\Framework\Exception\LocalizedException;
911
use Magento\Framework\Exception\NoSuchEntityException;
1012
use Magento\Framework\GraphQl\Config\Element\Field;
1113
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
1214
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1315
use Magento\Framework\GraphQl\Query\ResolverInterface;
1416
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
15-
use Magento\Quote\Api\GuestCartRepositoryInterface;
16-
use Magento\QuoteGraphQl\Model\Cart\ExtractDataFromCart;
17-
use Magento\Framework\Stdlib\ArrayManager;
18-
use Magento\QuoteGraphQl\Model\Cart\UpdateCartItems as UpdateCartItemsService;
17+
use Magento\Quote\Api\CartItemRepositoryInterface;
18+
use Magento\Quote\Model\Quote;
19+
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
1920

21+
/**
22+
* @inheritdoc
23+
*/
2024
class UpdateCartItems implements ResolverInterface
2125
{
2226
/**
23-
* @var ExtractDataFromCart
27+
* @var GetCartForUser
2428
*/
25-
private $extractDataFromCart;
29+
private $getCartForUser;
2630

2731
/**
28-
* @var ArrayManager
32+
* @var CartItemRepositoryInterface
2933
*/
30-
private $arrayManager;
34+
private $cartItemRepository;
3135

3236
/**
33-
* @var UpdateCartItemsService
34-
*/
35-
private $updateCartItems;
36-
37-
/**
38-
* @param ExtractDataFromCart $extractDataFromCart
39-
* @param ArrayManager $arrayManager
40-
* @param UpdateCartItemsService $updateCartItems
37+
* @param GetCartForUser $getCartForUser
38+
* @param CartItemRepositoryInterface $cartItemRepository
4139
*/
4240
public function __construct(
43-
ExtractDataFromCart $extractDataFromCart,
44-
ArrayManager $arrayManager,
45-
UpdateCartItemsService $updateCartItems
41+
GetCartForUser $getCartForUser,
42+
CartItemRepositoryInterface $cartItemRepository
4643
) {
47-
$this->extractDataFromCart = $extractDataFromCart;
48-
$this->arrayManager = $arrayManager;
49-
$this->updateCartItems = $updateCartItems;
44+
$this->getCartForUser = $getCartForUser;
45+
$this->cartItemRepository = $cartItemRepository;
5046
}
5147

48+
/**
49+
* @inheritdoc
50+
*/
5251
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
5352
{
54-
$cartItems = $this->arrayManager->get('input/cart_items', $args);
55-
$maskedCartId = $this->arrayManager->get('input/cart_id', $args);
56-
57-
if (!$maskedCartId) {
53+
if (!isset($args['input']['cart_id']) || empty($args['input']['cart_id'])) {
5854
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
5955
}
60-
if (!$cartItems) {
61-
throw new GraphQlInputException(__('Required parameter "cart_items " is missing'));
56+
$maskedCartId = $args['input']['cart_id'];
57+
58+
if (!isset($args['input']['cart_items']) || empty($args['input']['cart_items'])
59+
|| !is_array($args['input']['cart_items'])
60+
) {
61+
throw new GraphQlInputException(__('Required parameter "cart_items" is missing'));
6262
}
63+
$cartItems = $args['input']['cart_items'];
64+
65+
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
6366

6467
try {
65-
$cart = $this->updateCartItems->update($maskedCartId, $cartItems);
68+
$this->processCartItems($cart, $cartItems);
6669
} catch (NoSuchEntityException $e) {
67-
throw new GraphQlNoSuchEntityException(__($e->getMessage()));
70+
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
71+
} catch (LocalizedException $e) {
72+
throw new GraphQlInputException(__($e->getMessage()), $e);
6873
}
6974

70-
$cartData = $this->extractDataFromCart->execute($cart);
75+
return [
76+
'cart' => [
77+
'model' => $cart,
78+
],
79+
];
80+
}
81+
82+
/**
83+
* Process cart items
84+
*
85+
* @param Quote $cart
86+
* @param array $items
87+
* @throws GraphQlInputException
88+
* @throws LocalizedException
89+
*/
90+
private function processCartItems(Quote $cart, array $items): void
91+
{
92+
foreach ($items as $item) {
93+
if (!isset($item['cart_item_id']) || empty($item['cart_item_id'])) {
94+
throw new GraphQlInputException(__('Required parameter "cart_item_id" for "cart_items" is missing.'));
95+
}
96+
$itemId = $item['cart_item_id'];
97+
98+
if (!isset($item['quantity'])) {
99+
throw new GraphQlInputException(__('Required parameter "quantity" for "cart_items" is missing.'));
100+
}
101+
$qty = (float)$item['quantity'];
71102

72-
return ['cart' => array_merge(['cart_id' => $maskedCartId], $cartData)];
103+
$cartItem = $cart->getItemById($itemId);
104+
if ($cartItem === false) {
105+
throw new GraphQlNoSuchEntityException(
106+
__('Could not find cart item with id: %1.', $item['cart_item_id'])
107+
);
108+
}
109+
110+
if ($qty <= 0.0) {
111+
$this->cartItemRepository->deleteById((int)$cart->getId(), $itemId);
112+
} else {
113+
$cartItem->setQty($qty);
114+
$this->cartItemRepository->save($cartItem);
115+
}
116+
}
73117
}
74118
}

app/code/Magento/QuoteGraphQl/etc/schema.graphqls

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,7 @@ input ApplyCouponToCartInput {
5656

5757
input UpdateCartItemsInput {
5858
cart_id: String!
59-
cart_items: [UpdateCartItemInput!]!
60-
}
61-
62-
input UpdateCartItemInput {
63-
item_id: String!
64-
qty: Float!
59+
cart_items: [CartItemQuantityInput!]!
6560
}
6661

6762
input RemoveItemFromCartInput {

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/RemoveItemFromCartTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,20 +75,24 @@ public function testRemoveItemFromCart()
7575

7676
/**
7777
* @magentoApiDataFixture Magento/Customer/_files/customer.php
78+
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
7879
* @expectedException \Exception
7980
* @expectedExceptionMessage Could not find a cart with ID "non_existent_masked_id"
8081
*/
8182
public function testRemoveItemFromNonExistentCart()
8283
{
83-
$query = $this->prepareMutationQuery('non_existent_masked_id', 1);
84+
$quote = $this->quoteFactory->create();
85+
$this->quoteResource->load($quote, 'test_order_1', 'reserved_order_id');
86+
$itemId = (int)$quote->getItemByProduct($this->productRepository->get('simple'))->getId();
8487

88+
$query = $this->prepareMutationQuery('non_existent_masked_id', $itemId);
8589
$this->graphQlQuery($query, [], '', $this->getHeaderMap());
8690
}
8791

8892
/**
8993
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_address_saved.php
9094
*/
91-
public function testRemoveNotExistentItem()
95+
public function testRemoveNonExistentItem()
9296
{
9397
$quote = $this->quoteFactory->create();
9498
$this->quoteResource->load($quote, 'test_order_1', 'reserved_order_id');

0 commit comments

Comments
 (0)