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

Resolve coupling between objects in \Magento\QuoteGraphQl\Model\Cart\SetBillingAddressOnCart #488

Merged
merged 4 commits into from
Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,8 +7,9 @@

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\CustomerGraphQl\Model\Customer\Address\GetCustomerAddress;
use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
use Magento\Framework\GraphQl\Exception\GraphQlAuthenticationException;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;
Expand All @@ -29,11 +30,6 @@ class SetBillingAddressOnCart
*/
private $getCustomer;

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

/**
* @var AssignBillingAddressToCart
*/
Expand All @@ -42,18 +38,15 @@ class SetBillingAddressOnCart
/**
* @param QuoteAddressFactory $quoteAddressFactory
* @param GetCustomer $getCustomer
* @param GetCustomerAddress $getCustomerAddress
* @param AssignBillingAddressToCart $assignBillingAddressToCart
*/
public function __construct(
QuoteAddressFactory $quoteAddressFactory,
GetCustomer $getCustomer,
GetCustomerAddress $getCustomerAddress,
AssignBillingAddressToCart $assignBillingAddressToCart
) {
$this->quoteAddressFactory = $quoteAddressFactory;
$this->getCustomer = $getCustomer;
$this->getCustomerAddress = $getCustomerAddress;
$this->assignBillingAddressToCart = $assignBillingAddressToCart;
}

Expand All @@ -65,6 +58,8 @@ public function __construct(
* @param array $billingAddressInput
* @return void
* @throws GraphQlInputException
* @throws GraphQlAuthenticationException
* @throws GraphQlAuthorizationException
* @throws GraphQlNoSuchEntityException
*/
public function execute(ContextInterface $context, CartInterface $cart, array $billingAddressInput): void
Expand Down Expand Up @@ -97,8 +92,10 @@ public function execute(ContextInterface $context, CartInterface $cart, array $b
$billingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput);
} else {
$customer = $this->getCustomer->execute($context);
$customerAddress = $this->getCustomerAddress->execute((int)$customerAddressId, (int)$customer->getId());
$billingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress($customerAddress);
$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