Skip to content

Commit

Permalink
move part logic to other file
Browse files Browse the repository at this point in the history
  • Loading branch information
Usik2203 committed May 21, 2020
1 parent 120a105 commit 035c157
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 90 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\CartItem\DataProvider;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\GiftMessage\Api\Data\MessageInterface;
use Magento\GiftMessage\Api\ItemRepositoryInterface;
use Magento\GiftMessage\Helper\Message as GiftMessageHelper;
use Magento\Quote\Api\CartItemRepositoryInterface;
use Magento\Quote\Model\Quote;
use Magento\QuoteGraphQl\Model\Cart\UpdateCartItem;

/**
* Class contain update cart items methods
*/
class UpdateCartItems
{
/**
* @var CartItemRepositoryInterface
*/
private $cartItemRepository;

/**
* @var UpdateCartItem
*/
private $updateCartItem;

/**
* @var ItemRepositoryInterface
*/
private $itemRepository;

/**
* @var GiftMessageHelper
*/
private $giftMessageHelper;

/**
* @param CartItemRepositoryInterface $cartItemRepository
* @param UpdateCartItem $updateCartItem
* @param ItemRepositoryInterface $itemRepository
* @param GiftMessageHelper $giftMessageHelper
*/
public function __construct(
CartItemRepositoryInterface $cartItemRepository,
UpdateCartItem $updateCartItem,
ItemRepositoryInterface $itemRepository,
GiftMessageHelper $giftMessageHelper
) {
$this->cartItemRepository = $cartItemRepository;
$this->updateCartItem = $updateCartItem;
$this->itemRepository = $itemRepository;
$this->giftMessageHelper = $giftMessageHelper;
}

/**
* Process cart items
*
* @param Quote $cart
* @param array $items
* @throws GraphQlInputException
* @throws LocalizedException
*/
public function processCartItems(Quote $cart, array $items): void
{
foreach ($items as $item) {
if (empty($item['cart_item_id'])) {
throw new GraphQlInputException(__('Required parameter "cart_item_id" for "cart_items" is missing.'));
}

$itemId = (int)$item['cart_item_id'];
$customizableOptions = $item['customizable_options'] ?? [];
$cartItem = $cart->getItemById($itemId);

if ($cartItem && $cartItem->getParentItemId()) {
throw new GraphQlInputException(__('Child items may not be updated.'));
}

if (count($customizableOptions) === 0 && !isset($item['quantity'])) {
throw new GraphQlInputException(__('Required parameter "quantity" for "cart_items" is missing.'));
}

$quantity = (float)$item['quantity'];

if ($quantity <= 0.0) {
$this->cartItemRepository->deleteById((int)$cart->getId(), $itemId);
} else {
$this->updateCartItem->execute($cart, $itemId, $quantity, $customizableOptions);
}

if (!empty($item['gift_message'])) {
try {
if (!$this->giftMessageHelper->isMessagesAllowed('items', $cartItem)) {
continue;
}
if (!$this->giftMessageHelper->isMessagesAllowed('item', $cartItem)) {
continue;
}

/** @var MessageInterface $giftItemMessage */
$giftItemMessage = $this->itemRepository->get($cart->getEntityId(), $itemId);

if (empty($giftItemMessage)) {
continue;
}
} catch (LocalizedException $exception) {
throw new GraphQlInputException(__('Gift Message can not be updated.'));
}

$this->updateGiftMessageForItem($cart, $giftItemMessage, $item, $itemId);
}
}
}

/**
* Update Gift Message for Quote item
*
* @param Quote $cart
* @param MessageInterface $giftItemMessage
* @param array $item
* @param int $itemId
*
* @throws GraphQlInputException
*/
private function updateGiftMessageForItem(Quote $cart, MessageInterface $giftItemMessage, array $item, int $itemId)
{
try {
$giftItemMessage->setRecipient($item['gift_message']['to']);
$giftItemMessage->setSender($item['gift_message']['from']);
$giftItemMessage->setMessage($item['gift_message']['message']);
$this->itemRepository->save($cart->getEntityId(), $giftItemMessage, $itemId);
} catch (LocalizedException $exception) {
throw new GraphQlInputException(__('Gift Message can not be updated'));
}
}
}
101 changes: 11 additions & 90 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/UpdateCartItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,62 +14,43 @@
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\GiftMessage\Api\ItemRepositoryInterface;
use Magento\Quote\Api\CartItemRepositoryInterface;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\Quote;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
use Magento\QuoteGraphQl\Model\Cart\UpdateCartItem;
use Magento\QuoteGraphQl\Model\CartItem\DataProvider\UpdateCartItems as UpdateCartItemsProvider;

