Skip to content

Commit

Permalink
Introduced cart address resolver and data provider
Browse files Browse the repository at this point in the history
  • Loading branch information
rogyar committed Oct 22, 2018
1 parent 87cfb71 commit 97b519c
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Resolver\Address;

use Magento\Framework\Api\ExtensibleDataObjectConverter;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Api\Data\CartInterface;

/**
* Class AddressDataProvider
*
* Collect and return information about cart shipping and billing addresses
*/
class AddressDataProvider
{
/**
* @var ExtensibleDataObjectConverter
*/
private $dataObjectConverter;

/**
* AddressDataProvider constructor.
*
* @param ExtensibleDataObjectConverter $dataObjectConverter
*/
public function __construct(
ExtensibleDataObjectConverter $dataObjectConverter
) {
$this->dataObjectConverter = $dataObjectConverter;
}

/**
* Collect and return information about shipping and billing addresses
*
* @param CartInterface $cart
* @return array
*/
public function getCartAddresses(CartInterface $cart): array
{
$addressData = [];
$shippingAddress = $cart->getShippingAddress();
$billingAddress = $cart->getBillingAddress();

if ($shippingAddress) {
$shippingData = $this->dataObjectConverter->toFlatArray($shippingAddress, [], AddressInterface::class);
$shippingData['address_type'] = 'SHIPPING';
$shippingData['selected_shipping_method'] = [
'code' => $shippingAddress->getShippingMethod(),
'label' => $shippingAddress->getShippingDescription(),
'free_shipping' => $shippingAddress->getFreeShipping(),
];
$shippingData['items_weight'] = $shippingAddress->getWeight();
$shippingData['customer_notes'] = $shippingAddress->getCustomerNotes();
$addressData[] = $shippingData;
}

if ($billingAddress) {
$billingData = $this->dataObjectConverter->toFlatArray($billingAddress, [], AddressInterface::class);
$billingData['address_type'] = 'BILLING';
$addressData[] = $billingData;
}

return $addressData;
}
}
87 changes: 87 additions & 0 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/CartAddress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?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\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\MaskedQuoteIdToQuoteId;
use Magento\QuoteGraphQl\Model\Resolver\Address\AddressDataProvider;

/**
* @inheritdoc
*/
class CartAddress implements ResolverInterface
{
/**
* @var AddressDataProvider
*/
private $addressDataProvider;

/**
* @var CartRepositoryInterface
*/
private $cartRepository;

/**
* @var MaskedQuoteIdToQuoteId
*/
private $maskedQuoteIdToQuoteId;

/**
* CartAddress constructor.
*
* @param MaskedQuoteIdToQuoteId $maskedQuoteIdToQuoteId
* @param CartRepositoryInterface $cartRepository
* @param AddressDataProvider $addressDataProvider
*/
public function __construct(
MaskedQuoteIdToQuoteId $maskedQuoteIdToQuoteId,
CartRepositoryInterface $cartRepository,
AddressDataProvider $addressDataProvider
) {
$this->maskedQuoteIdToQuoteId = $maskedQuoteIdToQuoteId;
$this->cartRepository = $cartRepository;
$this->addressDataProvider = $addressDataProvider;
}

/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
if (!isset($value['cart_id'])) {
// TODO: consider the possibility to pass quote model instead od quote ID
throw new LocalizedException(__('"cart_id" value should be specified'));
}

try {
$quoteId = $this->maskedQuoteIdToQuoteId->execute($value['cart_id']);
} catch (NoSuchEntityException $exception) {
throw new GraphQlNoSuchEntityException(
__('Could not find a cart with ID "%masked_cart_id"', ['masked_cart_id' => $value['cart_id']])
);
}

// TODO: should we check customer permissions here as well?
try {
$quote = $this->cartRepository->get($quoteId);
} catch (NoSuchEntityException $exception) {
throw new GraphQlNoSuchEntityException(
__('Could not find a cart with ID "%quote_id"', ['quote_id' => $quoteId])
);
}

return $this->addressDataProvider->getCartAddresses($quote);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
throw new GraphQlInputException(__($exception->getMessage()));
}

return 'Success!'; // TODO we should return cart here
return [
'cart' => [
'cart_id' => $maskedCartId
]
];
}
}
22 changes: 11 additions & 11 deletions app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ type SetShippingAddressesOnCartOutput {
}

type SetShippingMethodsOnCartOutput {
cart: String #TODO: temp placeholder, should be Cart!
cart: Cart!
}

# If no address is provided, the system get address assigned to a quote
Expand All @@ -92,28 +92,28 @@ type ApplyCouponToCartOutput {
}

type Cart {
cart_id: String
items: [CartItemInterface]
applied_coupon: AppliedCoupon
addresses: [CartAddress]!
addresses: [CartAddress]! @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\CartAddress")
}

type CartAddress {
firstname: String!
lastname: String!
firstname: String
lastname: String
company: String
street: [String!]!
city: String!
street: [String]
city: String
region: CartAddressRegion
postcode: String
country: CartAddressCountry!
telephone: String!
address_type: AdressTypeEnum!
country: CartAddressCountry
telephone: String
address_type: AdressTypeEnum
selected_shipping_method: CheckoutShippingMethod
available_shipping_methods: [CheckoutShippingMethod]!
available_shipping_methods: [CheckoutShippingMethod]
items_weight: Float
customer_notes: String
cart_items: [CartItemQuantity]
applied_coupon: AppliedCoupon
}

type CartItemQuantity {
Expand Down

0 comments on commit 97b519c

Please sign in to comment.