|
1 | 1 | <?php
|
2 |
| -declare(strict_types=1); |
3 | 2 | /**
|
4 | 3 | * Copyright © Magento, Inc. All rights reserved.
|
5 | 4 | * See COPYING.txt for license details.
|
6 | 5 | */
|
| 6 | +declare(strict_types=1); |
| 7 | + |
7 | 8 | namespace Magento\QuoteGraphQl\Model\Resolver;
|
8 | 9 |
|
| 10 | +use Magento\Framework\Exception\LocalizedException; |
9 | 11 | use Magento\Framework\Exception\NoSuchEntityException;
|
10 | 12 | use Magento\Framework\GraphQl\Config\Element\Field;
|
11 | 13 | use Magento\Framework\GraphQl\Exception\GraphQlInputException;
|
12 | 14 | use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
|
13 | 15 | use Magento\Framework\GraphQl\Query\ResolverInterface;
|
14 | 16 | 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; |
19 | 20 |
|
| 21 | +/** |
| 22 | + * @inheritdoc |
| 23 | + */ |
20 | 24 | class UpdateCartItems implements ResolverInterface
|
21 | 25 | {
|
22 | 26 | /**
|
23 |
| - * @var ExtractDataFromCart |
| 27 | + * @var GetCartForUser |
24 | 28 | */
|
25 |
| - private $extractDataFromCart; |
| 29 | + private $getCartForUser; |
26 | 30 |
|
27 | 31 | /**
|
28 |
| - * @var ArrayManager |
| 32 | + * @var CartItemRepositoryInterface |
29 | 33 | */
|
30 |
| - private $arrayManager; |
| 34 | + private $cartItemRepository; |
31 | 35 |
|
32 | 36 | /**
|
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 |
41 | 39 | */
|
42 | 40 | public function __construct(
|
43 |
| - ExtractDataFromCart $extractDataFromCart, |
44 |
| - ArrayManager $arrayManager, |
45 |
| - UpdateCartItemsService $updateCartItems |
| 41 | + GetCartForUser $getCartForUser, |
| 42 | + CartItemRepositoryInterface $cartItemRepository |
46 | 43 | ) {
|
47 |
| - $this->extractDataFromCart = $extractDataFromCart; |
48 |
| - $this->arrayManager = $arrayManager; |
49 |
| - $this->updateCartItems = $updateCartItems; |
| 44 | + $this->getCartForUser = $getCartForUser; |
| 45 | + $this->cartItemRepository = $cartItemRepository; |
50 | 46 | }
|
51 | 47 |
|
| 48 | + /** |
| 49 | + * @inheritdoc |
| 50 | + */ |
52 | 51 | public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
|
53 | 52 | {
|
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'])) { |
58 | 54 | throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
|
59 | 55 | }
|
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')); |
62 | 62 | }
|
| 63 | + $cartItems = $args['input']['cart_items']; |
| 64 | + |
| 65 | + $cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId()); |
63 | 66 |
|
64 | 67 | try {
|
65 |
| - $cart = $this->updateCartItems->update($maskedCartId, $cartItems); |
| 68 | + $this->processCartItems($cart, $cartItems); |
66 | 69 | } 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); |
68 | 73 | }
|
69 | 74 |
|
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']; |
71 | 102 |
|
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 | + } |
73 | 117 | }
|
74 | 118 | }
|
0 commit comments