/**
* @inheritdoc
*/
class UpdateCartItems implements ResolverInterface
{
/**
* @var UpdateCartItem
*/
private $updateCartItem;

/**
* @var GetCartForUser
*/
private $getCartForUser;

/**
* @var CartItemRepositoryInterface
*/
private $cartItemRepository;

/**
* @var CartRepositoryInterface
*/
private $cartRepository;

/**
* @var ItemRepositoryInterface
* @var UpdateCartItemsProvider
*/
private $itemRepository;
private $updateCartItems;

/**
* @param GetCartForUser $getCartForUser
* @param CartItemRepositoryInterface $cartItemRepository
* @param UpdateCartItem $updateCartItem
* @param CartRepositoryInterface $cartRepository
* @param ItemRepositoryInterface $itemRepository
* @param GetCartForUser $getCartForUser
* @param CartRepositoryInterface $cartRepository
* @param UpdateCartItemsProvider $updateCartItems
*/
public function __construct(
GetCartForUser $getCartForUser,
CartItemRepositoryInterface $cartItemRepository,
UpdateCartItem $updateCartItem,
CartRepositoryInterface $cartRepository,
ItemRepositoryInterface $itemRepository
UpdateCartItemsProvider $updateCartItems
) {
$this->getCartForUser = $getCartForUser;
$this->cartItemRepository = $cartItemRepository;
$this->updateCartItem = $updateCartItem;
$this->cartRepository = $cartRepository;
$this->itemRepository = $itemRepository;
$this->updateCartItems = $updateCartItems;
}

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

$maskedCartId = $args['input']['cart_id'];

if (empty($args['input']['cart_items'])
|| !is_array($args['input']['cart_items'])
) {
throw new GraphQlInputException(__('Required parameter "cart_items" is missing.'));
}
$cartItems = $args['input']['cart_items'];

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

try {
$this->processCartItems($cart, $cartItems);
$this->updateCartItems->processCartItems($cart, $cartItems);
$this->cartRepository->save($cart);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()), $e);
Expand All @@ -107,65 +89,4 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
],
];
}

/**
* Process cart items
*
* @param Quote $cart
* @param array $items
* @throws GraphQlInputException
* @throws LocalizedException
*/
private function processCartItems(Quote $cart, array $items): void
{
foreach ($items as $item) {
if (empty($item['cart_item_id'])) {
throw new GraphQlInputException(__('Required parameter "cart_item_id" for "cart_items" is missing.'));
}
$itemId = (int)$item['cart_item_id'];
$customizableOptions = $item['customizable_options'] ?? [];

$cartItem = $cart->getItemById($itemId);
if ($cartItem && $cartItem->getParentItemId()) {
throw new GraphQlInputException(__('Child items may not be updated.'));
}

if (count($customizableOptions) === 0 && !isset($item['quantity'])) {
throw new GraphQlInputException(__('Required parameter "quantity" for "cart_items" is missing.'));
}
$quantity = (float)$item['quantity'];

if ($quantity <= 0.0) {
$this->cartItemRepository->deleteById((int)$cart->getId(), $itemId);
} else {
$this->updateCartItem->execute($cart, $itemId, $quantity, $customizableOptions);
}

if (!empty($item['gift_message'])) {
$this->updateGiftMessageForItem($cart, $item, $itemId);
}
}
}

/**
* Update Gift Message for Quote item
*
* @param Quote $cart
* @param array $item
* @param int $itemId
*
* @throws GraphQlInputException
*/
private function updateGiftMessageForItem(Quote $cart, array $item, int $itemId)
{
try {
$giftItemMessage = $this->itemRepository->get($cart->getEntityId(), $itemId);
$giftItemMessage->setRecipient($item['gift_message']['to']);
$giftItemMessage->setSender($item['gift_message']['from']);
$giftItemMessage->setMessage($item['gift_message']['message']);
$this->itemRepository->save($cart->getEntityId(), $giftItemMessage, $itemId);
} catch (LocalizedException $exception) {
throw new GraphQlInputException(__('Gift Message can not be updated.'));
}
}
}

0 comments on commit 035c157

Please sign in to comment.