-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced cart address resolver and data provider
- Loading branch information
Showing
4 changed files
with
173 additions
and
12 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
app/code/Magento/QuoteGraphQl/Model/Resolver/Address/AddressDataProvider.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
87
app/code/Magento/QuoteGraphQl/Model/Resolver/CartAddress.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters