Skip to content

Commit 035c157

Browse files
committed
move part logic to other file
1 parent 120a105 commit 035c157

File tree

2 files changed

+153
-90
lines changed

2 files changed

+153
-90
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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\DataProvider;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
12+
use Magento\GiftMessage\Api\Data\MessageInterface;
13+
use Magento\GiftMessage\Api\ItemRepositoryInterface;
14+
use Magento\GiftMessage\Helper\Message as GiftMessageHelper;
15+
use Magento\Quote\Api\CartItemRepositoryInterface;
16+
use Magento\Quote\Model\Quote;
17+
use Magento\QuoteGraphQl\Model\Cart\UpdateCartItem;
18+
19+
/**
20+
* Class contain update cart items methods
21+
*/
22+
class UpdateCartItems
23+
{
24+
/**
25+
* @var CartItemRepositoryInterface
26+
*/
27+
private $cartItemRepository;
28+
29+
/**
30+
* @var UpdateCartItem
31+
*/
32+
private $updateCartItem;
33+
34+
/**
35+
* @var ItemRepositoryInterface
36+
*/
37+
private $itemRepository;
38+
39+
/**
40+
* @var GiftMessageHelper
41+
*/
42+
private $giftMessageHelper;
43+
44+
/**
45+
* @param CartItemRepositoryInterface $cartItemRepository
46+
* @param UpdateCartItem $updateCartItem
47+
* @param ItemRepositoryInterface $itemRepository
48+
* @param GiftMessageHelper $giftMessageHelper
49+
*/
50+
public function __construct(
51+
CartItemRepositoryInterface $cartItemRepository,
52+
UpdateCartItem $updateCartItem,
53+
ItemRepositoryInterface $itemRepository,
54+
GiftMessageHelper $giftMessageHelper
55+
) {
56+
$this->cartItemRepository = $cartItemRepository;
57+
$this->updateCartItem = $updateCartItem;
58+
$this->itemRepository = $itemRepository;
59+
$this->giftMessageHelper = $giftMessageHelper;
60+
}
61+
62+
/**
63+
* Process cart items
64+
*
65+
* @param Quote $cart
66+
* @param array $items
67+
* @throws GraphQlInputException
68+
* @throws LocalizedException
69+
*/
70+
public function processCartItems(Quote $cart, array $items): void
71+
{
72+
foreach ($items as $item) {
73+
if (empty($item['cart_item_id'])) {
74+
throw new GraphQlInputException(__('Required parameter "cart_item_id" for "cart_items" is missing.'));
75+
}
76+
77+
$itemId = (int)$item['cart_item_id'];
78+
$customizableOptions = $item['customizable_options'] ?? [];
79+
$cartItem = $cart->getItemById($itemId);
80+
81+
if ($cartItem && $cartItem->getParentItemId()) {
82+
throw new GraphQlInputException(__('Child items may not be updated.'));
83+
}
84+
85+
if (count($customizableOptions) === 0 && !isset($item['quantity'])) {
86+
throw new GraphQlInputException(__('Required parameter "quantity" for "cart_items" is missing.'));
87+
}
88+
89+
$quantity = (float)$item['quantity'];
90+
91+
if ($quantity <= 0.0) {
92+
$this->cartItemRepository->deleteById((int)$cart->getId(), $itemId);
93+
} else {
94+
$this->updateCartItem->execute($cart, $itemId, $quantity, $customizableOptions);
95+
}
96+
97+
if (!empty($item['gift_message'])) {
98+
try {
99+
if (!$this->giftMessageHelper->isMessagesAllowed('items', $cartItem)) {
100+
continue;
101+
}
102+
if (!$this->giftMessageHelper->isMessagesAllowed('item', $cartItem)) {
103+
continue;
104+
}
105+
106+
/** @var MessageInterface $giftItemMessage */
107+
$giftItemMessage = $this->itemRepository->get($cart->getEntityId(), $itemId);
108+
109+
if (empty($giftItemMessage)) {
110+
continue;
111+
}
112+
} catch (LocalizedException $exception) {
113+
throw new GraphQlInputException(__('Gift Message can not be updated.'));
114+
}
115+
116+
$this->updateGiftMessageForItem($cart, $giftItemMessage, $item, $itemId);
117+
}
118+
}
119+
}
120+
121+
/**
122+
* Update Gift Message for Quote item
123+
*
124+
* @param Quote $cart
125+
* @param MessageInterface $giftItemMessage
126+
* @param array $item
127+
* @param int $itemId
128+
*
129+
* @throws GraphQlInputException
130+
*/
131+
private function updateGiftMessageForItem(Quote $cart, MessageInterface $giftItemMessage, array $item, int $itemId)
132+
{
133+
try {
134+
$giftItemMessage->setRecipient($item['gift_message']['to']);
135+
$giftItemMessage->setSender($item['gift_message']['from']);
136+
$giftItemMessage->setMessage($item['gift_message']['message']);
137+
$this->itemRepository->save($cart->getEntityId(), $giftItemMessage, $itemId);
138+
} catch (LocalizedException $exception) {
139+
throw new GraphQlInputException(__('Gift Message can not be updated'));
140+
}
141+
}
142+
}

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

Lines changed: 11 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -14,62 +14,43 @@
1414
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1515
use Magento\Framework\GraphQl\Query\ResolverInterface;
1616
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
17-
use Magento\GiftMessage\Api\ItemRepositoryInterface;
18-
use Magento\Quote\Api\CartItemRepositoryInterface;
1917
use Magento\Quote\Api\CartRepositoryInterface;
20-
use Magento\Quote\Model\Quote;
2118
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
22-
use Magento\QuoteGraphQl\Model\Cart\UpdateCartItem;
19+
use Magento\QuoteGraphQl\Model\CartItem\DataProvider\UpdateCartItems as UpdateCartItemsProvider;
2320

