Skip to content

Commit

Permalink
magento/graphql-ce:#468 - Resolve coupling between objects in `\Magen…
Browse files Browse the repository at this point in the history
…to\QuoteGraphQl\Model\Cart\SetBillingAddressOnCart`
  • Loading branch information
Vasilii committed Mar 16, 2019
1 parent 7b1cd79 commit b3280d0
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

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\Quote\Model\Quote\Address;
use Magento\Framework\GraphQl\Query\Resolver\ContextInterface;

/**
* Creates a quote address based on given context, customer address ID and customer address
*/
class CreateQuoteAddressByCustomerAddress
{
/**
* @var QuoteAddressFactory
*/
private $quoteAddressFactory;

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

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

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

/**
* @param ContextInterface $context
* @param int|string|null $customerAddressId
* @param array|null $customerAddress
* @return Address
* @throws GraphQlAuthenticationException
* @throws GraphQlAuthorizationException
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
*/
public function execute(
ContextInterface $context,
$customerAddressId,
$customerAddress
): Address {
if (null === $customerAddressId) {
return $this->quoteAddressFactory->createBasedOnInputData($customerAddress);
}

$customer = $this->getCustomer->execute($context);
$customerAddress = $this->getCustomerAddress->execute((int)$customerAddressId, (int)$customer->getId());

return $this->quoteAddressFactory->createBasedOnCustomerAddress($customerAddress);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

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 @@ -20,41 +20,25 @@
class SetBillingAddressOnCart
{
/**
* @var QuoteAddressFactory
*/
private $quoteAddressFactory;

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

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

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

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

/**
Expand All @@ -66,6 +50,8 @@ public function __construct(
* @return void
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
* @throws GraphQlAuthorizationException
* @throws GraphQlAuthenticationException
*/
public function execute(ContextInterface $context, CartInterface $cart, array $billingAddressInput): void
{
Expand Down Expand Up @@ -93,13 +79,11 @@ public function execute(ContextInterface $context, CartInterface $cart, array $b
);
}

if (null === $customerAddressId) {
$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->createQuoteAddressByCustomerAddress->execute(
$context,
$customerAddressId,
$addressInput
);

$this->assignBillingAddressToCart->execute($cart, $billingAddress, $useForShipping);
}
Expand Down

0 comments on commit b3280d0

Please sign in to comment.