Skip to content

Commit

Permalink
GraphQL-468: Resolve coupling between objects in `\Magento\QuoteGraph…
Browse files Browse the repository at this point in the history
…Ql\Model\Cart\SetBillingAddressOnCart`
  • Loading branch information
naydav committed Apr 3, 2019
1 parent ace616f commit 5b03488
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 102 deletions.

This file was deleted.

25 changes: 20 additions & 5 deletions app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@

namespace Magento\QuoteGraphQl\Model\Cart;

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

Expand All @@ -23,13 +25,21 @@ class QuoteAddressFactory
*/
private $quoteAddressFactory;

/**
* @var GetCustomerAddress
*/
private $getCustomerAddress;

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

/**
Expand All @@ -48,14 +58,19 @@ public function createBasedOnInputData(array $addressInput): QuoteAddress
}

/**
* Create QuoteAddress based on CustomerAddress
* Create Quote Address based on Customer Address
*
* @param CustomerAddress $customerAddress
* @param int $customerAddressId
* @param int $customerId
* @return QuoteAddress
* @throws GraphQlInputException
* @throws GraphQlAuthorizationException
* @throws GraphQlNoSuchEntityException
*/
public function createBasedOnCustomerAddress(CustomerAddress $customerAddress): QuoteAddress
public function createBasedOnCustomerAddress(int $customerAddressId, int $customerId): QuoteAddress
{
$customerAddress = $this->getCustomerAddress->execute((int)$customerAddressId, $customerId);

$quoteAddress = $this->quoteAddressFactory->create();
try {
$quoteAddress->importCustomerAddressData($customerAddress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
Expand All @@ -20,25 +21,33 @@
class SetBillingAddressOnCart
{
/**
* @var AssignBillingAddressToCart
* @var QuoteAddressFactory
*/
private $assignBillingAddressToCart;
private $quoteAddressFactory;

/**
* @var CreateQuoteAddressByCustomerAddress
* @var GetCustomer
*/
private $createQuoteAddressByCustomerAddress;
private $getCustomer;

/**
* @var AssignBillingAddressToCart
*/
private $assignBillingAddressToCart;

/**
* @param QuoteAddressFactory $quoteAddressFactory
* @param GetCustomer $getCustomer
* @param AssignBillingAddressToCart $assignBillingAddressToCart
* @param CreateQuoteAddressByCustomerAddress $createQuoteAddressByCustomerAddress
*/
public function __construct(
AssignBillingAddressToCart $assignBillingAddressToCart,
CreateQuoteAddressByCustomerAddress $createQuoteAddressByCustomerAddress
QuoteAddressFactory $quoteAddressFactory,
GetCustomer $getCustomer,
AssignBillingAddressToCart $assignBillingAddressToCart
) {
$this->quoteAddressFactory = $quoteAddressFactory;
$this->getCustomer = $getCustomer;
$this->assignBillingAddressToCart = $assignBillingAddressToCart;
$this->createQuoteAddressByCustomerAddress = $createQuoteAddressByCustomerAddress;
}

/**
Expand All @@ -49,9 +58,9 @@ public function __construct(
* @param array $billingAddressInput
* @return void
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
* @throws GraphQlAuthorizationException
* @throws GraphQlAuthenticationException
* @throws GraphQlAuthorizationException
* @throws GraphQlNoSuchEntityException
*/
public function execute(ContextInterface $context, CartInterface $cart, array $billingAddressInput): void
{
Expand Down Expand Up @@ -79,11 +88,15 @@ public function execute(ContextInterface $context, CartInterface $cart, array $b
);
}

$billingAddress = $this->createQuoteAddressByCustomerAddress->execute(
$context,
$customerAddressId,
$addressInput
);
if (null === $customerAddressId) {
$billingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput);
} else {
$customer = $this->getCustomer->execute($context);
$billingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress(
(int)$customerAddressId,
(int)$customer->getId()
);
}

$this->assignBillingAddressToCart->execute($cart, $billingAddress, $useForShipping);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\CustomerGraphQl\Model\Customer\Address\GetCustomerAddress;
use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
Expand All @@ -28,11 +27,6 @@ class SetShippingAddressesOnCart implements SetShippingAddressesOnCartInterface
*/
private $getCustomer;

/**
* @var GetCustomerAddress
*/
private $getCustomerAddress;

/**
* @var AssignShippingAddressToCart
*/
Expand All @@ -41,18 +35,15 @@ class SetShippingAddressesOnCart implements SetShippingAddressesOnCartInterface
/**
* @param QuoteAddressFactory $quoteAddressFactory
* @param GetCustomer $getCustomer
* @param GetCustomerAddress $getCustomerAddress
* @param AssignShippingAddressToCart $assignShippingAddressToCart
*/
public function __construct(
QuoteAddressFactory $quoteAddressFactory,
GetCustomer $getCustomer,
GetCustomerAddress $getCustomerAddress,
AssignShippingAddressToCart $assignShippingAddressToCart
) {
$this->quoteAddressFactory = $quoteAddressFactory;
$this->getCustomer = $getCustomer;
$this->getCustomerAddress = $getCustomerAddress;
$this->assignShippingAddressToCart = $assignShippingAddressToCart;
}

Expand Down Expand Up @@ -86,8 +77,10 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s
$shippingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput);
} else {
$customer = $this->getCustomer->execute($context);
$customerAddress = $this->getCustomerAddress->execute((int)$customerAddressId, (int)$customer->getId());
$shippingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress($customerAddress);
$shippingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress(
(int)$customerAddressId,
(int)$customer->getId()
);
}

$this->assignShippingAddressToCart->execute($cart, $shippingAddress);
Expand Down

0 comments on commit 5b03488

Please sign in to comment.