Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 3b2e305

Browse files
authored
Merge pull request #3759 from magento-engcom/graphql-develop-prs
[EngCom] Public Pull Requests - GraphQL
2 parents 7afe974 + 2d09e02 commit 3b2e305

14 files changed

+436
-329
lines changed

app/code/Magento/QuoteGraphQl/Model/Cart/AddSimpleProductToCart.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,16 @@ public function execute(Quote $cart, array $cartItemData): void
7575
throw new GraphQlNoSuchEntityException(__('Could not find a product with SKU "%sku"', ['sku' => $sku]));
7676
}
7777

78-
$result = $cart->addProduct($product, $this->createBuyRequest($qty, $customizableOptions));
78+
try {
79+
$result = $cart->addProduct($product, $this->createBuyRequest($qty, $customizableOptions));
80+
} catch (\Exception $e) {
81+
throw new GraphQlInputException(
82+
__(
83+
'Could not add the product with SKU %sku to the shopping cart: %message',
84+
['sku' => $sku, 'message' => $e->getMessage()]
85+
)
86+
);
87+
}
7988

8089
if (is_string($result)) {
8190
throw new GraphQlInputException(__($result));

app/code/Magento/QuoteGraphQl/Model/Cart/Address/AddressDataProvider.php

Lines changed: 0 additions & 94 deletions
This file was deleted.

app/code/Magento/QuoteGraphQl/Model/Cart/Address/BillingAddressDataProvider.php

Lines changed: 0 additions & 72 deletions
This file was deleted.

app/code/Magento/QuoteGraphQl/Model/Cart/Address/ShippingAddressesDataProvider.php

Lines changed: 0 additions & 76 deletions
This file was deleted.

app/code/Magento/QuoteGraphQl/Model/Cart/Address/Mapper/Address.php renamed to app/code/Magento/QuoteGraphQl/Model/Cart/ExtractDataFromAddress.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,42 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\QuoteGraphQl\Model\Cart\Address\Mapper;
8+
namespace Magento\QuoteGraphQl\Model\Cart;
99

10+
use Magento\Framework\Api\ExtensibleDataObjectConverter;
11+
use Magento\Quote\Api\Data\AddressInterface;
1012
use Magento\Quote\Model\Quote\Address as QuoteAddress;
1113

1214
/**
13-
* Class Address
14-
*
1515
* Extract the necessary address fields from an Address model
1616
*/
17-
class Address
17+
class ExtractDataFromAddress
1818
{
1919
/**
20-
* Converts Address model data to nested array
20+
* @var ExtensibleDataObjectConverter
21+
*/
22+
private $dataObjectConverter;
23+
24+
/**
25+
* @param ExtensibleDataObjectConverter $dataObjectConverter
26+
*/
27+
public function __construct(ExtensibleDataObjectConverter $dataObjectConverter)
28+
{
29+
$this->dataObjectConverter = $dataObjectConverter;
30+
}
31+
32+
/**
33+
* Converts Address model to flat array
2134
*
2235
* @param QuoteAddress $address
2336
* @return array
2437
*/
25-
public function toNestedArray(QuoteAddress $address): array
38+
public function execute(QuoteAddress $address): array
2639
{
27-
$addressData = [
40+
$addressData = $this->dataObjectConverter->toFlatArray($address, [], AddressInterface::class);
41+
$addressData['model'] = $address;
42+
43+
$addressData = array_merge($addressData, [
2844
'country' => [
2945
'code' => $address->getCountryId(),
3046
'label' => $address->getCountry()
@@ -41,7 +57,7 @@ public function toNestedArray(QuoteAddress $address): array
4157
],
4258
'items_weight' => $address->getWeight(),
4359
'customer_notes' => $address->getCustomerNotes()
44-
];
60+
]);
4561

4662
if (!$address->hasItems()) {
4763
return $addressData;

app/code/Magento/QuoteGraphQl/Model/Resolver/BillingAddress.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,24 @@
1111
use Magento\Framework\GraphQl\Config\Element\Field;
1212
use Magento\Framework\GraphQl\Query\ResolverInterface;
1313
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
14-
use Magento\QuoteGraphQl\Model\Cart\Address\BillingAddressDataProvider;
14+
use Magento\QuoteGraphQl\Model\Cart\ExtractDataFromAddress;
1515

1616
/**
1717
* @inheritdoc
1818
*/
1919
class BillingAddress implements ResolverInterface
2020
{
2121
/**
22-
* @var BillingAddressDataProvider
22+
* @var ExtractDataFromAddress
2323
*/
24-
private $addressDataProvider;
24+
private $extractDataFromAddress;
2525

2626
/**
27-
* @param BillingAddressDataProvider $addressDataProvider
27+
* @param ExtractDataFromAddress $extractDataFromAddress
2828
*/
29-
public function __construct(
30-
BillingAddressDataProvider $addressDataProvider
31-
) {
32-
$this->addressDataProvider = $addressDataProvider;
29+
public function __construct(ExtractDataFromAddress $extractDataFromAddress)
30+
{
31+
$this->extractDataFromAddress = $extractDataFromAddress;
3332
}
3433

3534
/**
@@ -40,9 +39,14 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
4039
if (!isset($value['model'])) {
4140
throw new LocalizedException(__('"model" value should be specified'));
4241
}
43-
4442
$cart = $value['model'];
4543

46-
return $this->addressDataProvider->getCartAddresses($cart);
44+
$billingAddress = $cart->getBillingAddress();
45+
if (null === $billingAddress) {
46+
return null;
47+
}
48+
49+
$addressData = $this->extractDataFromAddress->execute($billingAddress);
50+
return $addressData;
4751
}
4852
}

0 commit comments

Comments
 (0)