Skip to content
This repository has been archived by the owner on Dec 19, 2019. It is now read-only.

Provide available shipping rates for addresses #339

Merged
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

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,42 @@
*/
declare(strict_types=1);

namespace Magento\QuoteGraphQl\Model\Cart\Address\Mapper;
namespace Magento\QuoteGraphQl\Model\Cart;

use Magento\Framework\Api\ExtensibleDataObjectConverter;
use Magento\Quote\Api\Data\AddressInterface;
use Magento\Quote\Model\Quote\Address as QuoteAddress;

/**
* Class Address
*
* Extract the necessary address fields from an Address model
*/
class Address
class ExtractDataFromAddress
{
/**
* Converts Address model data to nested array
* @var ExtensibleDataObjectConverter
*/
private $dataObjectConverter;

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

/**
* Converts Address model to flat array
*
* @param QuoteAddress $address
* @return array
*/
public function toNestedArray(QuoteAddress $address): array
public function execute(QuoteAddress $address): array
{
$addressData = [
$addressData = $this->dataObjectConverter->toFlatArray($address, [], AddressInterface::class);
$addressData['model'] = $address;

$addressData = array_merge($addressData, [
'country' => [
'code' => $address->getCountryId(),
'label' => $address->getCountry()
Expand All @@ -41,7 +57,7 @@ public function toNestedArray(QuoteAddress $address): array
],
'items_weight' => $address->getWeight(),
'customer_notes' => $address->getCustomerNotes()
];
]);

if (!$address->hasItems()) {
return $addressData;
Expand Down
24 changes: 14 additions & 10 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/BillingAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,24 @@
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\QuoteGraphQl\Model\Cart\Address\BillingAddressDataProvider;
use Magento\QuoteGraphQl\Model\Cart\ExtractDataFromAddress;

/**
* @inheritdoc
*/
class BillingAddress implements ResolverInterface
{
/**
* @var BillingAddressDataProvider
* @var ExtractDataFromAddress
*/
private $addressDataProvider;
private $extractDataFromAddress;

/**
* @param BillingAddressDataProvider $addressDataProvider
* @param ExtractDataFromAddress $extractDataFromAddress
*/
public function __construct(
BillingAddressDataProvider $addressDataProvider
) {
$this->addressDataProvider = $addressDataProvider;
public function __construct(ExtractDataFromAddress $extractDataFromAddress)
{
$this->extractDataFromAddress = $extractDataFromAddress;
}

/**
Expand All @@ -40,9 +39,14 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
if (!isset($value['model'])) {
throw new LocalizedException(__('"model" value should be specified'));
}

$cart = $value['model'];

return $this->addressDataProvider->getCartAddresses($cart);
$billingAddress = $cart->getBillingAddress();
if (null === $billingAddress) {
return null;
}

$addressData = $this->extractDataFromAddress->execute($billingAddress);
return $addressData;
}
}
Loading