SetBillingAddressOnCart.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\QuoteGraphQl\Model\Resolver;
  8. use Magento\Framework\GraphQl\Config\Element\Field;
  9. use Magento\Framework\GraphQl\Exception\GraphQlInputException;
  10. use Magento\Framework\GraphQl\Query\ResolverInterface;
  11. use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
  12. use Magento\Framework\Stdlib\ArrayManager;
  13. use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
  14. use Magento\QuoteGraphQl\Model\Cart\SetBillingAddressOnCart as SetBillingAddressOnCartModel;
  15. /**
  16. * Class SetBillingAddressOnCart
  17. *
  18. * Mutation resolver for setting billing address for shopping cart
  19. */
  20. class SetBillingAddressOnCart implements ResolverInterface
  21. {
  22. /**
  23. * @var GetCartForUser
  24. */
  25. private $getCartForUser;
  26. /**
  27. * @var ArrayManager
  28. */
  29. private $arrayManager;
  30. /**
  31. * @var SetBillingAddressOnCartModel
  32. */
  33. private $setBillingAddressOnCart;
  34. /**
  35. * @param GetCartForUser $getCartForUser
  36. * @param ArrayManager $arrayManager
  37. * @param SetBillingAddressOnCartModel $setBillingAddressOnCart
  38. */
  39. public function __construct(
  40. GetCartForUser $getCartForUser,
  41. ArrayManager $arrayManager,
  42. SetBillingAddressOnCartModel $setBillingAddressOnCart
  43. ) {
  44. $this->getCartForUser = $getCartForUser;
  45. $this->arrayManager = $arrayManager;
  46. $this->setBillingAddressOnCart = $setBillingAddressOnCart;
  47. }
  48. /**
  49. * @inheritdoc
  50. */
  51. public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
  52. {
  53. $billingAddress = $this->arrayManager->get('input/billing_address', $args);
  54. $maskedCartId = $this->arrayManager->get('input/cart_id', $args);
  55. if (!$maskedCartId) {
  56. throw new GraphQlInputException(__('Required parameter "cart_id" is missing'));
  57. }
  58. if (!$billingAddress) {
  59. throw new GraphQlInputException(__('Required parameter "billing_address" is missing'));
  60. }
  61. $maskedCartId = $args['input']['cart_id'];
  62. $cart = $this->getCartForUser->execute($maskedCartId, $context->getUserId());
  63. $this->setBillingAddressOnCart->execute($context, $cart, $billingAddress);
  64. return [
  65. 'cart' => [
  66. 'cart_id' => $maskedCartId,
  67. 'model' => $cart,
  68. ]
  69. ];
  70. }
  71. }