diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/GetShippingAddress.php b/app/code/Magento/QuoteGraphQl/Model/Cart/GetShippingAddress.php new file mode 100644 index 000000000000..6e35091aecd7 --- /dev/null +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/GetShippingAddress.php @@ -0,0 +1,80 @@ +quoteAddressFactory = $quoteAddressFactory; + } + + /** + * Get Shipping Address based on the input. + * + * @param ContextInterface $context + * @param array $shippingAddressInput + * @return Address + * @throws GraphQlAuthorizationException + * @throws GraphQlInputException + * @throws GraphQlNoSuchEntityException + */ + public function execute(ContextInterface $context, array $shippingAddressInput): Address + { + $customerAddressId = $shippingAddressInput['customer_address_id'] ?? null; + $addressInput = $shippingAddressInput['address'] ?? null; + + if ($addressInput) { + $addressInput['customer_notes'] = $shippingAddressInput['customer_notes'] ?? ''; + } + + if (null === $customerAddressId && null === $addressInput) { + throw new GraphQlInputException( + __('The shipping address must contain either "customer_address_id" or "address".') + ); + } + + if ($customerAddressId && $addressInput) { + throw new GraphQlInputException( + __('The shipping address cannot contain "customer_address_id" and "address" at the same time.') + ); + } + + if (null === $customerAddressId) { + $shippingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput); + } else { + if (false === $context->getExtensionAttributes()->getIsCustomer()) { + throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.')); + } + + $shippingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress( + (int)$customerAddressId, + $context->getUserId() + ); + } + + return $shippingAddress; + } +} diff --git a/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php b/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php index 260f1343556f..d4f2853467b3 100644 --- a/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php +++ b/app/code/Magento/QuoteGraphQl/Model/Cart/SetShippingAddressesOnCart.php @@ -17,26 +17,25 @@ */ class SetShippingAddressesOnCart implements SetShippingAddressesOnCartInterface { - /** - * @var QuoteAddressFactory - */ - private $quoteAddressFactory; - /** * @var AssignShippingAddressToCart */ private $assignShippingAddressToCart; + /** + * @var GetShippingAddress + */ + private $getShippingAddress; /** - * @param QuoteAddressFactory $quoteAddressFactory * @param AssignShippingAddressToCart $assignShippingAddressToCart + * @param GetShippingAddress $getShippingAddress */ public function __construct( - QuoteAddressFactory $quoteAddressFactory, - AssignShippingAddressToCart $assignShippingAddressToCart + AssignShippingAddressToCart $assignShippingAddressToCart, + GetShippingAddress $getShippingAddress ) { - $this->quoteAddressFactory = $quoteAddressFactory; $this->assignShippingAddressToCart = $assignShippingAddressToCart; + $this->getShippingAddress = $getShippingAddress; } /** @@ -50,37 +49,8 @@ public function execute(ContextInterface $context, CartInterface $cart, array $s ); } $shippingAddressInput = current($shippingAddressesInput); - $customerAddressId = $shippingAddressInput['customer_address_id'] ?? null; - $addressInput = $shippingAddressInput['address'] ?? null; - - if ($addressInput) { - $addressInput['customer_notes'] = $shippingAddressInput['customer_notes'] ?? ''; - } - - if (null === $customerAddressId && null === $addressInput) { - throw new GraphQlInputException( - __('The shipping address must contain either "customer_address_id" or "address".') - ); - } - if ($customerAddressId && $addressInput) { - throw new GraphQlInputException( - __('The shipping address cannot contain "customer_address_id" and "address" at the same time.') - ); - } - - if (null === $customerAddressId) { - $shippingAddress = $this->quoteAddressFactory->createBasedOnInputData($addressInput); - } else { - if (false === $context->getExtensionAttributes()->getIsCustomer()) { - throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.')); - } - - $shippingAddress = $this->quoteAddressFactory->createBasedOnCustomerAddress( - (int)$customerAddressId, - $context->getUserId() - ); - } + $shippingAddress = $this->getShippingAddress->execute($context, $shippingAddressInput); $this->assignShippingAddressToCart->execute($cart, $shippingAddress); }