Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.
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
80 changes: 80 additions & 0 deletions app/code/Magento/QuoteGraphQl/Model/Cart/GetShippingAddress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\GraphQl\Model\Query\ContextInterface;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Quote\Model\Quote\Address;

/**
* Get shipping address
*/
class GetShippingAddress
{
/**
* @var QuoteAddressFactory
*/
private $quoteAddressFactory;

/**
* @param QuoteAddressFactory $quoteAddressFactory
*/
public function __construct(QuoteAddressFactory $quoteAddressFactory)
{
$this->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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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);
}
Expand Down