2421
/**
2522
* @inheritdoc
2623
*/
2724
class UpdateCartItems implements ResolverInterface
2825
{
29-
/**
30-
* @var UpdateCartItem
31-
*/
32-
private $updateCartItem;
33-
3426
/**
3527
* @var GetCartForUser
3628
*/
3729
private $getCartForUser;
3830

39-
/**
40-
* @var CartItemRepositoryInterface
41-
*/
42-
private $cartItemRepository;
43-
4431
/**
4532
* @var CartRepositoryInterface
4633
*/
4734
private $cartRepository;
4835

4936
/**
50-
* @var ItemRepositoryInterface
37+
* @var UpdateCartItemsProvider
5138
*/
52-
private $itemRepository;
39+
private $updateCartItems;
5340

5441
/**
55-
* @param GetCartForUser $getCartForUser
56-
* @param CartItemRepositoryInterface $cartItemRepository
57-
* @param UpdateCartItem $updateCartItem
58-
* @param CartRepositoryInterface $cartRepository
59-
* @param ItemRepositoryInterface $itemRepository
42+
* @param GetCartForUser $getCartForUser
43+
* @param CartRepositoryInterface $cartRepository
44+
* @param UpdateCartItemsProvider $updateCartItems
6045
*/
6146
public function __construct(
6247
GetCartForUser $getCartForUser,
63-
CartItemRepositoryInterface $cartItemRepository,
64-
UpdateCartItem $updateCartItem,
6548
CartRepositoryInterface $cartRepository,
66-
ItemRepositoryInterface $itemRepository
49+
UpdateCartItemsProvider $updateCartItems
6750
) {
6851
$this->getCartForUser = $getCartForUser;
69-
$this->cartItemRepository = $cartItemRepository;
70-
$this->updateCartItem = $updateCartItem;
7152
$this->cartRepository = $cartRepository;
72-
$this->itemRepository = $itemRepository;
53+
$this->updateCartItems = $updateCartItems;
7354
}
7455

7556
/**
@@ -80,20 +61,21 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
8061
if (empty($args['input']['cart_id'])) {
8162
throw new GraphQlInputException(__('Required parameter "cart_id" is missing.'));
8263
}
64+
8365
$maskedCartId = $args['input']['cart_id'];
8466

8567
if (empty($args['input']['cart_items'])
8668
|| !is_array($args['input']['cart_items'])
8769
) {
8870
throw new GraphQlInputException(__('Required parameter "cart_items" is missing.'));
8971
}
90-
$cartItems = $args['input']['cart_items'];
9172

73+
$cartItems = $args['input']['cart_items'];
9274
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
9375
$cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId(), $storeId);
9476

9577
try {
96-
$this->processCartItems($cart, $cartItems);
78+
$this->updateCartItems->processCartItems($cart, $cartItems);
9779
$this->cartRepository->save($cart);
9880
} catch (NoSuchEntityException $e) {
9981
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
@@ -107,65 +89,4 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
10789
],
10890
];
10991
}
110-
111-
/**
112-
* Process cart items
113-
*
114-
* @param Quote $cart
115-
* @param array $items
116-
* @throws GraphQlInputException
117-
* @throws LocalizedException
118-
*/
119-
private function processCartItems(Quote $cart, array $items): void
120-
{
121-
foreach ($items as $item) {
122-
if (empty($item['cart_item_id'])) {
123-
throw new GraphQlInputException(__('Required parameter "cart_item_id" for "cart_items" is missing.'));
124-
}
125-
$itemId = (int)$item['cart_item_id'];
126-
$customizableOptions = $item['customizable_options'] ?? [];
127-
128-
$cartItem = $cart->getItemById($itemId);
129-
if ($cartItem && $cartItem->getParentItemId()) {
130-
throw new GraphQlInputException(__('Child items may not be updated.'));
131-
}
132-
133-
if (count($customizableOptions) === 0 && !isset($item['quantity'])) {
134-
throw new GraphQlInputException(__('Required parameter "quantity" for "cart_items" is missing.'));
135-
}
136-
$quantity = (float)$item['quantity'];
137-
138-
if ($quantity <= 0.0) {
139-
$this->cartItemRepository->deleteById((int)$cart->getId(), $itemId);
140-
} else {
141-
$this->updateCartItem->execute($cart, $itemId, $quantity, $customizableOptions);
142-
}
143-
144-
if (!empty($item['gift_message'])) {
145-
$this->updateGiftMessageForItem($cart, $item, $itemId);
146-
}
147-
}
148-
}
149-
150-
/**
151-
* Update Gift Message for Quote item
152-
*
153-
* @param Quote $cart
154-
* @param array $item
155-
* @param int $itemId
156-
*
157-
* @throws GraphQlInputException
158-
*/
159-
private function updateGiftMessageForItem(Quote $cart, array $item, int $itemId)
160-
{
161-
try {
162-
$giftItemMessage = $this->itemRepository->get($cart->getEntityId(), $itemId);
163-
$giftItemMessage->setRecipient($item['gift_message']['to']);
164-
$giftItemMessage->setSender($item['gift_message']['from']);
165-
$giftItemMessage->setMessage($item['gift_message']['message']);
166-
$this->itemRepository->save($cart->getEntityId(), $giftItemMessage, $itemId);
167-
} catch (LocalizedException $exception) {
168-
throw new GraphQlInputException(__('Gift Message can not be updated.'));
169-
}
170-
}
17192
}

0 commit comments

Comments
 (0)