Skip to content

Commit

Permalink
Logic for setting shipping method is moved to a separate class
Browse files Browse the repository at this point in the history
  • Loading branch information
rogyar committed Nov 2, 2018
1 parent 5c0bcfd commit c98c3d5
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,26 @@
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Resolver\ShippingMethod;
namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Checkout\Api\ShippingInformationManagementInterface;
use Magento\Checkout\Model\ShippingInformation;
use Magento\Framework\Exception\InputException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Exception\StateException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\Stdlib\ArrayManager;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\Quote\AddressFactory as QuoteAddressFactory;
use Magento\Quote\Model\ResourceModel\Quote\Address as QuoteAddressResource;
use Magento\Checkout\Model\ShippingInformationFactory;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
use Magento\Checkout\Api\ShippingInformationManagementInterface;
use Magento\Checkout\Model\ShippingInformation;

/**
* Class SetShippingMethodsOnCart
*
* Mutation resolver for setting shipping methods for shopping cart
* Set shipping method for a specified shopping cart address
*/
class SetShippingMethodsOnCart implements ResolverInterface
class SetShippingMethodOnCart
{
/**
* @var ShippingInformationFactory
Expand All @@ -45,87 +41,52 @@ class SetShippingMethodsOnCart implements ResolverInterface
*/
private $quoteAddressResource;

/**
* @var ArrayManager
*/
private $arrayManager;

/**
* @var GetCartForUser
*/
private $getCartForUser;

/**
* @var ShippingInformationManagementInterface
*/
private $shippingInformationManagement;

/**
* SetShippingMethodsOnCart constructor.
* @param ArrayManager $arrayManager
* @param GetCartForUser $getCartForUser
* @param ShippingInformationManagementInterface $shippingInformationManagement
* @param QuoteAddressFactory $quoteAddressFactory
* @param QuoteAddressResource $quoteAddressResource
* @param ShippingInformationFactory $shippingInformationFactory
*/
public function __construct(
ArrayManager $arrayManager,
GetCartForUser $getCartForUser,
ShippingInformationManagementInterface $shippingInformationManagement,
QuoteAddressFactory $quoteAddressFactory,
QuoteAddressResource $quoteAddressResource,
ShippingInformationFactory $shippingInformationFactory
) {
$this->arrayManager = $arrayManager;
$this->getCartForUser = $getCartForUser;
$this->shippingInformationManagement = $shippingInformationManagement;
$this->quoteAddressResource = $quoteAddressResource;
$this->quoteAddressFactory = $quoteAddressFactory;
$this->shippingInformationFactory = $shippingInformationFactory;
}

/**
* @inheritdoc
* Sets shipping method for a specified shopping cart address
*
* @param Quote $cart
* @param int $cartAddressId
* @param string $carrierCode
* @param string $methodCode
* @throws GraphQlInputException
* @throws GraphQlNoSuchEntityException
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
public function execute(Quote $cart, int $cartAddressId, string $carrierCode, string $methodCode): void
{
$shippingMethods = $this->arrayManager->get('input/shipping_methods', $args);
$maskedCartId = $this->arrayManager->get('input/cart_id', $args);

if (!$maskedCartId) {
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
}
if (!$shippingMethods) {
throw new GraphQlInputException(__('Required parameter "shipping_methods" is missing'));
}

$shippingMethod = reset($shippingMethods);

if (!$shippingMethod['cart_address_id']) {
throw new GraphQlInputException(__('Required parameter "cart_address_id" is missing'));
}
if (!$shippingMethod['shipping_carrier_code']) {
throw new GraphQlInputException(__('Required parameter "shipping_carrier_code" is missing'));
}
if (!$shippingMethod['shipping_method_code']) {
throw new GraphQlInputException(__('Required parameter "shipping_method_code" is missing'));
}

$userId = $context->getUserId();
$cart = $this->getCartForUser->execute((string) $maskedCartId, $userId);

$quoteAddress = $this->quoteAddressFactory->create();
$this->quoteAddressResource->load($quoteAddress, $shippingMethod['cart_address_id']);
$this->quoteAddressResource->load($quoteAddress, $cartAddressId);

/** @var ShippingInformation $shippingInformation */
$shippingInformation = $this->shippingInformationFactory->create();

/* If the address is not a shipping address (but billing) the system will find the proper shipping address for
the selected cart and set the information there (actual for single shipping address) */
$shippingInformation->setShippingAddress($quoteAddress);
$shippingInformation->setShippingCarrierCode($shippingMethod['shipping_carrier_code']);
$shippingInformation->setShippingMethodCode($shippingMethod['shipping_method_code']);
$shippingInformation->setShippingCarrierCode($carrierCode);
$shippingInformation->setShippingMethodCode($methodCode);

try {
$this->shippingInformationManagement->saveAddressInformation($cart->getId(), $shippingInformation);
Expand All @@ -136,12 +97,5 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
} catch (InputException $exception) {
throw new GraphQlInputException(__($exception->getMessage()));
}

return [
'cart' => [
'cart_id' => $maskedCartId,
'model' => $cart
]
];
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Resolver;

use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\Stdlib\ArrayManager;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
use Magento\QuoteGraphQl\Model\Cart\SetShippingMethodOnCart;

/**
* Class SetShippingMethodsOnCart
*
* Mutation resolver for setting shipping methods for shopping cart
*/
class SetShippingMethodsOnCart implements ResolverInterface
{
/**
* @var SetShippingMethodOnCart
*/
private $setShippingMethodOnCart;

/**
* @var ArrayManager
*/
private $arrayManager;

/**
* @var GetCartForUser
*/
private $getCartForUser;

/**
* @param ArrayManager $arrayManager
* @param GetCartForUser $getCartForUser
* @param SetShippingMethodOnCart $setShippingMethodOnCart
*/
public function __construct(
ArrayManager $arrayManager,
GetCartForUser $getCartForUser,
SetShippingMethodOnCart $setShippingMethodOnCart
) {
$this->arrayManager = $arrayManager;
$this->getCartForUser = $getCartForUser;
$this->setShippingMethodOnCart = $setShippingMethodOnCart;
}

/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
$shippingMethods = $this->arrayManager->get('input/shipping_methods', $args);
$maskedCartId = $this->arrayManager->get('input/cart_id', $args);

if (!$maskedCartId) {
throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
}
if (!$shippingMethods) {
throw new GraphQlInputException(__('Required parameter "shipping_methods" is missing'));
}

$shippingMethod = reset($shippingMethods); // This point can be extended for multishipping

if (!$shippingMethod['cart_address_id']) {
throw new GraphQlInputException(__('Required parameter "cart_address_id" is missing'));
}
if (!$shippingMethod['shipping_carrier_code']) {
throw new GraphQlInputException(__('Required parameter "shipping_carrier_code" is missing'));
}
if (!$shippingMethod['shipping_method_code']) {
throw new GraphQlInputException(__('Required parameter "shipping_method_code" is missing'));
}

$userId = $context->getUserId();
$cart = $this->getCartForUser->execute((string) $maskedCartId, $userId);

$this->setShippingMethodOnCart->execute(
$cart,
$shippingMethod['cart_address_id'],
$shippingMethod['shipping_carrier_code'],
$shippingMethod['shipping_method_code']
);

return [
'cart' => [
'cart_id' => $maskedCartId,
'model' => $cart
]
];
}
}
2 changes: 1 addition & 1 deletion app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Mutation {
removeCouponFromCart(input: RemoveCouponFromCartInput): RemoveCouponFromCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\RemoveCouponFromCart")
setShippingAddressesOnCart(input: SetShippingAddressesOnCartInput): SetShippingAddressesOnCartOutput
setBillingAddressOnCart(input: SetBillingAddressOnCartInput): SetBillingAddressOnCartOutput
setShippingMethodsOnCart(input: SetShippingMethodsOnCartInput): SetShippingMethodsOnCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\ShippingMethod\\SetShippingMethodsOnCart")
setShippingMethodsOnCart(input: SetShippingMethodsOnCartInput): SetShippingMethodsOnCartOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetShippingMethodsOnCart")
addSimpleProductsToCart(input: AddSimpleProductsToCartInput): AddSimpleProductsToCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AddSimpleProductsToCart")
}

Expand Down

0 comments on commit c98c3d5

Please sign in to comment.