Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Commit

Permalink
GraphQL-37: [Cart Operations] Manage Cart Items
Browse files Browse the repository at this point in the history
-- Refactoring
  • Loading branch information
naydav committed Feb 28, 2019
1 parent 5070458 commit f0b8b7b
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 213 deletions.
67 changes: 67 additions & 0 deletions app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Customer\Api\Data\AddressInterface as CustomerAddress;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Quote\Model\Quote\Address as QuoteAddress;
use Magento\Quote\Model\Quote\AddressFactory as BaseQuoteAddressFactory;

/**
* Create QuoteAddress
*/
class QuoteAddressFactory
{
/**
* @var BaseQuoteAddressFactory
*/
private $quoteAddressFactory;

/**
* @param BaseQuoteAddressFactory $quoteAddressFactory
*/
public function __construct(
QuoteAddressFactory $quoteAddressFactory
) {
$this->quoteAddressFactory = $quoteAddressFactory;
}

/**
* Create QuoteAddress based on input data
*
* @param array $addressInput
* @return QuoteAddress
*/
public function createBasedOnInputData(array $addressInput): QuoteAddress
{
$addressInput['country_id'] = $addressInput['country_code'] ?? '';

$quoteAddress = $this->quoteAddressFactory->create();
$quoteAddress->addData($addressInput);
return $quoteAddress;
}

/**
* Create QuoteAddress based on CustomerAddress
*
* @param CustomerAddress $customerAddress
* @return QuoteAddress
* @throws GraphQlInputException
*/
public function createBasedOnCustomerAddress(CustomerAddress $customerAddress): QuoteAddress
{
$quoteAddress = $this->quoteAddressFactory->create();
try {
$quoteAddress->importCustomerAddressData($customerAddress);
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()));
}
return $quoteAddress;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Customer\Api\Data\AddressInterface as CustomerAddress;
use Magento\CustomerGraphQl\Model\Customer\CheckCustomerAccount;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Model\Quote\Address as QuoteAddress;
use Magento\Quote\Model\Quote\AddressFactory as QuoteAddressFactory;
use Magento\Quote\Api\BillingAddressManagementInterface;
use Magento\Quote\Model\Quote\Address as QuoteAddress;

/**
* Set billing address for a specified shopping cart
Expand Down Expand Up @@ -77,7 +75,8 @@ public function execute(ContextInterface $context, CartInterface $cart, array $b
{
$customerAddressId = $billingAddress['customer_address_id'] ?? null;
$addressInput = $billingAddress['address'] ?? null;
$useForShipping = $billingAddress['use_for_shipping'] ?? false;
$useForShipping = isset($billingAddress['use_for_shipping'])
? (bool)$billingAddress['use_for_shipping'] : false;

if (null === $customerAddressId && null === $addressInput) {
throw new GraphQlInputException(
Expand All @@ -99,48 +98,34 @@ public function execute(ContextInterface $context, CartInterface $cart, array $b
}

if (null === $customerAddressId) {
$billingAddress = $this->processNewAddress($addressInput);
$billingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput);
} else {
$this->checkCustomerAccount->execute($context->getUserId(), $context->getUserType());
$customerAddress = $this->getCustomerAddress->execute((int)$customerAddressId, (int)$context->getUserId());
$billingAddress = $this->processAddressFromAddressBook($customerAddress);
}

try {
$this->billingAddressManagement->assign($cart->getId(), $billingAddress, $useForShipping);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()));
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()));
$billingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress($customerAddress);
}
}

/**
* @param array $addressInput
* @return QuoteAddress
*/
private function processNewAddress(array $addressInput): QuoteAddress
{
$addressInput['country_id'] = $addressInput['country_code'] ?? '';

$quoteAddress = $this->quoteAddressFactory->create();
$quoteAddress->addData($addressInput);
return $quoteAddress;
$this->assignBillingAddressToCart($cart, $billingAddress, $useForShipping);
}

/**
* @param CustomerAddress $customerAddress
* @return QuoteAddress
* @param CartInterface $cart
* @param QuoteAddress $billingAddress
* @param bool $useForShipping
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
*/
private function processAddressFromAddressBook(CustomerAddress $customerAddress): QuoteAddress
{
$quoteAddress = $this->quoteAddressFactory->create();
private function assignBillingAddressToCart(
CartInterface $cart,
QuoteAddress $billingAddress,
bool $useForShipping
): void {
try {
$quoteAddress->importCustomerAddressData($customerAddress);
$this->billingAddressManagement->assign($cart->getId(), $billingAddress, $useForShipping);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()));
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()));
}
return $quoteAddress;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Customer\Api\Data\AddressInterface as CustomerAddress;
use Magento\CustomerGraphQl\Model\Customer\CheckCustomerAccount;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
Expand All @@ -16,7 +15,6 @@
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
use Magento\Quote\Api\Data\CartInterface;
use Magento\Quote\Model\Quote\Address as QuoteAddress;
use Magento\Quote\Model\Quote\AddressFactory as QuoteAddressFactory;
use Magento\Quote\Model\ShippingAddressManagementInterface;

/**
Expand Down Expand Up @@ -89,48 +87,32 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s
}

if (null === $customerAddressId) {
$shippingAddress = $this->processNewAddress($addressInput);
$shippingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput);
} else {
$this->checkCustomerAccount->execute($context->getUserId(), $context->getUserType());
$customerAddress = $this->getCustomerAddress->execute((int)$customerAddressId, (int)$context->getUserId());
$shippingAddress = $this->processAddressFromAddressBook($customerAddress);
$shippingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress($customerAddress);
}

try {
$this->shippingAddressManagement->assign($cart->getId(), $shippingAddress);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()));
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()));
}
$this->assignShippingAddressToCart($cart, $shippingAddress);
}

/**
* @param array $addressInput
* @return QuoteAddress
*/
private function processNewAddress(array $addressInput): QuoteAddress
{
$addressInput['country_id'] = $addressInput['country_code'] ?? '';

$quoteAddress = $this->quoteAddressFactory->create();
$quoteAddress->addData($addressInput);
return $quoteAddress;
}

/**
* @param CustomerAddress $customerAddress
* @return QuoteAddress
* @param CartInterface $cart
* @param QuoteAddress $shippingAddress
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
*/
private function processAddressFromAddressBook(CustomerAddress $customerAddress): QuoteAddress
{
$quoteAddress = $this->quoteAddressFactory->create();
private function assignShippingAddressToCart(
CartInterface $cart,
QuoteAddress $shippingAddress
): void {
try {
$quoteAddress->importCustomerAddressData($customerAddress);
$this->shippingAddressManagement->assign($cart->getId(), $shippingAddress);
} catch (NoSuchEntityException $e) {
throw new GraphQlNoSuchEntityException(__($e->getMessage()));
} catch (LocalizedException $e) {
throw new GraphQlInputException(__($e->getMessage()));
}
return $quoteAddress;
}
}
Loading

0 comments on commit f0b8b7b

Please sign in to comment.